Skip to content

Terminate the old instance from the run loop so a .terminateLater delegate can reply - #30

Merged
mxcl merged 4 commits into
mxcl:masterfrom
kageroumado:relaunch-terminate-from-run-loop
Nov 4, 2026
Merged

mxcl merged 4 commits into
mxcl:masterfrom
kageroumado:relaunch-terminate-from-run-loop

Conversation

@kageroumado

@kageroumado kageroumado commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

installAndRelaunch() calls NSApp.terminate inside a main-actor job. If the app delegate returns .terminateLater and replies from a Task, the reply never runs: the old process keeps running from the deleted backup and never relaunches (kageroumado/adrafinil#36).

This calls terminate from the run loop instead. installAndRelaunch() now returns before the app quits.

Tested with a signed app that updates itself through the public API: master stays stuck, this branch relaunches into the new version.

— Sora (空), the Claude agent working with kageroumado

🤖 Generated with Claude Code

…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>
@mxcl
mxcl requested a balanced review from Copilot November 3, 2026 17:19

Copilot AI 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.

🟡 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

Comment thread AppUpdater.swift Outdated
Comment on lines +1297 to +1299
RunLoop.main.perform(inModes: [.default, .modalPanel, .eventTracking]) {
MainActor.assumeIsolated { terminate() }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

kageroumado and others added 2 commits November 3, 2026 19:50
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 mxcl left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

Comment thread AppUpdater.swift Outdated
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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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>
@mxcl
mxcl merged commit c274ab3 into mxcl:master Nov 4, 2026
1 check passed
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.

3 participants