mxc-sdk 1.0.1

Importable library for starting MXC sandboxes in-process, with streaming stdio.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Streaming WSLC backend — the handle-based counterpart of
//! [`WSLContainerRunner`]'s run-to-completion `ScriptRunner`.
//!
//! Implementing [`SandboxBackend`] for the same runner lets the Rust SDK
//! (`mxc-sdk`, via `mxc_engine`) spawn a WSL container and drive it live: read
//! its stdout/stderr while it runs, wait for it with the request's timeout, and
//! kill it.
//!
//! Both this and the run-to-completion runner share one lifecycle
//! implementation (`start_container`); they differ only in where the WSLC SDK's
//! output callbacks send their bytes.
//!
//! # What WSLC can and cannot stream
//!
//! - **stdout / stderr** — the SDK pushes them to callbacks, which this module
//!   forwards to an in-memory stream per handle (see [`crate::wslc_common::stream_buffer`]);
//!   the caller reads the other end. Native-stdio callers receive synthesized
//!   OS pipes pumped from those streams because the SDK exposes bytes rather
//!   than pipe handles. Each exposed output needs its own blocking pump because
//!   the SDK offers no waitable or overlapped read primitive; the cost is
//!   therefore bounded at two threads per native-streaming sandbox.
//! - **stdin** — the WSLC SDK exposes no process-input API, so
//!   [`take_stdin`](SandboxProcess::take_stdin) always returns `None`.
//! - **[`id`](SandboxProcess::id)** — the process lives inside the WSL VM and
//!   has no host process id, so this is always `0`.

use std::io::{Read, Write};
use std::sync::mpsc::{sync_channel, SyncSender};
use std::sync::OnceLock;

use crate::mxc_common::logger::{Logger, Mode};
use crate::mxc_common::models::{ExecutionRequest, ScriptResponse};
use crate::mxc_common::process_util::{create_local_pipe, PipeWriter};
use crate::mxc_common::sandbox_process::{
    boxed_closer, cancel_and_join_discard, spawn_discard, take_boxed_read, NativeStdio, OwnedPipe,
    SandboxBackend, SandboxProcess, StdioMode, StreamCloser,
};
use crate::mxc_common::script_runner::ScriptRunner;
use crate::mxc_common::validator::validate_common;
use windows::core::PCWSTR;
use windows::Win32::Foundation::HMODULE;
use windows::Win32::System::LibraryLoader::{
    GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GET_MODULE_HANDLE_EX_FLAG_PIN,
};

use crate::wslc_common::stream_buffer::{StreamCanceller, StreamReader};
use crate::wslc_common::wsl_container_runner::{OutputMode, StartedContainer, WSLContainerRunner};
use crate::wslc_common::wslc_bindings::WslcSignal;

impl SandboxBackend for WSLContainerRunner {
    fn validate(&self, request: &ExecutionRequest) -> Result<(), ScriptResponse> {
        ScriptRunner::validate_runner(self, request)
    }

    fn spawn(
        &mut self,
        request: &ExecutionRequest,
        logger: &mut Logger,
        stdio: StdioMode,
    ) -> Result<Box<dyn SandboxProcess>, ScriptResponse> {
        if matches!(stdio, StdioMode::Pty(_)) {
            return Err(ScriptResponse::rejected(
                "WSLC does not support caller-controlled PTY spawning",
            ));
        }
        // The run-to-completion path gets these through `ScriptRunner::run`;
        // the streaming path bypasses that, so — like every other
        // `SandboxBackend` — apply them here. Beyond rejecting an empty
        // command, this is what enforces the central testing-features gate on
        // `network.proxy.builtinTestServer`.
        validate_common(request)?;
        self.validate(request)?;

        // SAFETY: `start_container` drives the WSLC SDK over raw FFI; it owns
        // every buffer it passes in and returns RAII guards for every handle it
        // opens, so the returned value is self-contained.
        let started = unsafe { self.start_container(request, logger, OutputMode::Stream(stdio)) }?;
        Ok(Box::new(WslcSandboxProcess::new(started)))
    }
}

/// The `ErrorKind::TimedOut` error [`SandboxProcess::wait`] reports when the
/// request's `scriptTimeout` elapsed and the container was torn down.
fn timeout_error(timeout_ms: u32) -> std::io::Error {
    std::io::Error::new(
        std::io::ErrorKind::TimedOut,
        format!("Process timed out after {timeout_ms}ms and was terminated"),
    )
}

/// The error reported when the deadline elapsed but the kill could **not** be
/// confirmed.
///
/// Deliberately *not* `ErrorKind::TimedOut`: the public
/// `mxc_sdk::v1::MxcProcess::wait` maps that kind onto `WaitResult::TimedOut`, a
/// success value whose contract is that the process tree *was* killed — which
/// would silently restore the very claim this case exists to avoid, discarding
/// the message with it. Being unable to establish the sandbox's state is a
/// genuine failure, so it surfaces as one and reaches the caller as `Err`.
fn unconfirmed_termination_error(timeout_ms: u32) -> std::io::Error {
    std::io::Error::other(format!(
        "Process timed out after {timeout_ms}ms; the container could not be confirmed terminated \
         and may still be running (call wait() to retry the teardown)"
    ))
}

/// A running WSL container's init process.
struct WslcSandboxProcess {
    started: StartedContainer,
    stdout: Option<StreamReader>,
    stderr: Option<StreamReader>,
    stdout_canceller: Option<StreamCanceller>,
    stderr_canceller: Option<StreamCanceller>,
    /// Pump handles are retained but deliberately not joined: after process
    /// exit they may still be delivering queued bytes to caller-owned pipes,
    /// and joining would block if the caller left a full pipe unread.
    native_output_pumps: Vec<std::thread::JoinHandle<()>>,
    /// Exit code cached by the first successful wait, so repeat waits are
    /// idempotent (and don't re-run teardown).
    exit: Option<i32>,
    /// Set once the wait deadline elapsed *and* the kill was confirmed, so
    /// `TimedOut` is reported stickily rather than a later-observed exit code.
    /// Deliberately narrower than [`deadline_elapsed`](Self::deadline_elapsed):
    /// this flag is what promises the process was terminated, so it must never
    /// be set on an unconfirmed kill.
    timed_out: bool,
    /// Set once the wait deadline elapsed, whether or not the kill that follows
    /// could be confirmed. Records only that the deadline is spent, so a repeat
    /// `wait` resumes at confirmation instead of blocking out another full one.
    deadline_elapsed: bool,
    /// Set once the container has been *confirmed* torn down, so `Drop` doesn't
    /// repeat it — and, just as importantly, still retries when teardown failed
    /// or could not be confirmed. Tracked separately from `exit`: a caller that
    /// only ever polls [`try_wait`](SandboxProcess::try_wait) learns the exit
    /// code without any teardown having run.
    torn_down: bool,
    /// Diagnostics from the SDK calls this handle makes after the spawn
    /// returned. The streaming contract has nowhere to flush them — the spawn
    /// itself already logged through the caller's logger — so they are
    /// buffered and dropped with the handle.
    logger: Logger,
}

// SAFETY: the WSLC handles inside `StartedContainer` are opaque SDK pointers,
// not COM interface pointers the caller marshals: `init_and_load_sdk` enters the
// *multithreaded* apartment (`COINIT_MULTITHREADED`), so the SDK's objects live
// in the MTA and the SDK itself invokes our I/O and exit callbacks on its own
// internal threads — it is free-threaded by design.
//
// `CoInitializeEx` is nonetheless per-thread, so moving this handle to a thread
// that never initialized COM would otherwise leave the SDK on the stack of an
// apartment-less thread. Every entry point that calls the SDK joins the MTA for
// the duration via `ComApartment`, mirroring `process_container_common`'s guard:
// `StartedContainer`'s `wait_for_exit` / `destroy` / `stop` / `exit_code`, and
// — because a `Send` handle can be *dropped* on such a thread too — the
// `Drop` impls of the `WslcSessionGuard` / `WslcContainerGuard` /
// `WslcProcessGuard` handles it owns. No apartment state or interface pointer
// is cached across calls, so setup and teardown may run on different threads.
//
// The invariant holds with no exception: where an apartment cannot be
// established at all, every one of those paths declines to call the SDK rather
// than proceeding without one — leaking the handle (the guards, which have no
// error channel) or reporting the failure (`destroy`).
//
// The handle is *moved* between threads, never shared (`Sync` is deliberately
// not claimed), so at most one thread calls into the SDK at a time.
unsafe impl Send for WslcSandboxProcess {}

/// What an earlier wait already established about this process's outcome, which
/// every later `wait` / `try_wait` must keep reporting.
///
/// Extracted so the two entry points cannot drift: the trait's promise is that
/// once a run is reported as timed out, no later status query downgrades it to
/// an exit code — and a timeout *kills* the container, so the raw SDK exit code
/// is always available to be mistakenly reported.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Settled {
    /// The deadline elapsed and the kill was confirmed.
    TimedOut,
    /// The deadline elapsed, but the kill could not be confirmed. Still a
    /// timeout — the deadline is spent — with the termination unproven.
    DeadlineUnconfirmed,
    /// The process exited on its own and teardown completed.
    Exited(i32),
    /// Nothing established yet; ask the container.
    Pending,
}

/// Decide the sticky outcome from the flags an earlier wait set.
///
/// `deadline_elapsed` and `exit` are mutually exclusive by construction (the
/// first is only set on the timeout path, the second only on the normal one),
/// so the order these are tested in does not matter.
fn settled_outcome(timed_out: bool, deadline_elapsed: bool, exit: Option<i32>) -> Settled {
    if timed_out {
        Settled::TimedOut
    } else if deadline_elapsed {
        Settled::DeadlineUnconfirmed
    } else if let Some(code) = exit {
        Settled::Exited(code)
    } else {
        Settled::Pending
    }
}

impl WslcSandboxProcess {
    fn new(mut started: StartedContainer) -> Self {
        let pipes = started.pipes.take();
        let (stdout, stderr) = match pipes {
            Some(pipes) => (Some(pipes.stdout), Some(pipes.stderr)),
            None => (None, None),
        };
        Self {
            stdout_canceller: stdout.as_ref().map(|r| r.canceller()),
            stderr_canceller: stderr.as_ref().map(|r| r.canceller()),
            stdout,
            stderr,
            native_output_pumps: Vec::new(),
            started,
            exit: None,
            timed_out: false,
            deadline_elapsed: false,
            torn_down: false,
            logger: Logger::new(Mode::Buffer),
        }
    }

    /// This handle's [`Settled`] outcome so far.
    fn settled(&self) -> Settled {
        settled_outcome(self.timed_out, self.deadline_elapsed, self.exit)
    }

    /// Complete the timeout path: confirm the sandboxed process is really gone,
    /// tear the container down, and only then make `TimedOut` sticky.
    ///
    /// `wait_for_exit`'s timeout branch only *asks* the container to stop
    /// (`SIGTERM`, short grace, HRESULT ignored), so reaching it proves nothing
    /// about whether the process died — while returning `TimedOut` promises it
    /// was terminated. Every failure here therefore leaves both `timed_out` and
    /// `torn_down` unset, so the promise is never made on an unconfirmed kill
    /// and a later `wait` (or `Drop`) retries instead.
    fn finish_timed_out(&mut self) -> std::io::Result<i32> {
        self.started
            .confirm_terminated(&mut self.logger)
            .map_err(std::io::Error::other)?;
        self.started
            .destroy(&mut self.logger)
            .map_err(std::io::Error::other)?;
        self.torn_down = true;
        self.timed_out = true;
        Err(timeout_error(self.started.timeout_ms))
    }
}

pub(crate) struct PreparedNativeOutput {
    reader: Option<OwnedPipe>,
    start: Option<SyncSender<StreamReader>>,
    pump: Option<std::thread::JoinHandle<()>>,
}

impl PreparedNativeOutput {
    pub(crate) fn activate(
        mut self,
        source: StreamReader,
    ) -> std::io::Result<(OwnedPipe, std::thread::JoinHandle<()>)> {
        let start = self
            .start
            .take()
            .ok_or_else(|| std::io::Error::other("native output pump is already active"))?;
        start
            .send(source)
            .map_err(|_| std::io::Error::other("native output pump stopped before activation"))?;
        let reader = self
            .reader
            .take()
            .ok_or_else(|| std::io::Error::other("native output reader is unavailable"))?;
        let pump = self
            .pump
            .take()
            .ok_or_else(|| std::io::Error::other("native output pump is unavailable"))?;
        Ok((reader, pump))
    }
}

impl Drop for PreparedNativeOutput {
    fn drop(&mut self) {
        self.start.take();
        if let Some(pump) = self.pump.take() {
            let _ = pump.join();
        }
    }
}

/// Keep the module containing output pump code loaded for the process lifetime.
///
/// Native pipe readers outlive the sandbox handle, so an active pump may still
/// return from a blocking write after `mxc_sandbox_free` has completed.
fn pin_output_pump_module() -> std::io::Result<()> {
    static MODULE_PIN: OnceLock<Result<(), String>> = OnceLock::new();

    MODULE_PIN
        .get_or_init(|| {
            let mut module = HMODULE::default();
            // SAFETY: FROM_ADDRESS interprets the pointer as an address within
            // the current module rather than as a string. PIN keeps that module
            // loaded until process exit, so detached pumps can finish safely.
            unsafe {
                GetModuleHandleExW(
                    GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
                    PCWSTR(pin_output_pump_module as *const () as *const u16),
                    &mut module,
                )
            }
            .map_err(|error| format!("failed to pin the WSLC output-pump module: {error}"))
        })
        .as_ref()
        .map_err(|error| std::io::Error::other(error.clone()))
        .copied()
}

/// Prepare a native pipe and an idle output pump before detaching a WSLC stream.
///
/// WSLC exposes stdout and stderr as callback-backed streams rather than
/// transferable OS handles. The pump copies each stream into a local pipe so
/// FFI consumers can adopt its native read endpoint. WSLC does not currently
/// expose a corresponding native stdin endpoint.
///
/// A shared worker cannot safely drain both callback streams: each read may
/// block independently, and failing to drain either stream can stall the
/// container. Keep the explicit one-pump-per-output tradeoff until the WSLC SDK
/// exposes a waitable or cancellable asynchronous read primitive.
pub(crate) fn prepare_native_output() -> std::io::Result<PreparedNativeOutput> {
    pin_output_pump_module()?;
    let (reader, writer) = create_local_pipe().map_err(std::io::Error::other)?;
    let mut destination = PipeWriter::new(writer);
    let (start, source) = sync_channel::<StreamReader>(1);
    let pump = std::thread::Builder::new()
        .name("mxc-wslc-output-pump".to_string())
        .spawn(move || {
            let Ok(mut source) = source.recv() else {
                return;
            };
            // A closed native read end reports BrokenPipe and ends the pump.
            // Dropping `source` then abandons the callback queue so it cannot
            // grow after the caller has stopped consuming this stream.
            let _ = std::io::copy(&mut source, &mut destination);
        })?;
    Ok(PreparedNativeOutput {
        reader: Some(reader.try_into_std_owned_handle()?),
        start: Some(start),
        pump: Some(pump),
    })
}

impl SandboxProcess for WslcSandboxProcess {
    fn take_native_stdio(&mut self) -> std::io::Result<Option<NativeStdio>> {
        if self.stdout.is_none() && self.stderr.is_none() {
            return Ok(None);
        }

        // Allocate pipes and start idle workers before detaching either source,
        // so setup failure leaves the ordinary stream API untouched.
        let stdout_pipe = self
            .stdout
            .as_ref()
            .map(|_| prepare_native_output())
            .transpose()?;
        let stderr_pipe = self
            .stderr
            .as_ref()
            .map(|_| prepare_native_output())
            .transpose()?;

        let stdout = match (self.stdout.take(), stdout_pipe) {
            (Some(source), Some(pipe)) => {
                let (reader, pump) = pipe.activate(source)?;
                self.native_output_pumps.push(pump);
                Some(reader)
            }
            _ => None,
        };
        let stderr = match (self.stderr.take(), stderr_pipe) {
            (Some(source), Some(pipe)) => {
                let (reader, pump) = pipe.activate(source)?;
                self.native_output_pumps.push(pump);
                Some(reader)
            }
            _ => None,
        };

        self.stdout_canceller.take();
        self.stderr_canceller.take();

        Ok(Some(NativeStdio {
            stdin: None,
            stdout,
            stderr,
        }))
    }

    /// Always `None`: the WSLC SDK has no API to write to a container process's
    /// stdin.
    fn take_stdin(&mut self) -> Option<Box<dyn Write + Send>> {
        None
    }

    fn take_stdout(&mut self) -> Option<Box<dyn Read + Send>> {
        take_boxed_read(&mut self.stdout)
    }

    fn take_stderr(&mut self) -> Option<Box<dyn Read + Send>> {
        take_boxed_read(&mut self.stderr)
    }

    fn try_wait(&mut self) -> std::io::Result<Option<i32>> {
        // The settled outcome is checked exactly as in `wait`. Without this,
        // the timeout path — which kills the container — leaves `has_exited()`
        // true below, so a caller that saw `wait` fail with `TimedOut` would
        // get `Ok(Some(<kill code>))` from a later poll: two contradictory
        // answers, with the timeout silently downgraded to a normal exit.
        match self.settled() {
            Settled::TimedOut => return Err(timeout_error(self.started.timeout_ms)),
            // `try_wait` is non-blocking, so confirmation cannot be retried
            // here the way `wait` retries it — but an exit code would still be
            // the wrong answer for a spent deadline.
            Settled::DeadlineUnconfirmed => {
                return Err(unconfirmed_termination_error(self.started.timeout_ms))
            }
            // A cached code means `wait` already ran the full teardown.
            Settled::Exited(code) => return Ok(Some(code)),
            Settled::Pending => {}
        }
        if !self.started.has_exited() {
            return Ok(None);
        }
        // Deliberately *not* cached into `self.exit`: `wait` treats a cached
        // code as proof that it already drained the streams, destroyed the
        // container, and set `torn_down`, so caching here would make a later
        // `wait` skip teardown entirely — and would mask a prior timeout,
        // turning a `TimedOut` into a normal exit. Re-querying is a cheap
        // SDK call.
        // SAFETY: `started` owns live process and SDK handles.
        unsafe { self.started.exit_code() }
            .map_err(std::io::Error::other)
            .map(Some)
    }

    /// Always `0` — the sandboxed process runs inside the WSL VM and has no
    /// host process id.
    fn id(&self) -> u32 {
        0
    }

    fn kill(&mut self) -> std::io::Result<()> {
        // WSLC has no per-process signal API: stopping the container terminates
        // the init process and everything it spawned — the tree kill the trait
        // asks for. `0` means "don't wait for a graceful stop".
        self.started
            .stop(WslcSignal::WSLC_SIGNAL_SIGKILL, 0)
            .map_err(std::io::Error::other)
    }

    fn wait(&mut self) -> std::io::Result<i32> {
        match self.settled() {
            // Sticky: a confirmed timeout must keep being reported as one,
            // never as a later-observed exit code.
            Settled::TimedOut => return Err(timeout_error(self.started.timeout_ms)),
            Settled::Exited(code) => return Ok(code),
            // An earlier call already burned the deadline but could not confirm
            // the kill. Resume at the confirmation step: re-entering
            // `wait_for_exit` would block for another full deadline, while
            // reporting `TimedOut` outright would promise a kill that never
            // happened.
            Settled::DeadlineUnconfirmed => return self.finish_timed_out(),
            Settled::Pending => {}
        }

        // Drain the streams the caller did not take, concurrently with the
        // wait, so unread output can't accumulate for the life of the process
        // (see the trait's pipe-deadlock contract — here the buffer is
        // in-memory and unbounded, so the cost of not draining is memory rather
        // than a stalled child). A stream the caller *did* take is left alone.
        let stdout_drain = spawn_discard(self.stdout.take());
        let stderr_drain = spawn_discard(self.stderr.take());

        let waited = self.started.wait_for_exit(&mut self.logger);

        // Release the drain readers we own first, whichever way the wait went:
        // firing their canceller can't truncate anything the caller is reading
        // (`cancel_and_join_discard` only fires for a stream we drained), and
        // joining them here keeps a later error return from leaving threads
        // parked on a read.
        cancel_and_join_discard(stdout_drain, &self.stdout_canceller);
        cancel_and_join_discard(stderr_drain, &self.stderr_canceller);

        // Deliberately before `close_streams`: `wait_for_exit` can fail while
        // the process is still running (a failed apartment, or
        // `WslcGetProcessExitEvent`), and closing the SDK-side writers then
        // would EOF a caller-taken reader and discard every later callback for
        // a process that is still producing output.
        let (exit_code, outcome) = waited.map_err(|response| {
            std::io::Error::other(if response.error_message.is_empty() {
                "waiting for the WSL container process failed".to_string()
            } else {
                response.error_message
            })
        })?;

        // The process is gone (or was killed): close the SDK-side write ends so
        // every reader — ours and the caller's — sees EOF once it has consumed
        // what was already buffered.
        self.started.close_streams();

        if outcome.timed_out() {
            // Records only that the deadline elapsed. Even
            // `WaitOutcome::TimedOutTerminated` goes through `finish_timed_out`,
            // whose `confirm_terminated` short-circuits on an already-confirmed
            // exit — so the confirmed case costs nothing and the unconfirmed one
            // gets the retry it needs.
            self.deadline_elapsed = true;
            return self.finish_timed_out();
        }

        // The exit callback fired, so the process is confirmed gone and only the
        // container object can still leak. `torn_down` therefore tracks whether
        // teardown actually succeeded, leaving `Drop` to retry it if not.
        self.torn_down = self.started.destroy(&mut self.logger).is_ok();
        self.exit = Some(exit_code);
        Ok(exit_code)
    }

    fn stdout_closer(&self) -> Option<Box<dyn StreamCloser>> {
        boxed_closer(&self.stdout_canceller)
    }

    fn stderr_closer(&self) -> Option<Box<dyn StreamCloser>> {
        boxed_closer(&self.stderr_canceller)
    }
}

impl Drop for WslcSandboxProcess {
    /// A handle dropped before [`wait`](SandboxProcess::wait) tore the container
    /// down still owns one — possibly still running: stop it (and, when the
    /// request asked for it, delete it) rather than leaking a VM-backed sandbox.
    fn drop(&mut self) {
        if self.torn_down {
            return;
        }
        // Settles the container and blocks on the exit callback before the
        // fields it owns are released — the SDK must have stopped calling back
        // before the `IoContext` is freed and the DLL unloaded.
        let mut logger = Logger::new(Mode::Buffer);
        self.started.quiesce(&mut logger);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::wslc_common::wsl_container_runner::START_CONTAINER_BANNER;
    use std::fs::File;
    use std::io::Read;
    use std::time::Duration;

    /// The banner is what makes this host-independent: on a host that *has*
    /// `wslcsdk.dll`, a guard moved after `start_container` would leak a
    /// container and still return the same message. `start_container` writes the
    /// banner first, so an untouched buffer proves it was never entered.
    #[test]
    fn spawn_rejects_policy_before_touching_the_wslc_sdk() {
        let request = ExecutionRequest {
            containment: crate::mxc_common::models::ContainmentBackend::Wslc,
            script_code: "echo hi".to_string(),
            policy: crate::mxc_common::models::ContainerPolicy {
                ui_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        let mut logger = Logger::new(Mode::Buffer);
        let mut runner = WSLContainerRunner::new(&crate::mxc_common::models::WslcConfig::default());

        let err = runner
            .spawn(&request, &mut logger, StdioMode::Pipes)
            .err()
            .expect("a rejected policy must not produce a live container");
        assert!(
            err.error_message.contains("ui section is not supported"),
            "the refusal must come from the policy gate, not from bring-up: {}",
            err.error_message
        );
        assert_eq!(
            err.failure_phase,
            crate::mxc_common::models::FailurePhase::Rejected
        );
        assert!(
            !logger.get_buffer().contains(START_CONTAINER_BANNER),
            "guard must run before `start_container`; logger: {}",
            logger.get_buffer()
        );
    }

    /// Regression for #902 on the streaming path: an unmappable cwd is refused
    /// before `start_container`, rather than dropped once the container exists.
    #[test]
    fn spawn_rejects_unmappable_working_directory_before_touching_the_wslc_sdk() {
        let request = ExecutionRequest {
            containment: crate::mxc_common::models::ContainmentBackend::Wslc,
            script_code: "pwd".to_string(),
            working_directory: "/workspace".to_string(),
            ..Default::default()
        };
        let mut logger = Logger::new(Mode::Buffer);
        let mut runner = WSLContainerRunner::new(&crate::mxc_common::models::WslcConfig::default());

        let err = runner
            .spawn(&request, &mut logger, StdioMode::Pipes)
            .err()
            .expect("an unmappable cwd must not produce a live container");
        assert!(
            err.error_message.contains("\"/workspace\""),
            "the refusal must name the cwd: {}",
            err.error_message
        );
        assert_eq!(
            err.failure_phase,
            crate::mxc_common::models::FailurePhase::Rejected
        );
        assert!(
            !logger.get_buffer().contains(START_CONTAINER_BANNER),
            "guard must run before `start_container`; logger: {}",
            logger.get_buffer()
        );
    }

    /// The exact contradiction the streaming contract forbids: a timed-out run
    /// is killed, so the container *has* an exit code to report — and reporting
    /// it would turn `wait`'s `Err(TimedOut)` into a later `Ok(Some(137))`.
    /// Both entry points read this one decision, so they cannot disagree.
    #[test]
    fn a_confirmed_timeout_is_never_downgraded_to_an_exit_code() {
        assert_eq!(
            settled_outcome(true, true, None),
            Settled::TimedOut,
            "a confirmed timeout stays a timeout"
        );
        // Even with an exit code somehow recorded, the timeout still wins.
        assert_eq!(
            settled_outcome(true, true, Some(137)),
            Settled::TimedOut,
            "a recorded exit code must not mask a confirmed timeout"
        );
    }

    /// A deadline that elapsed but whose kill could not be confirmed is still a
    /// timeout — never an exit code — but it is reported apart from the
    /// confirmed case so nothing claims a termination that was not shown.
    #[test]
    fn an_unconfirmed_deadline_is_distinct_from_a_confirmed_timeout() {
        assert_eq!(
            settled_outcome(false, true, None),
            Settled::DeadlineUnconfirmed,
        );
        assert_ne!(
            settled_outcome(false, true, None),
            Settled::TimedOut,
            "an unconfirmed kill must not report as a completed termination"
        );
    }

    #[test]
    fn a_normal_exit_is_reported_once_teardown_cached_it() {
        assert_eq!(settled_outcome(false, false, Some(0)), Settled::Exited(0));
        assert_eq!(settled_outcome(false, false, Some(3)), Settled::Exited(3));
    }

    #[test]
    fn a_fresh_handle_has_nothing_settled_yet() {
        assert_eq!(settled_outcome(false, false, None), Settled::Pending);
    }

    /// The safety property behind both entry points: once the deadline has
    /// elapsed, *no* combination of flags may answer "exited normally". This is
    /// what stops a killed container's exit code from masking the timeout.
    #[test]
    fn a_spent_deadline_never_yields_an_exit_code() {
        for timed_out in [false, true] {
            for exit in [None, Some(0), Some(137)] {
                let outcome = settled_outcome(timed_out, true, exit);
                assert!(
                    !matches!(outcome, Settled::Exited(_)),
                    "a spent deadline (timed_out={timed_out}, exit={exit:?}) must not report an \
                     exit code, got {outcome:?}"
                );
            }
        }
    }

    /// Only a *confirmed* kill may carry `ErrorKind::TimedOut`.
    ///
    /// `mxc_sdk::v1::MxcProcess::wait` turns that kind into `WaitResult::TimedOut`, a
    /// success value promising the process tree was killed — and drops the
    /// message doing it. An unconfirmed kill must therefore not use that kind,
    /// or the distinction would be erased at the public API boundary.
    #[test]
    fn only_a_confirmed_kill_uses_the_timedout_error_kind() {
        let confirmed = timeout_error(1500);
        let unconfirmed = unconfirmed_termination_error(1500);

        assert_eq!(confirmed.kind(), std::io::ErrorKind::TimedOut);
        assert_ne!(
            unconfirmed.kind(),
            std::io::ErrorKind::TimedOut,
            "an unconfirmed kill must not be convertible into WaitOutcome::TimedOut"
        );

        assert!(confirmed.to_string().contains("1500ms"));
        assert!(unconfirmed.to_string().contains("1500ms"));
        assert!(
            confirmed.to_string().contains("was terminated"),
            "the confirmed message states the termination"
        );
        assert!(
            !unconfirmed.to_string().contains("was terminated"),
            "the unconfirmed message must not claim a termination it did not prove"
        );
        assert!(
            unconfirmed.to_string().contains("may still be running"),
            "the unconfirmed message must say the container may still be running"
        );
    }

    #[test]
    fn native_output_bridge_delivers_bytes_and_eof() {
        let (writer, reader) = crate::wslc_common::stream_buffer::stream_pair();
        let pipe = prepare_native_output().expect("create native output pipe");
        let (native_reader, pump) = pipe.activate(reader).expect("activate output pump");
        let mut native_reader = File::from(native_reader);

        writer.write(b"hello from wslc");
        writer.close();

        let mut output = Vec::new();
        native_reader
            .read_to_end(&mut output)
            .expect("read bridged output");
        pump.join().expect("output pump");
        assert_eq!(output, b"hello from wslc");
    }

    #[test]
    fn native_output_bridge_preserves_large_output_after_delayed_read() {
        let expected = vec![b'x'; 1024 * 1024];
        let (writer, reader) = crate::wslc_common::stream_buffer::stream_pair();
        let pipe = prepare_native_output().expect("create native output pipe");
        let (native_reader, pump) = pipe.activate(reader).expect("activate output pump");
        let mut native_reader = File::from(native_reader);

        writer.write(&expected);
        writer.close();
        std::thread::sleep(Duration::from_millis(50));

        let mut output = Vec::new();
        native_reader
            .read_to_end(&mut output)
            .expect("read bridged output");
        pump.join().expect("output pump");
        assert_eq!(output, expected);
    }

    #[test]
    fn dropping_native_reader_ends_the_output_pump() {
        let (writer, reader) = crate::wslc_common::stream_buffer::stream_pair();
        let pipe = prepare_native_output().expect("create native output pipe");
        let (native_reader, pump) = pipe.activate(reader).expect("activate output pump");
        drop(native_reader);

        writer.write(b"unobserved output");
        writer.close();

        pump.join().expect("output pump");
    }

    #[test]
    fn dropping_unactivated_native_output_joins_idle_pump() {
        let pipe = prepare_native_output().expect("prepare native output");
        drop(pipe);
    }
}