Repository navigation
Fix NRE when the application shuts down while showing the main window - #22497
Merged
Merged
Conversation
NuGet packagesThe packages for this pull request have been published (workflow run):
|
MrJul
approved these changes
Oct 19, 2026
MrJul
pushed a commit
that referenced
this pull request
Nov 2, 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.
What does the pull request do?
Fixes a
NullReferenceExceptioninClassicDesktopStyleApplicationLifetime.StartCorewhen the application shuts down while the main window is being shown, e.g. whenlifetime.Shutdown()orClose()is called from the main window'sOpenedhandler.What is the current behavior?
Window.Openedis raised synchronously insideMainWindow.Show(). If the handler shuts the application down (explicitly, or by closing the last/main window),DoShutdowncancels_cts, sets it tonulland shuts down the dispatcher beforeStartCorereachesDispatcher.UIThread.MainLoop(_cts.Token), which then throws aNullReferenceException.What is the updated/expected behavior with this PR?
Start()returns normally with the exit code set byShutdown(exitCode)(or by theExitevent handler), andEnvironment.ExitCodeis set as before.New tests in
DesktopStyleApplicationLifetimeTests:Should_Return_ExitCode_When_Shutdown_From_MainWindow_OpenedShould_Exit_When_MainWindow_Closed_From_OpenedHow was the solution implemented (if it's not obvious)?
StartCorenow keeps the token in a local and only entersMainLoopif it hasn't been cancelled yet. A null check alone isn't enough: when shutdown happens before any dispatcher frame is pushed,InvokeShutdowncompletes the dispatcher shutdown immediately, and a laterMainLoopcall throwsInvalidOperationException("Cannot perform requested operation because the Dispatcher shut down") fromPushFrame.ShutdownRequestedEventArgs.WillExitMainLoopis stilltruein this case, which remains correct:Start()returns right away, so deferring to the managed side (asAvaloniaNativeApplicationPlatformdoes) is still the right behavior.Checklist
Breaking changes
None.
Obsoletions / Deprecations
None.
Fixed issues
Fixes #7265