Repository navigation
Terminate the old instance from the run loop so a .terminateLater delegate can reply - #30
Conversation
…gate can reply installAndRelaunch() called NSApp.terminate from inside its main-actor job. A delegate that returns .terminateLater and replies from a Task or DispatchQueue.main then never replies: AppKit waits in a nested loop that cannot drain the main queue while that job holds it. The old instance stayed alive running the removed backup, and the fallback relauncher gave up after 30 seconds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The termination callback can deadlock a .terminateLater reply when a main-actor caller enters a synchronous modal loop, and the Boolean-only test does not verify that an asynchronous reply can run.
1 open issue
What changed in this PR
This PR moves AppUpdater’s termination request to the main run loop to address relaunch hangs when an app delegate returns .terminateLater and replies asynchronously.
Changes:
- Routes live installation termination through a new run-loop helper.
- Adds a regression test for deferred termination and main-queue behavior.
- Documents deferred termination and its effect on callers.
| File | Description |
|---|---|
| Tests/AppUpdaterTests/AppUpdaterTests.swift | Adds a termination scheduling test and modal-loop helper. |
| README.md | Explains deferred termination and .terminateLater support. |
| AppUpdater.swift | Adds ApplicationTerminator and connects it to the installation driver. |
Review effort: Balanced
| RunLoop.main.perform(inModes: [.default, .modalPanel, .eventTracking]) { | ||
| MainActor.assumeIsolated { terminate() } | ||
| } |
There was a problem hiding this comment.
Fixed in 992ce02: the run-loop block is registered from the main queue, so it waits for the calling job, and the test now checks that a queued reply runs inside the termination's nested loop.
— Sora (空), the Claude agent working with kageroumado
A run-loop block also fires inside a nested loop the caller starts after installAndRelaunch() returns, such as a modal alert, while its job still holds the main queue. Registering the block from the main queue defers it until that job has returned. The test now checks that a reply queued on the main queue runs inside the termination's nested loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mxcl
left a comment
There was a problem hiding this comment.
Found one remaining deadlock case in the termination scheduling. All 85 tests pass, including the new regression test, but the intervening-main-queue-job ordering described inline reproduces locally. No standards findings.
| DispatchQueue.main.async { | ||
| // The main queue has returned to the run loop by the time this | ||
| // block runs, so the delegate's reply can use it. The run loop | ||
| // runs blocks on the main thread. |
There was a problem hiding this comment.
[P2] Termination can still deadlock inside another main-queue job
The extra dispatch waits for the original caller to return, but another queued main-queue callback can open a modal loop after the run-loop block is registered. Because this block also runs in .modalPanel, termination then executes inside that callback, and a .terminateLater delegate's asynchronous main-queue reply remains blocked.
I reproduced this locally by queuing the registration block, then queuing a second callback that spins .modalPanel. Inside the termination callback, a queued reply could not execute during a bounded nested loop; removing the second callback allowed it to execute.
Please extend the regression test with this intervening main-queue callback and ensure termination executes outside a main-queue job in that ordering.
There was a problem hiding this comment.
Right, the hop only cleared the calling job. a7c59ba checks instead of waiting: the block queues a probe on the main queue and runs one zero-timeout pass; if the probe did not run, a job is on the stack, so it re-arms after that job. The test now queues your second job behind the call.
— Sora (空), the Claude agent working with kageroumado
A run-loop block also fires inside a nested loop that a later main-queue job runs, such as a modal alert queued behind the install. The block now checks that the main queue is free, by queuing a probe and running one zero-timeout pass of the loop, and otherwise tries again after the current job. The test queues such a job behind the call. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

installAndRelaunch()callsNSApp.terminateinside a main-actor job. If the app delegate returns.terminateLaterand replies from aTask, the reply never runs: the old process keeps running from the deleted backup and never relaunches (kageroumado/adrafinil#36).This calls
terminatefrom the run loop instead.installAndRelaunch()now returns before the app quits.Tested with a signed app that updates itself through the public API:
masterstays stuck, this branch relaunches into the new version.— Sora (空), the Claude agent working with kageroumado
🤖 Generated with Claude Code