From 0e5a6774c38c2f427fa4ba594816a05346cc8fd8 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Mon, 30 Nov 2026 20:12:38 +0800 Subject: [PATCH] Defer approved Windows WM_CLOSE delivery to next message turn --- src/platform/windows/window_close_windows.cpp | 49 +++++++++++++++---- tests/window_close_windows_test.cpp | 49 +++++++++++++++++++ 2 files changed, 89 insertions(+), 9 deletions(-) diff --git a/src/platform/windows/window_close_windows.cpp b/src/platform/windows/window_close_windows.cpp index fd6e9473..f3c47972 100644 --- a/src/platform/windows/window_close_windows.cpp +++ b/src/platform/windows/window_close_windows.cpp @@ -26,14 +26,16 @@ struct Policy : std::enable_shared_from_this { bool bypass = false; bool required = false; bool session_ending = false; + bool delivery_pending = false; + uint64_t delivery_generation = 0; bool Valid() const { return !closed.load() && IsWindow(hwnd) && GetPropW(hwnd, kPolicy) == box && static_cast(reinterpret_cast(GetPropW(hwnd, kId))) == id; } - bool Post(std::function work) { + bool Post(std::function work, bool defer_on_owner_thread = false) { if (!Valid()) return false; - if (GetCurrentThreadId() == thread) { + if (!defer_on_owner_thread && GetCurrentThreadId() == thread) { work(); return true; } @@ -64,22 +66,47 @@ struct Policy : std::enable_shared_from_this { if (Valid()) entry.second(); } + bool QueueDelivery(UINT message, WPARAM wp, LPARAM lp) { + if (!Valid()) + return false; + if (delivery_pending) + return true; + const uint64_t generation = ++delivery_generation; + delivery_pending = true; + std::weak_ptr weak = shared_from_this(); + // Always defer the approved close to a later message-loop turn on the + // owning thread so a synchronous caller (such as a Dart FFI Window.close() + // or EventDecision.accept() on the merged UI/platform thread) unwinds + // before the host WM_CLOSE handler can tear down the window or runtime. + if (Post( + [weak, generation, message, wp, lp] { + auto policy = weak.lock(); + if (!policy || !policy->Valid() || policy->delivery_generation != generation) + return; + policy->delivery_pending = false; + const bool previous = policy->bypass; + policy->bypass = true; + SendMessageW(policy->hwnd, message, wp, lp); + policy->bypass = previous; + }, + /*defer_on_owner_thread=*/true)) + return true; + delivery_pending = false; + return false; + } bool Request(UINT message, WPARAM wp, LPARAM lp) { if (!Valid() || required) return false; - if (session_ending) { - SendMessageW(hwnd, message, wp, lp); + if (delivery_pending) return true; - } + if (session_ending) + return QueueDelivery(message, wp, lp); std::weak_ptr weak = shared_from_this(); return state->Request([weak, message, wp, lp] { auto policy = weak.lock(); if (!policy || !policy->Valid()) return; - const bool previous = policy->bypass; - policy->bypass = true; - SendMessageW(policy->hwnd, message, wp, lp); - policy->bypass = previous; + (void)policy->QueueDelivery(message, wp, lp); }); } }; @@ -90,6 +117,8 @@ CloseProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp, UINT_PTR subclass, DWOR auto policy = *box; if (message == WM_NCDESTROY) { policy->closed = true; + ++policy->delivery_generation; + policy->delivery_pending = false; policy->state->Invalidate(); { std::vector>> discarded; @@ -123,6 +152,8 @@ CloseProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp, UINT_PTR subclass, DWOR (message == WM_ENDSESSION && wp)) && !policy->required) { policy->required = true; + ++policy->delivery_generation; + policy->delivery_pending = false; if (message == WM_DESTROY) policy->closed = true; policy->state->NotifyRequired(); diff --git a/tests/window_close_windows_test.cpp b/tests/window_close_windows_test.cpp index 4f1b09c1..d549fe35 100644 --- a/tests/window_close_windows_test.cpp +++ b/tests/window_close_windows_test.cpp @@ -121,6 +121,55 @@ int main() { Drain(); Check(abandoned_callbacks == 0 && host_close == 2 && IsWindow(hwnd), "wrapper teardown fences queued foreign-thread setup and close"); + // Direct owner-thread Close() without listeners must defer WM_CLOSE to a + // later message-loop turn so callers (e.g. Dart FFI on merged UI thread) + // unwind before host teardown runs. + allow = false; + auto direct = std::make_unique(hwnd); + Check(direct->Close() && host_close == 2, + "direct owner-thread Close defers WM_CLOSE off the call stack"); + Check(direct->Close() && host_close == 2, + "repeated direct owner-thread Close coalesces while delivery is queued"); + Drain(); + Check(host_close == 3 && IsWindow(hwnd), + "deferred direct Close delivers WM_CLOSE once on the next message turn"); + // Owner-thread deferred approval must also unwind before host WM_CLOSE runs. + std::shared_ptr owner_decision; + auto owner_listener = + direct->AddListener([&](const auto& event) { + if (event.GetRequest()->IsCancelable()) + owner_decision = event.GetRequest()->Defer(); + }); + Check(direct->Close() && owner_decision && host_close == 3, + "owner-thread deferred request awaits approval"); + owner_decision->Accept(); + Check(host_close == 3, "owner-thread approval defers WM_CLOSE off the Accept stack"); + Drain(); + Check(host_close == 4 && IsWindow(hwnd), + "owner-thread approval delivers WM_CLOSE on the next message turn"); + direct->RemoveListener(owner_listener); + // Raw WM_CLOSE refused by the host invalidates an already-queued Close(). + Check(direct->Close() && host_close == 4, "queued Close before raw WM_CLOSE"); + SendMessageW(hwnd, WM_CLOSE, 0, 0); + Check(host_close == 5 && IsWindow(hwnd), "raw WM_CLOSE runs immediately and is refused"); + Drain(); + Check(host_close == 5 && IsWindow(hwnd), + "raw WM_CLOSE invalidates earlier queued Close delivery"); + // Destroying the window before Drain() fences the queued delivery cleanly. + Check(direct->Close() && host_close == 5, "queued Close before DestroyWindow"); DestroyWindow(hwnd); + Drain(); + Check(host_close == 5 && !IsWindow(hwnd), + "DestroyWindow before dispatch fences queued Close delivery"); + // Direct Close() with host DestroyWindow succeeds cleanly after Drain(). + hwnd = CreateWindowW(cls.lpszClassName, L"", WS_OVERLAPPEDWINDOW, 0, 0, 160, 100, nullptr, + nullptr, cls.hInstance, nullptr); + allow = true; + direct = std::make_unique(hwnd); + Check(direct->Close() && IsWindow(hwnd) && host_close == 5, + "direct Close does not destroy window synchronously on the caller stack"); + Drain(); + Check(!IsWindow(hwnd) && host_close == 6, + "direct Close destroys window cleanly on the next message turn"); return failures ? 1 : 0; }