Fix disposable leak warning from in-process node.js file watcher#320246
Merged
Conversation
NodeJSFileWatcherLibrary.watch() awaited doWatch() and then called this._register() on the result. If the watcher was disposed during that async window, _store was already disposed, producing a "disposable added to a disposed store" warning and leaking the fs.watch handle. Use thenRegisterOrDispose so the resolved handle is disposed instead of registered when the watcher is torn down mid-flight. This is common in the agent host where non-recursive watching runs in-process and SessionCustomizationDiscovery churns watchers on file changes. Fixes #320245 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a disposable-registration race in the in-process Node.js file watcher implementation that could emit “Trying to add a disposable to a DisposableStore that has already been disposed of…” and leak the underlying fs.watch handle when the watcher is disposed while doWatch() is still resolving (notably common in the agent host due to frequent watcher churn).
Changes:
- Replaces
this._register(await this.doWatch(...))withthenRegisterOrDispose(this.doWatch(...), this._store)to ensure the resolved watch handle is disposed (not registered) if the watcher’s store was already disposed mid-flight. - Adds an explanatory comment documenting the async disposal race and the intent of the fix.
Show a summary per file
| File | Description |
|---|---|
| src/vs/platform/files/node/watcher/nodejs/nodejsWatcherLib.ts | Uses thenRegisterOrDispose to avoid registering into a disposed store and to dispose resolved watcher handles instead of leaking them. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 0
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
alexdima
approved these changes
Jun 6, 2026
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.
Fixes #320245
NodeJSFileWatcherLibrary.watch()awaiteddoWatch()and then calledthis._register()on the result. If the watcher instance was disposed while that promise was in flight,_storewas already disposed by the time_registerran, producing:and leaking the underlying
fs.watchhandle.This surfaces frequently in the agent host because non-recursive file watching runs in-process there, and
SessionCustomizationDiscoverytears down and recreates watchers on the same paths whenever workspace files change.The fix uses the existing
thenRegisterOrDisposehelper (already used elsewhere in the same file) so the resolved handle is disposed instead of registered + leaked when the watcher is torn down mid-flight.Type-check (
compile-check-ts-native) passes.(Written by Copilot)