Skip to content

[WSLC] RAII guard Drop impls call SDK teardown outside a COM apartment #721

Closed

Description

Brief description of your issue

The WSLc RAII guards WslcSessionGuard, WslcContainerGuard, and WslcProcessGuard
call SDK teardown functions (WslcTerminateSession/WslcReleaseSession,
WslcReleaseContainer, WslcReleaseProcess) in their Drop impls without entering
a COM apartment
(ComApartment::enter()), unlike every other SDK entry point
(destroy/stop/wait_for_exit), which join the MTA for the call.

init_and_load_sdk initializes COM per-thread (COINIT_MULTITHREADED). On the
run-to-completion path this is harmless because spawn/wait/teardown all run on the one
thread that entered the apartment. But once the streaming SandboxBackend path is used,
the Send WslcSandboxProcess/Sandbox handle can be moved to and dropped on a thread
that never joined the MTA (e.g. a tokio spawn_blocking worker, or
wait_with_output(mut self) consuming and dropping on the waiting thread) — so heavy
session/container/process teardown runs on an apartment-less thread, contradicting the
unsafe impl Send for WslcSandboxProcess soundness argument
(src/backends/wslc/common/src/sandbox.rs:105-121).

Source: src/backends/wslc/common/src/wslc_bindings.rs:362-441.

Notes:

  • AppContainer already fixed this same class: its NetworkManager used to cache an STA
    COM interface and reuse it at teardown, and was reworked so each firewall op is
    apartment-self-contained (src/backends/appcontainer/common/src/appcontainer_runner.rs:1388-1404).

Steps to reproduce

  1. Build with --features wslc and enable the experimental streaming path.
  2. Spawn a WSLc sandbox via the mxc-sdk streaming API (spawn_sandbox).
  3. Move the returned handle to another thread (e.g. tokio spawn_blocking, or call
    wait_with_output) and let it drop there.
  4. The guard Drop impls call WslcTerminateSession/WslcReleaseContainer/
    WslcReleaseProcess on a thread that never entered the MTA apartment.

Expected behavior

SDK teardown always runs inside the multithreaded apartment the SDK was initialized
under — either by having the guards enter a ComApartment in Drop, or by tearing the
handles down explicitly inside an apartment scope before the guards drop (mirroring
destroy/stop and the AppContainer NetworkManager fix).

Actual behavior

The guard Drop impls call the SDK release/terminate functions with no ComApartment,
so on a cross-thread drop the teardown runs on an apartment-less thread.

Activity

  1. added
    Issue-Bug
    Needs-Triage
    on Jul 31, 2026
  2. added
    Backend-WSLC
    and removed
    Needs-Triage
    on Jul 31, 2026
  3. added
    Needs-Triage
    OS-Windows
    and removed
    Needs-Triage
    on Jul 31, 2026
  4. added
    OS-Linux
    OS-Windows
    and removed
    OS-Windows
    OS-Linux
    on Jul 31, 2026
  5. github-actions commented on Jul 31, 2026

    @github-actions

    Triage complete.

    • Labels applied: Issue-Bug, OS-Linux, Container-WSLC (already present — confirmed correct)
    • Assigned to: Soham Das (@SohamDas2021) (WSLC / Linux area owner, already assigned)

    This is a soundness bug in the WSLC backend: WslcSessionGuard, WslcContainerGuard, and WslcProcessGuard Drop impls invoke SDK teardown functions without entering a ComApartment, which is unsafe when the handle is dropped on a thread that never joined the MTA (e.g. via spawn_blocking or wait_with_output). The fix should mirror the AppContainer NetworkManager pattern — either enter ComApartment in Drop, or ensure explicit teardown always happens inside an apartment scope before the guards drop.

    Maintainers: Comment /investigate to check whether this issue or bug is valid against the most current code. Copilot will also produce a report with the changes that will be needed. For a small, unambiguous documentation or test fix, it may also create one draft PR.

    Generated by 🏷️ Issue Triage for #721 · 22 AIC · ⌖ 5.64 AIC · ⊞ 6.6K · ◷

  6. github-actions commented on Jul 31, 2026

    @github-actions

    Triage complete.

    • Labels applied: existing labels (Issue-Bug, OS-Windows, Container-WSLC) are correct — no new labels added.
    • Owner assigned: @SohamDas2021 (already assigned — WSLC / Linux backend owner).
    • Needs-Triage removed.

    The issue is a soundness bug in the WSLC RAII guard Drop impls: WslcSessionGuard, WslcContainerGuard, and WslcProcessGuard call SDK teardown functions without entering a ComApartment, which is unsafe when handles are dropped on threads that never joined the MTA (e.g. after Send-ing across tokio threads or calling wait_with_output).

    Maintainers: Comment /investigate to check whether this issue or bug is valid against the most current code. Copilot will also produce a report with the changes that will be needed. For a small, unambiguous documentation or test fix, it may also create one draft PR.

    Generated by 🏷️ Issue Triage for #721 · 16.3 AIC · ⌖ 5.51 AIC · ⊞ 6.6K · ◷

  7. SohamDas2021 commented on Jul 31, 2026

    @SohamDas2021
    ContributorAuthor

    Needs to go after #687

  8. SohamDas2021 commented on Aug 10, 2026

    @SohamDas2021
    ContributorAuthor

    Fixed in #687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Backend-WSLCIssue-BugOS-Windows

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions