Repository navigation
fix: memory leak in mainThreadTask - #331198
Simon Siefke (SimonSiefke) wants to merge 27 commits into
Conversation
…ry-leak-mainThreadTask
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR makes task system registrations disposable so callers can unregister task systems automatically (e.g., when the registering component is disposed).
Changes:
- Change
ITaskService.registerTaskSystemto return anIDisposable. - Implement unregister-on-dispose behavior in
AbstractTaskService.registerTaskSystem. - Register the returned disposable in
MainThreadTaskand add a test to verify disposal unregisters task systems.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/vs/workbench/contrib/tasks/common/taskService.ts | Updates the task service interface to return an IDisposable for task system registrations. |
| src/vs/workbench/contrib/tasks/browser/abstractTaskService.ts | Returns a disposable that removes task system info from the internal registry on dispose. |
| src/vs/workbench/api/browser/mainThreadTask.ts | Registers the task system disposable so it’s cleaned up when MainThreadTask is disposed. |
| src/vs/workbench/api/test/browser/mainThreadTask.test.ts | Adds a unit test ensuring task system registrations are disposed/unregistered. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Potential remote-startup regression: removal can release task-system readinessThe new removal notification addresses the earlier review feedback, but it changes an assumption made by the readiness consumers. At reviewed head
Consequently, if one host is disposed before initial remote readiness, its removal can release pending task discovery and automatic-task startup. Suggested deterministic reproduction / regression test (not yet executed)
The ordering in step 5 matters for the baseline comparison: at true base Please preserve removal notifications, but make the readiness consumers wait for a notification where Validation scope: this is a source-backed, medium-severity correctness concern. The proposed sequence has not been run in Code OSS or as a unit test; no live reproduction or measured task failure is claimed. |
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
Please consider feedback in the comment
|
Added a separate PR #342418 to fix a task system race condition which should address the review feedback from this PR. |
This PR is needed for #331198 <br> ### Details A task-system change notification can arrive before the required task systems are ready. Readiness can also change before an awaiting callback resumes, or the automatic-task contribution can be disposed while it waits. These cases can start task discovery too early or after disposal. ### Change Wait for notifications that satisfy `hasTaskSystemInfo`, recheck readiness after each wait, and dispose pending listeners with their owner. Stop automatic-task startup when the contribution has been disposed. ### Before Four readiness regression tests fail on the base branch: a removal notification resolves readiness too early, automatic tasks start before remote readiness, and pending startup work continues or leaves a listener after disposal. ### After All five readiness tests pass, including local startup, changes before remote readiness, and disposal before or immediately after readiness. ### Test Video https://github.com/user-attachments/assets/7a5a4570-74a6-4000-b2c5-7fc0d10af4fd <br> <sub>AI disclosure: Model: GPT-6.1 Sol. Worktime: 13 min</sub>
Preserve both task test suites and the readiness disposal guard. Dispose task-system registrations and command registrations in the integrated test fixtures.
|
With the other PR merged, all feedback should be all addressed now! |
There was a problem hiding this comment.
🟡 Changes recommended
An unresolved readiness gap after remote extension-host restarts allows task discovery without current task-system information, potentially parsing remote tasks with incorrect client-platform overrides.
3 open issues
3 resolved since last review
Review effort: Max
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
Please consider review comments
| // Execution needs a live host; platform-only task discovery does not. | ||
| while (!this._store.isDisposed) { | ||
| const info = this._getTaskSystemInfo(key); | ||
| if (info || (key !== Schemas.vscodeRemote && !this._taskSystemPlatforms.has(key))) { |
There was a problem hiding this comment.
Could execution require a compatible native registration here, rather than accepting any surviving registration for the scheme?
After a remote-native-only extension-host crash, a Web worker can remain registered under vscode-remote. Removing the native registration promotes that worker, so this condition immediately calls the execution callback with Platform.Web. On a Windows remote, TerminalTaskSystem then takes the Linux default-shell-profile branch and skips Windows shell-argument/process-executable handling. The remote pty host is separate from the extension host, so terminal creation can still proceed.
This is distinct from the earlier task-discovery/platform-cache issue, which the new metadata separation addresses. Before this PR the stale native registration's RPC rejected; the regression is substituting an incompatible execution provider, not breaking a previously successful crash-window run.
Suggested regression test (not executed): register native Windows and Web systems for the same remote workspace, dispose only the native registration while keeping the worker, then launch/rerun a shell or process task before native replacement. Verify that native execution does not use Web/Linux platform handling, then restore the native registration and verify correct execution. A global Restart Extension Host command may stop both hosts and miss this ordering.
Source-backed finding at 0f238cf; no runtime reproduction claimed.
| return callback(info); | ||
| } | ||
| await Event.toPromise(Event.any(this.onDidChangeTaskSystemInfo, | ||
| listener => this._taskSystemInfoCancellation.onCancellationRequested(listener))); |
There was a problem hiding this comment.
Could this wait also settle when the task is cancelled or the native host will not be restarted?
For a previously registered scheme with no surviving registration, this loop only wakes on registration changes or whole-task-service disposal. If the host never returns (for example after automatic restart is exhausted), the task stays pending indefinitely. TerminalTaskSystem inserts its active-task entry before awaiting execution and removes it in the execution promise's finally; that cleanup cannot run while this wait is pending. Terminate returns failure because no terminal exists yet, retries encounter the existing-instance policy, and manually restoring the host later can launch the abandoned request.
Before this change the stale host RPC rejected and the execution promise settled, allowing the active entry to be removed. The new indefinite wait therefore introduces a lifecycle regression, rather than merely preserving the existing host-loss failure.
Suggested regression test (not executed): remove the last native registration after initial readiness, run a configured task while withholding replacement, then cancel/terminate it. Verify the execution settles and the active entry is cleared; registering a replacement afterward must not launch the cancelled request. Cover both remote and previously registered local schemes, since retained platform metadata makes both enter this wait.
Source-backed finding at 0f238cf; no runtime reproduction claimed. Cancellation tied only to task-service disposal does not cover this task-level lifetime.
This PR is needed for microsoft/vscode#331198 <br> ### Details A task-system change notification can arrive before the required task systems are ready. Readiness can also change before an awaiting callback resumes, or the automatic-task contribution can be disposed while it waits. These cases can start task discovery too early or after disposal. ### Change Wait for notifications that satisfy `hasTaskSystemInfo`, recheck readiness after each wait, and dispose pending listeners with their owner. Stop automatic-task startup when the contribution has been disposed. ### Before Four readiness regression tests fail on the base branch: a removal notification resolves readiness too early, automatic tasks start before remote readiness, and pending startup work continues or leaves a listener after disposal. ### After All five readiness tests pass, including local startup, changes before remote readiness, and disposal before or immediately after readiness. ### Test Video https://github.com/user-attachments/assets/7a5a4570-74a6-4000-b2c5-7fc0d10af4fd <br> <sub>AI disclosure: Model: GPT-6.1 Sol. Worktime: 13 min</sub>
Preserve task system registration disposal tests alongside upstream task provider timeout regression coverage.



Details
The same
AbstractTaskServiceis reused when the extension host restarts. Its task-system registrations kept callbacks from the oldMainThreadTaskinstance in memory after each restart.Change
registerTaskSystemnow returns a disposable.MainThreadTaskregisters it for cleanup, so its callbacks are unregistered when the instance is disposed.Before
Restarting extensions 37 times retained one additional set of
uriProvider,resolveVariables, andfindExecutablecallbacks per restart:After
The matching task-system callback growth is gone (162 rows before, 159 unrelated rows after).
Test Video
test.mp4