Avoid nested quit confirmation modal loops

Merged
AZ
5mo ago
+332-81

Summary

Addresses the high-frequency quit-path Sentry hang signature:

https://manaflow.sentry.io/issues/7391555405/

That issue's representative events stop in NSAlert.runModal under AppDelegate.applicationShouldTerminate, which means the main thread is sitting in a nested AppKit modal loop during quit confirmation.

Changes

  • Replaces quit confirmation runModal() calls with a retained QuitConfirmationAlertPresenter that uses beginSheetModal(for:completionHandler:) when a main window is available.
  • Falls back to a modeless alert window instead of runModal() when no sheet host is available.
  • Keeps .terminateLater semantics and calls reply(toApplicationShouldTerminate:) from the async completion.
  • Moves full session snapshot/history flushing out of the pre-confirmation path so canceling quit does not do heavy termination persistence first.
  • Preserves suppression, cancel, confirmed quit, and remote tmux kill deferral behavior.

Tests

  • xcodebuild test -project cmux.xcodeproj -scheme cmux-unit -configuration Debug -destination 'platform=macOS' -derivedDataPath /tmp/cmux-sentryquit -only-testing:cmuxTests/QuitConfirmationModalLoopRegressionTests -only-testing:cmuxTests/QuitConfirmationAlertPresenterTests CODE_SIGNING_ALLOWED=NO

The first commit is test-only and fails against the old runModal() quit-confirmation paths; the second commit makes it pass.

Dogfood

Tagged build: quitcf

The tagged dev app builds and launches. The exact release/stable quit-confirmation dialog path cannot be fully UI-preflighted in a tagged dev build because existing policy intentionally skips quit confirmation for dev builds (QuitConfirmationStore.shouldShowConfirmation(... isDevBuild: true) == false). I verified the presenter behavior with the focused unit test and verified the tagged app launches and responds on its debug socket.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


Summary by cubic

Prevents UI hangs on quit by removing nested AppKit modal loops and unifying quit confirmation into a non-blocking presenter. Avoids duplicate dialogs and returns the right terminate reply for both Cmd+Q and app-initiated quits.

  • Bug Fixes

    • Added a retained QuitConfirmationAlertPresenter that uses beginSheetModal when a host window exists, or a modeless alert with direct button actions otherwise (no runModal()).
    • Handles concurrent quit flows: applicationShouldTerminate uses a pending-reply policy to return .terminateLater only when terminate triggered the dialog; otherwise .terminateCancel. Blocks re-entry while a presenter is active.
    • Defers session snapshot/history flush until after confirmation so cancel stays fast; keeps suppression, cancel/confirm behavior, and remote tmux kill deferral intact.
    • Adds tests for sheet and modeless paths (no nested modal loop) and the pending-terminate reply policy.
  • Refactors

    • Moved quit confirmation helpers out of AppDelegate into a dedicated presenter file and an AppDelegate extension for pending-terminate and dirty-workspace checks.

Summary by CodeRabbit

  • New Features
    • Improved quit-confirmation flow with more reliable dialog handling, including session snapshot/history preservation and “don’t warn again” suppression.
    • Added a dedicated quit confirmation presenter to show the warning consistently (sheet when possible, otherwise modal).
  • Bug Fixes
    • Fixed Cmd+Q quit-warning to prevent duplicate/stacked dialogs and correctly apply suppression; termination now defers when needed before cleanup work completes.
  • Tests
    • Added coverage for presenter presentation mode and the resulting termination/confirmation outcomes.