fix: make app shutdown disposal idempotent and ownership-correct#856
Merged
Conversation
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
Makes the application's shutdown teardown idempotent and ownership-correct. App cleanup
ran 2–3 times on every exit; the redundant passes double-released the network charts'
unmanaged SkiaSharp paint/typeface handles. The resulting
ObjectDisposedExceptionwascaught and hidden (in
App.OnExitand the global handler) but happened on every close.Root cause
MainWindowViewModel.Dispose()is wired to two shutdown paths — the window'sOnClosedoverride andApplication.Current.Exit(MainWindow.xaml.cs:54, 133) — andthe child VMs/services it disposes are DI singletons the
ServiceProviderdisposesagain on teardown (
App.xaml.cs:92). NeitherMainWindowViewModelnorNetworkSharedStatehad a_disposedguard (they derive fromObservableObject, notViewModelBase, so they didn't inherit the existing guard), so the unmanaged painthandles in
NetworkSharedState.Dispose()were freed more than once — undefined behavior.Additionally,
NetworkSharedState.Dispose()calledPinger.Dispose()/TraceMonitor.Dispose()on monitor services that are themselves DI singletons — anownership inversion (a consumer disposing a container-owned dependency).
Changes
NetworkSharedState: added aprivate bool _disposed;guard (if (_disposed) return; _disposed = true;) — same pattern asViewModelBase/TrayIconService. ReplacedPinger.Dispose()/TraceMonitor.Dispose()withPinger.Stop()/TraceMonitor.Stop()— stop the loops, let the container own disposal of the singletons. (It already calledStop()beforeDispose(), so this just drops the redundant, ownership-invertingDispose.)MainWindowViewModel: added the same_disposedguard at the top ofDispose().Dispose_CalledTwice_DoesNotThrowinNetworkSharedStateTestspins the bug — a secondDispose()must be a no-op.Why behavior is otherwise unchanged
The first
Dispose()does exactly what it did before (minus the redundant singletonDisposethatStop()already covered). Child VMs derived fromViewModelBasewerealready idempotent via their own guard; this adds the guard to the two
ObservableObjectroots that lacked it. Runtime behavior on a single clean shutdown is identical; the
double/triple passes are now safe no-ops instead of hidden exceptions.
Verification
version bump (1.20.13 → 1.20.14) in this same PR.
fix:→ patch release (1.20.14). Protocol C to follow after merge.