Skip to content

Fix NRE when the application shuts down while showing the main window - #22497

Merged
MrJul merged 1 commit into
AvaloniaUI:mainfrom
mfkl:fix-shutdown-during-main-window-show
Oct 19, 2026
Merged

MrJul merged 1 commit into
AvaloniaUI:mainfrom
mfkl:fix-shutdown-during-main-window-show

Conversation

@mfkl

@mfkl mfkl commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

What does the pull request do?

Fixes a NullReferenceException in ClassicDesktopStyleApplicationLifetime.StartCore when the application shuts down while the main window is being shown, e.g. when lifetime.Shutdown() or Close() is called from the main window's Opened handler.

What is the current behavior?

Window.Opened is raised synchronously inside MainWindow.Show(). If the handler shuts the application down (explicitly, or by closing the last/main window), DoShutdown cancels _cts, sets it to null and shuts down the dispatcher before StartCore reaches Dispatcher.UIThread.MainLoop(_cts.Token), which then throws a NullReferenceException.

What is the updated/expected behavior with this PR?

Start() returns normally with the exit code set by Shutdown(exitCode) (or by the Exit event handler), and Environment.ExitCode is set as before.

New tests in DesktopStyleApplicationLifetimeTests:

  • Should_Return_ExitCode_When_Shutdown_From_MainWindow_Opened
  • Should_Exit_When_MainWindow_Closed_From_Opened

How was the solution implemented (if it's not obvious)?

StartCore now keeps the token in a local and only enters MainLoop if it hasn't been cancelled yet. A null check alone isn't enough: when shutdown happens before any dispatcher frame is pushed, InvokeShutdown completes the dispatcher shutdown immediately, and a later MainLoop call throws InvalidOperationException ("Cannot perform requested operation because the Dispatcher shut down") from PushFrame.

ShutdownRequestedEventArgs.WillExitMainLoop is still true in this case, which remains correct: Start() returns right away, so deferring to the managed side (as AvaloniaNativeApplicationPlatform does) is still the right behavior.

Checklist

Breaking changes

None.

Obsoletions / Deprecations

None.

Fixed issues

Fixes #7265

@MrJul MrJul added bug backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Oct 16, 2026
@github-actions

Copy link
Copy Markdown

NuGet packages

The packages for this pull request have been published (workflow run):

@MrJul
MrJul added this pull request to the merge queue Oct 19, 2026
Merged via the queue into AvaloniaUI:main with commit da7ea04 Oct 19, 2026
12 checks passed
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.NullReferenceException at Window.Close() from Opened or Activated handler

2 participants