Skip to content

Fix WSLC SDK IO callback startup race - #42304

Open
ggarzia-MSFT wants to merge 6 commits into
masterfrom
user/ggarzia/fix-io-callback-startup-race
Open

ggarzia-MSFT wants to merge 6 commits into
masterfrom
user/ggarzia/fix-io-callback-startup-race

Conversation

@ggarzia-MSFT

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

Fixes a startup race in the WSLC SDK. In an Event-mode container, an output callback that runs during WslcStartContainer, before ioCallbacks is published, could release the container. The callback could also run before the WinRT init Process had its handle attached. Callbacks now stay dormant until ownership is published and handles are attached.

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

The IOCallback constructor started its worker thread, and that thread delivered callbacks right away. Startup could therefore interleave like this:

  1. A callback calls WslcReleaseContainer (or WinRT Close()). The IO-thread guard sees no ioCallbacks yet, so it lets the native container be freed.
  2. WslcStartContainer continues and assigns ioCallbacks into freed memory.
  3. In WinRT, the init Process (the callback context) can be destroyed before its handle is attached.

Validation Steps Performed

  • New test WslcSdkTests::ReleaseContainerFromIOCallbackDuringStart: the output callback releases the container during start. It expects ERROR_INVALID_HANDLE_STATE and the container to still be running.
  • New test WslcSdkWinRtTests::EventModeInitProcessAttachedBeforeOutputCallback: the first OutputReceived handler uses the init process. It expects the handle to already be attached.

…wnership is published

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ggarzia-MSFT
ggarzia-MSFT requested review from a team as code owners November 18, 2026 18:57
Copilot AI balanced review requested due to automatic review settings November 18, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical close-during-activation race can destroy the WinRT Process context while the output worker is still alive, requiring a lifetime fix and regression coverage for closing before Start() returns.

1 open issue
What changed in this PR

This PR addresses WSLC SDK startup races by deferring I/O callbacks until ownership is published and, for WinRT containers, the init-process handle is attached.

Changes:

  • Add explicit callback activation and cancellation handling for inactive workers.
  • Introduce internal startup entry points to coordinate native and WinRT initialization.
  • Add regression tests for callback-triggered release and init-process handle readiness.
File Description
test/​windows/​wslc/​WslcSdkWinRTTests.cpp Test init-process handle readiness during output callbacks.
test/​windows/​wslc/​WslcSdkTests.cpp Test rejection of container release from a startup callback.
src/​windows/​WslcSDK/​WslcsdkInternal.h Declare deferred-start and callback-activation entry points.
src/​windows/​WslcSDK/​wslcsdk.def Export internal entry points by ordinal.
src/​windows/​WslcSDK/​wslcsdk.cpp Publish callback ownership and process handles before activation.
src/​windows/​WslcSDK/​winrt/​Container.cpp Activate callbacks after attaching the init-process handle.
src/​windows/​WslcSDK/​IOCallback.h Declare activation support and its synchronization event.
src/​windows/​WslcSDK/​IOCallback.cpp Gate callback delivery and handle inactive-worker cleanup.
src/​windows/​WslcSDK/​CMakeLists.txt Include the new internal header in the SDK target.

Review effort: Balanced

Comment thread src/windows/WslcSDK/winrt/Container.cpp Resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 19:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical exec-process lifetime race allows WslcReleaseProcess to return while a callback is still executing, risking use-after-free of the callback context; explicit cancellation and joining plus a regression test are needed.

2 open issues

Review effort: Balanced

Comment thread src/windows/WslcSDK/wslcsdk.cpp Resolved
…ose-during-start test

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 20:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical lifecycle issues remain: releasing an init-process alias cancels shared container callbacks, and closing from an output callback can free context still used by the worker, causing use-after-free.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

High severity Callback-thread Close clears init-process ownership after release is rejected

src/​windows/​WslcSDK/​winrt/​Container.cpp:88

The first OutputReceived handler can still call container.Close() on the callback thread: native release rejects this, but Container::Close() ignores the failure and clears m_initProcess. Without an external process reference, this destroys the raw callback context while the worker remains alive, leaving subsequent callbacks to access freed memory. Preserve ownership when release is rejected or defer teardown off the callback thread, and add a same-thread-close test without retaining an external InitProcess reference.

2 resolved since last review

Review effort: Balanced

Comment thread src/windows/WslcSDK/wslcsdk.cpp Resolved
…ndle

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 21:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to evaluate potential indefinite blocking and data races involving callback shutdown and independently released container and process handles.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

OneBlue
Blue (OneBlue) previously approved these changes Nov 19, 2026

@OneBlue Blue (OneBlue) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor comment

Comment thread test/windows/wslc/WslcSdkTests.cpp Resolved
Comment thread src/windows/WslcSDK/wslcsdk.def Resolved
…ep with retry in init handle release test

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 01:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Releasing the init-process handle before deferred callback activation cancels the container-owned worker, causing later activation to deliver neither buffered output nor onExit.

1 open issue

Review effort: Balanced

Comment thread src/windows/WslcSDK/wslcsdk.cpp Resolved
…andle before activation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 01:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

A critical use-after-free risk when an output handler closes the container requires human assessment of callback lifetime and cleanup behavior.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

};

template <>
struct FlagsTraits<WslcContainerStartFlags>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must add new flag here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to add it here because it never gets passed to the runtime

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants