fix: implement IDisposable on WslConfigService to dispose FileSystemWatcher#40249
Merged
fix: implement IDisposable on WslConfigService to dispose FileSystemWatcher#40249
Conversation
…atcher FileSystemWatcher holds unmanaged OS resources and implements IDisposable. Without disposing it, the watcher leaks handles. Implement the standard Dispose pattern. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the WinUI WslConfigService (used by the Settings app via DI) to deterministically clean up its FileSystemWatcher by implementing the standard .NET IDisposable pattern, instead of relying on non-deterministic finalization.
Changes:
- Implement
IDisposableonWslConfigService. - Add
Dispose(bool)to release managed resources (FileSystemWatcher) and unmanaged resources (FreeWslConfig), and update the finalizer to callDispose(false). - Suppress finalization when
Dispose()is called.
bf7d272 to
3efbe5b
Compare
Member
Author
|
Addressed the synchronization feedback — Dispose(bool) now takes _wslCoreConfigInterfaceLockObj before touching the watcher or config objects. Removed the _wslConfig = null\ / _wslConfigDefaults = null\ assignments since _wslConfigDefaults\ has { get; init; }\ (can't assign outside constructor). Also added unsubscribe for \Deleted\ and \Renamed\ handlers. Force-pushed the fix — initial build was failing due to the init-only property assignment. |
OneBlue
reviewed
Apr 20, 2026
…atcher Synchronize Dispose with _wslCoreConfigInterfaceLockObj to prevent racing with OnWslConfigFileChanged/SetWslConfigSetting. Use _wslConfigFileSystemWatcher null check instead of a separate _disposed flag so the watcher is always cleaned up regardless of Dispose(true) vs Dispose(false) ordering. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
3efbe5b to
06e69b9
Compare
OneBlue
approved these changes
Apr 20, 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.
Problem
\WslConfigService\ creates a \FileSystemWatcher\ but never disposes it. The existing finalizer only frees unmanaged WslConfig objects — the managed \FileSystemWatcher\ and its OS handles leak until the GC finalizes them (which is non-deterministic and not guaranteed).
Fix
Implement the standard \IDisposable\ pattern:
This follows the standard .NET dispose pattern and ensures deterministic cleanup of the file watcher's OS handles.