fix(main): defer hide() in hideOnClose handler past windowShouldClose: - #22
Open
MaxLeiter wants to merge 1 commit into
Open
fix(main): defer hide() in hideOnClose handler past windowShouldClose:#22MaxLeiter wants to merge 1 commit into
MaxLeiter wants to merge 1 commit into
Conversation
The hideOnClose close handler does event.preventDefault() then browserWindow.hide() synchronously. On macOS, the red traffic-light button fires the JS close event from inside AppKit's windowShouldClose: delegate; calling hide() (orderOut:) synchronously within that callback trips app termination — the consumer's whole app quits, not just the pooled window. Cmd+W and BrowserWindow.close() route through NativeWindowMac::Close() (no delegate) and never hit this, but the one-tick defer is harmless there too, so it's not Darwin-gated. Reported and verified in anthropics/apps#43100. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
amorriscode
approved these changes
May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
On macOS, clicking the red traffic-light on a
hideOnClosepool window quits the consumer's entire app, not just the popout. Cmd+W on the same window works correctly.Reported and root-caused in anthropics/apps#43100 (closed unmerged in favor of fixing it here, per @alicelovescake).
Root cause
WindowInstance's close handler doesevent.preventDefault()thenbrowserWindow.hide()synchronously. The two close paths diverge at the native layer:-[NSWindow performClose:]→-windowShouldClose:delegate → emits JSclose. The handler'shide()→-[NSWindow orderOut:]runs while still insidewindowShouldClose:, which trips app termination.focused.close()→NativeWindowMac::Close()→ emits JSclosedirectly (no delegate). Same handler runs, no termination.Fix
Wrap the
hide()insetImmediate(...)sowindowShouldClose:returnsNObefore the window orders out. ThepreventDefault()andUserCloseRequestedevent stay synchronous so consumers see no API change. TheisDestroyed()guard re-evaluates inside the deferred callback to handle a force-close racing the deferred hide.The defer is unconditional (not Darwin-gated) — one event-loop tick is harmless on Windows/Linux and keeps the code path uniform.
Observable change
The renderer receives
UserCloseRequestedwhile the window is still visible for ~1 tick (previously it was already hidden). TheHiddenevent still fires when the deferredorderOut:runs, so anything that genuinely depends on visibility should be listening for that instead.Tests
Two new unit tests in
tests/unit/main-process.test.ts:hideOnClose: defers hide() past the close event, preventDefault stays synchronous— assertshide()is not called inside the close dispatch and is called on the next tick.hideOnClose: deferred hide() is a no-op if the window was destroyed in between— guards the race against adestroy()between the close event and the deferred hide.yarn test:unit: 174 passed.yarn typecheck,yarn lint,yarn format:check: clean.Verification
Verified working on macOS by @alicelovescake against an earlier iteration of the same defer in anthropics/apps#43100. The exact patch in that PR (against the built
dist/) is byte-equivalent to this change insrc/.Related
_forceClosing, app-quit drain). The two changes are orthogonal — fix(main): disarm hideOnClose veto on app quit so the drain completes #19 disarms the veto so the close proceeds on quit; this PR fixes the case where the veto is intentional. Should rebase cleanly in either order.🤖 Generated with Claude Code