Skip to content

Windows: direct Window.close() can tear down the Flutter engine before the Dart FFI call returns #59

Done
libnativeapi/nativeapi-core
#104

Description

@Empty-57

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_manager 0.7.1, nativeapi and cnativeapi 0.6.2

Trigger in the affected application

  1. Use a single-window Windows runner with SetQuitOnClose(true) and the standard WM_DESTROY handler that calls Destroy().
  2. Get the main window with WindowManager.instance.getCurrent()!. The app registers an ordinary window-event listener for focus, resize and similar events.
  3. 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

  1. lijy91 commented on Nov 30, 2026

    @lijy91
    Member

    Fixed in libnativeapi/nativeapi-core#104 and synced to main in #61.

    Root Cause

    When Flutter runs Dart on the Windows platform thread (UIThreadPolicy::RunOnPlatformThread), calling Window.close() (or EventDecision.accept()) ran Window::DispatchWindowTask and Policy::Post inline and invoked SendMessageW(hwnd, WM_CLOSE, ...) synchronously while the Dart FFI call was still on the stack. If the runner's WM_CLOSE / WM_DESTROY handler destroyed the Flutter view controller and shut down the engine, Dart_EnterIsolate aborted with 0xC0000409 (FAST_FAIL_FATAL_APP_EXIT).

    Fix

    • Added Policy::QueueDelivery in src/platform/windows/window_close_windows.cpp so approved WM_CLOSE / SC_CLOSE delivery is always posted via CompletionMessage() to a later Win32 message-loop turn on the owning thread, allowing Dart FFI callers to unwind before host teardown runs.
    • Preserved synchronous veto (Cancel()) semantics while coalescing repeated Window.close() calls during queued delivery (delivery_pending) and fencing stale queued deliveries if the window is destroyed or receives a forced WM_CLOSE / WM_QUERYENDSESSION before dispatch (delivery_generation).
    • Added Windows regression tests in tests/window_close_windows_test.cpp and verified with a Windows x64 Release Flutter app under UIThreadPolicy::RunOnPlatformThread (both direct window.close() and deferred decision.accept() now exit cleanly with code 0 instead of 0xC0000409).

    This is now on main and will be included in the next release.

  2. added a commit that references this issue on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions