Fix wslservice lifetime callback teardown to stop long-run threadpool resource buildup#40486
Draft
Copilot wants to merge 5 commits into
Draft
Fix wslservice lifetime callback teardown to stop long-run threadpool resource buildup#40486Copilot wants to merge 5 commits into
Copilot wants to merge 5 commits into
Conversation
2 tasks
Agent-Logs-Url: https://github.com/microsoft/WSL/sessions/b6855d2b-63ea-4edf-ba7c-81eb71f49268 Co-authored-by: benhillis <17727402+benhillis@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/WSL/sessions/b6855d2b-63ea-4edf-ba7c-81eb71f49268 Co-authored-by: benhillis <17727402+benhillis@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix memory leaks in wslservice.exe
Fix wslservice lifetime callback teardown to stop long-run threadpool resource buildup
May 10, 2026
Replace open-coded SetThreadpoolWait/SetThreadpoolTimer null calls in ClearCallbacks and RemoveCallback with the existing ClientCallback::CancelTimer helper and a new symmetric OwnedProcess::CancelWait helper. Expand the ClearCallbacks comment block to explain the full strategy: why the per-client members use the _nowait wil variants (chain-of-waits in m_lastCallbackWait/m_lastTimerWait), why teardown re-homes them into the cancel-and-wait variants, and how in-flight callbacks are drained safely outside m_lock by the local container destructors. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens LifetimeManager teardown in wslservice.exe to prevent long-running WSL1 scenarios from accumulating threadpool wait/timer resources by explicitly canceling/unregistering per-client threadpool objects and draining them outside m_lock.
Changes:
- Update
LifetimeManager::ClearCallbacks()to cancel timers and unregister waits, then re-home threadpool handles into draining WIL RAII containers outside the lock. - Update
LifetimeManager::RemoveCallback()to apply the same cancel/unregister + drain behavior for single-client removal. - Add
OwnedProcess::CancelWait()helper to centralize unregistering a threadpool wait.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/windows/service/exe/Lifetime.h | Adds OwnedProcess::CancelWait() declaration to support explicit wait unregistration during teardown. |
| src/windows/service/exe/Lifetime.cpp | Implements cancel/unregister + out-of-lock draining for waits/timers in ClearCallbacks and RemoveCallback, and defines OwnedProcess::CancelWait(). |
Address Copilot reviewer feedback: release()-then-emplace_back leaks the raw PTP_WAIT/PTP_TIMER if the vector grows and allocation throws (the source has already lost ownership before emplace_back can construct the new element). Wrap each released handle in a named wil RAII temporary first, then push_back(std::move(...)). The temporary owns the handle while push_back may throw, ensuring the handle is properly closed via the destructor on failure. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| } | ||
|
|
||
| void LifetimeManager::OwnedProcess::CancelWait() const | ||
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of the Pull Request
On long-running WSL1 workloads,
wslservice.execan accumulate large commit size and thread count. This change tightensLifetimeManagerteardown so per-client threadpool waits/timers are explicitly canceled/unregistered when callbacks are removed or globally cleared.PR Checklist
Detailed Description of the Pull Request / Additional comments
Scoped fix:
LifetimeManager::ClearCallbacksSetThreadpoolWait(..., nullptr, nullptr)) before releasing ownership.Scoped fix:
LifetimeManager::RemoveCallbackBehavioral intent
Validation Steps Performed
LifetimeManagercallback removal and global clear flows.