Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions src/platform/windows/window_close_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ struct Policy : std::enable_shared_from_this<Policy> {
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<WindowId>(reinterpret_cast<uintptr_t>(GetPropW(hwnd, kId))) == id;
}
bool Post(std::function<void()> work) {
bool Post(std::function<void()> work, bool defer_on_owner_thread = false) {
if (!Valid())
return false;
if (GetCurrentThreadId() == thread) {
if (!defer_on_owner_thread && GetCurrentThreadId() == thread) {
work();
return true;
}
Expand Down Expand Up @@ -64,22 +66,47 @@ struct Policy : std::enable_shared_from_this<Policy> {
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<Policy> 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<Policy> 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);
});
}
};
Expand All @@ -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<std::pair<uint64_t, std::function<void()>>> discarded;
Expand Down Expand Up @@ -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();
Expand Down
49 changes: 49 additions & 0 deletions tests/window_close_windows_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nativeapi::Window>(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<nativeapi::EventDecision> owner_decision;
auto owner_listener =
direct->AddListener<nativeapi::WindowCloseRequestedEvent>([&](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<nativeapi::Window>(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;
}
Loading