Wait for init to exit before trying to remount the distro VHD - #41669
Blue (OneBlue) wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Normal instance creation can still reuse the VHD/LUN while init is unmounting, so the reported race remains unresolved.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 2
Open (2)
What changed in this PR
Adds termination tracking so distro operations can wait for WSL2 init to exit before reusing VHDs.
Changes:
- Tracks pending init termination events.
- Waits during selected conversion and VHD operations.
- Logs timeout warnings without blocking the session.
| File | Review |
|---|---|
src/windows/service/exe/LxssUserSession.h |
Adds termination tracking state and wait APIs. Critical: normal instance creation does not wait before reusing the distro VHD/LUN. |
src/windows/service/exe/LxssUserSession.cpp |
Implements termination tracking and waits for selected operations. Critical: the CreateInstance path still does not consume pending termination waits. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
PID reuse can associate exit notifications with the wrong distro, and unregister can still eject the VHD before init exits.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Resolved since last review (2)
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Unresolved termination ordering and default-distribution selection issues remain.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (1)
Resolved since last review (1)
| std::vector<LxssUserSessionImpl::PidTermination> LxssUserSessionImpl::_ConversionBegin(_In_ GUID DistroGuid, _In_ LxssDistributionState State) | ||
| { | ||
| _EnsureNotLocked(&DistroGuid); | ||
| _TerminateInstanceInternal(&DistroGuid); | ||
|
|
||
| auto pidTerminations = _GetPidTerminations(DistroGuid); | ||
| m_lockedDistributions.emplace_back(DistroGuid, State); | ||
| return pidTerminations; |

Summary of the Pull Request
This change solves a race condition that has been observed in the CI. If a distribution is force-terminated, its LUN can be reused by a future instance creation while init is still in the process of unmounting its filesystem, which can fail the instance creation.
This change solves this by actually waiting for init to exit to consider that the distribution is stopped. The timeout reused the distribution start timeout for now (we can add a .wslconfig entry for it later if needed), and the timeout only logs a warning for now, so a single broken distro doesn't deadlock the entire user session
PR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed