Skip to content

fix(desktop): a quit can no longer hang on a fatal main-process error - #10396

Merged
waleedlatif1 merged 3 commits into
stagingfrom
fix/desktop-fatal-quit-hang
Nov 25, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
fix/desktop-fatal-quit-hang

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two bugs, surfaced by the desktop E2E harness's bounded quit ("Electron did not quit within 15s") on terminal-cancel.spec.ts, whose stall diagnostics showed a main_uncaught_exception, a failed load of the bundled dialog page, and the main thread parked in -[NSAlert runModal].

1. A fatal error during a quit hung the app forever. The fatal handler opened the "Sim encountered a problem" prompt even during shutdown. The bundled dialog page cannot load then (ERR_FAILED on sim-shell://pages/dialog.html), so it fell back to a parentless dialog.showMessageBox, which on macOS is a synchronous app-modal NSAlert that nothing answers.

  • A fatal error once the quit is committed (will-quit) now records the event and exits without prompting.
  • A quit request (before-quit) while the prompt is open answers it as Quit through the prompt's signal. A before-quit that another listener cancels (account teardown guard) leaves later failures their Restart prompt.

2. The exception itself. A CI experiment (draft #10394) that traced the error and window events over ~1,400 launches found it: TypeError: Object has been destroyed at BrowserWindow.visibilityChanged. When the app quits just as its first window appears, macOS delivers the window's show (an occlusion change) after the window has closed and been destroyed; Electron's own per-window visibilityChanged listener then calls isVisible() on the dead window. Every hit followed before-quit → will-quit → quit, with the show arriving after the window was destroyed. The fix wraps Electron's visibility listener (show, hide, minimize, maximize, restore) on every BrowserWindow, from browser-window-created (emitted right after Electron subscribes it), so it is skipped once the window is destroyed. A first version that only dropped the listeners on closed cut the rate from 7/960 to 2/960: some late shows arrive after the window is destroyed but before its closed runs.

Type of Change

  • Bug fix

Testing

  • observability.test.ts: a failure after will-quit exits without prompting; a failure after a cancelled before-quit still prompts; a quit request while the prompt is open answers it as Quit. Each fails with its guard reverted.
  • window.test.ts: on an Electron-wired window, visibility is still tracked while it lives, and a show delivered after it is destroyed no longer throws (fails with the isDestroyed guard reverted).
  • Desktop unit tests for both files, type-check, biome.
  • CI experiment experiment: trace desktop fatal main-process errors (do not merge) #10394: with bug 1 fixed, the late show still threw in 2/480 then 7/960 relaunch-and-quit loops, and every quit completed. With the close-time listener removal: 2/960. The rerun with the destroyed-window guard is in that PR.

Checklist

  • Code follows project style
  • Tests added that fail without the fix
  • No prod data

🤖 Generated with Claude Code

@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Nov 25, 2026 2:32am UTC

Request Review

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@greptile-apps

greptile-apps Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect desktop shutdown and window cleanup.

The remaining macOS window race and the shared-helper requirement should be addressed before merging.

Findings

  1. P1 Late window events still throw ▶
  2. P2 Shared test helper is duplicated ▶

Summary

The PR changes fatal-error recovery during quit and removes visibility listeners from closed windows.

  • Fatal errors no longer keep a quitting desktop app stuck.
  • Closed windows ignore late visibility events.

Reviews (3) · Last reviewed commit: "fix(desktop): only a committed quit skip..." · Reviewed by Greptile

Comment thread apps/desktop/src/main/observability.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/desktop/src/main/observability.ts Resolved
Comment thread apps/desktop/src/main/observability.ts Resolved
@waleedlatif1 waleedlatif1 changed the title fix(desktop): a fatal main-process error can no longer hang a quit fix(desktop): a quit can no longer hang on a fatal main-process error Nov 25, 2026
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit e4452fb into staging Nov 25, 2026
53 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/desktop-fatal-quit-hang branch November 25, 2026 02:42
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

Comment on lines +33 to +35
win.once('closed', () => {
for (const event of VISIBILITY_EVENTS) win.removeAllListeners(event)
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Late window events still throw

detachVisibilityListenersOnClose leaves Electron's listener active until closed. The PR description reports that some late show events arrive after destruction but before closed. In that order, visibilityChanged still calls isVisible() on a destroyed window and raises a fatal error during quit.

Guard the listener with isDestroyed() before it runs, and cover that event order in the test.

Comment on lines +104 to +106
async function settlePrompt() {
for (let turn = 0; turn < 5; turn++) await Promise.resolve()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Shared test helper is duplicated

settlePrompt() redefines the existing flushMicrotasks() helper. The testing guide requires using shared helpers instead of redefining them. Replace this function with flushMicrotasks(5) from @sim/testing/helpers/async, adding the workspace dependency if needed.

This repository requirement must be satisfied before merging.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

This branch was previously deployed

1 inactive deployment
Preview — fa41a2d6 Deployed Nov 25, 2026 by vercel[bot]
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.

1 participant