Repository navigation
Description
A Windows x64 Flutter release application crashes when its title-bar button calls Window.close() on the main window. The same app closes cleanly when the runner defers engine teardown until after the message loop returns.
Environment
- Windows x64, OS build 26100
- Flutter 3.50.1, Dart 3.14.0
window_manager0.7.1,nativeapiandcnativeapi0.6.2
Trigger in the affected application
- Use a single-window Windows runner with
SetQuitOnClose(true)and the standardWM_DESTROYhandler that callsDestroy(). - Get the main window with
WindowManager.instance.getCurrent()!. The app registers an ordinary window-event listener for focus, resize and similar events. - From the Flutter title-bar button, finish async cleanup, remove that listener, then call
window.close(). The app has no close-confirmation handler.
Expected: the process exits normally. Actual: the release process terminates with 0xC0000409, subcode 0x7 FAST_FAIL_FATAL_APP_EXIT. The observed crash is from the application described above.
Crash stack and likely code path
WinDbg with matching Flutter engine symbols shows the following call sequence on the crashing thread (outermost first):
cnativeapi.dll -> user32!SendMessageW -> runner window procedure
-> FlutterDesktopViewControllerDestroy -> FlutterEngineShutdown
-> flutter::DartIsolate::Shutdown -> Dart_EnterIsolate
-> dart::Assert::Fail -> abort
In the Windows implementation, both Window::DispatchWindowTask and Policy::Post execute work inline when called on the owning window thread. After the close is approved, Policy::Request uses SendMessageW(hwnd, WM_CLOSE, ...). With the last window closing, that permits the runner to destroy the engine while the Dart-originated call is still on the stack. Could the approved programmatic close be queued for a later window-message turn on the owner thread too, while preserving cancellation and handling a window destroyed before dispatch?
The earlier report leanflutter/window_manager#593 involved the legacy confirm-before-close flow. Its fix deferred the vote after event acknowledgment. This case uses a direct Window.close() after the listener has been removed.
Verified application workaround
For the quit-on-close main window, our runner handles an accepted WM_CLOSE by calling PostQuitMessage(0) and returning 0. After the message loop exits, it calls window.Destroy() before CoUninitialize(). With the original Dart window.close() retained, both the Flutter title-bar button and the native tray menu exited with code 0 in separate Release runs, with no new crash dump.
Activity
lijy91 commentedon Nov 30, 2026
Fixed in libnativeapi/nativeapi-core#104 and synced to
mainin #61.Root Cause
When Flutter runs Dart on the Windows platform thread (
UIThreadPolicy::RunOnPlatformThread), callingWindow.close()(orEventDecision.accept()) ranWindow::DispatchWindowTaskandPolicy::Postinline and invokedSendMessageW(hwnd, WM_CLOSE, ...)synchronously while the Dart FFI call was still on the stack. If the runner'sWM_CLOSE/WM_DESTROYhandler destroyed the Flutter view controller and shut down the engine,Dart_EnterIsolateaborted with0xC0000409(FAST_FAIL_FATAL_APP_EXIT).Fix
Policy::QueueDeliveryinsrc/platform/windows/window_close_windows.cppso approvedWM_CLOSE/SC_CLOSEdelivery is always posted viaCompletionMessage()to a later Win32 message-loop turn on the owning thread, allowing Dart FFI callers to unwind before host teardown runs.Cancel()) semantics while coalescing repeatedWindow.close()calls during queued delivery (delivery_pending) and fencing stale queued deliveries if the window is destroyed or receives a forcedWM_CLOSE/WM_QUERYENDSESSIONbefore dispatch (delivery_generation).tests/window_close_windows_test.cppand verified with a Windows x64 Release Flutter app underUIThreadPolicy::RunOnPlatformThread(both directwindow.close()and deferreddecision.accept()now exit cleanly with code0instead of0xC0000409).This is now on
mainand will be included in the next release.Merge pull request #61 from libnativeapi/fix/issue-59