Skip to content

Defer approved Windows WM_CLOSE delivery to next message turn - #104

Merged
lijy91 merged 1 commit into
mainfrom
fix/windows-window-close-defer-issue-59
Nov 30, 2026
Merged

lijy91 merged 1 commit into
mainfrom
fix/windows-window-close-defer-issue-59

Conversation

@lijy91

@lijy91 lijy91 commented Nov 30, 2026

Copy link
Copy Markdown
Member

Fixes libnativeapi/nativeapi#59.

Root Cause

On Windows, when Flutter runs Dart on the platform thread (UIThreadPolicy::RunOnPlatformThread), calling Window::Close() (or EventDecision::Accept()) executed DispatchWindowTask and Policy::Post inline and invoked SendMessageW(hwnd, WM_CLOSE, ...) synchronously on the Dart FFI call stack. When the runner's WM_CLOSE / WM_DESTROY handler tore down the Flutter engine while the Dart FFI call was still on the stack, Dart_EnterIsolate aborted with 0xC0000409 (FAST_FAIL_FATAL_APP_EXIT).

Changes

  • Added Policy::QueueDelivery and defer_on_owner_thread in src/platform/windows/window_close_windows.cpp so approved WM_CLOSE / SC_CLOSE delivery is always posted via CompletionMessage() to a subsequent message-loop turn on the owning thread.
  • Preserved synchronous veto (Cancel()) semantics while coalescing repeated Close() calls via delivery_pending and fencing stale queued deliveries on WM_CLOSE / WM_DESTROY / WM_NCDESTROY / WM_QUERYENDSESSION via delivery_generation.
  • Added regression tests in tests/window_close_windows_test.cpp covering owner-thread direct Close(), owner-thread EventDecision::Accept(), coalescing, raw WM_CLOSE refusal invalidation, and DestroyWindow before dispatch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant