fix(desktop): let lifecycle shutdown finish before exiting #20303
main fromrefs/pull/20303/head·merged by juliusmarminge View on GitHubThe desktop runtime and lifecycle both handled SIGINT/SIGTERM, allowing the generic runtime to interrupt backend cleanup. DesktopLifecycle now owns those signals. On macOS/Linux, backend termination permits 30 seconds before force-kill; Windows retains immediate taskkill /T /F process-tree termination; ordinary quit and update installation wait up to 45 seconds per backend, concurrently, before continuing.
Retains @spencermorris04’s contribution in #20303, rebuilt against current main. The update-install path now receives the same cleanup budget because its updater-owned quit bypasses asynchronous before-quit cleanup. The lifecycle layer retains its signal listeners through program finalizers so repeated signals during cleanup cannot default-terminate the host. Electron event listeners keep their original program lifetime. Shutdown remains bounded; a backend still hung at the deadline can outlive that stop wait.
Verification: 71 focused tests pass across lifecycle, backend manager and updates; targeted lint/format and desktop typecheck pass. Consolidated 33 tests from three files into two existing files without increasing the count. Main’s runtime exits 130 in the real SIGTERM regression, and main abandons the backend wait after five seconds. This head completes READY/STOPPING/CLEANED with exit 0 and allows slow cleanup while bounding a hung backend wait at 45 seconds. The existing lifecycle test also proves signal ownership remains during acknowledged backend cleanup, repeated callbacks are deduplicated, and listeners release afterward; it fails against the first revision’s early listener removal. Additional real Node probes verify cleanup before successful, failed, interrupted and custom-teardown exits. Before/after proof and check output.
Shutdown policy: once quit, update installation or relaunch sets the quitting state, additional POSIX signals are deduplicated and do not cancel that operation. An in-flight update/relaunch may therefore still restart the app after a signal; if installation fails and recovery restores the running app, a new signal is needed to quit. Retaining that operation’s ownership avoids racing installer recovery with a second shutdown.
Three independent review rounds ended with no remaining code findings; the first round’s repeated-signal listener gap was fixed without adding tests.
Human approval and native Electron verification remain required. Linux verification is headless: real Node signals, lifecycle test doubles and TestClock, not a packaged Electron run. The local Electron binary is absent; an import-only override avoids its automatic download during tests. No screenshots apply to this nonvisual shutdown change. Before merge, verify packaged macOS/Windows quit, relaunch, update installation and failed-install recovery, multiple backends/WSL, and provider cleanup taking longer than five seconds or ignoring termination. iOS/Android and remote clients were not run; their protocols are unchanged, and clients disconnect when their desktop-hosted server quits.
Implemented with Codex gpt-6.1-sol; tests with Codex gpt-6-astra; adversarial review through Claude claude-opus-5-5 and Codex gpt-6-sol in T3 Code.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
Warning
Review limit reached#
Only developers with an assigned seat can use this organization's usage-based review budget, and seats here are assigned manually. Ask an admin to assign a seat, or change the review continuation mode in Billing.
Next included review available in 13 minutes.
View limit details
-
Limit details: You’ve used all 10 included reviews currently available.
Review configuration:
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Advanced
- Run ID:
869da5b7-aebd-4caf-bb40-e3db7d207d09
📥 Commits
-
Reviewing files that changed from the base of the PR and between 902293a7167667ada4e566ccfb92595e9704efc5 and 274fdb51328d726185f26863fbcd18832afe287e.
📒 Files selected for processing: 8
-
apps/desktop/src/app/DesktopApp.tsapps/desktop/src/app/DesktopLifecycle.test.tsapps/desktop/src/app/DesktopLifecycle.tsapps/desktop/src/app/DesktopRuntime.tsapps/desktop/src/backend/DesktopBackendManager.test.tsapps/desktop/src/backend/DesktopBackendManager.tsapps/desktop/src/main.tsapps/desktop/src/updates/DesktopUpdates.ts
-
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Advanced
- Run ID:
bbf04cde-4202-4ce9-88d1-c4c7a4c4ef1f
📥 Commits
-
Reviewing files that changed from the base of the PR and between cfafb4bb54d244e4612f5681aad3832ea64cb0fb and 902293a7167667ada4e566ccfb92595e9704efc5.
📒 Files selected for processing: 6
-
apps/desktop/src/app/DesktopApp.tsapps/desktop/src/app/DesktopRuntime.test.tsapps/desktop/src/app/DesktopRuntime.tsapps/desktop/src/backend/DesktopBackendManager.test.tsapps/desktop/src/backend/DesktopBackendManager.tsapps/desktop/src/main.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
-
📝 Walkthrough
-
The desktop entry point now uses a custom
runMainthat invokes teardown after the main fiber exits. The backend termination grace period increases from 5 to 30 seconds, and the pool-instance stop timeout increases from 5 to 45 seconds. Tests cover SIGTERM cleanup and the updated shutdown timing.Changes: Desktop Shutdown Lifecycle
Layer / File(s) Summary Runtime entry point and teardown
apps/desktop/src/app/DesktopRuntime.ts,apps/desktop/src/main.ts,apps/desktop/src/app/DesktopRuntime.test.tsDesktop startup now runs through DesktopRuntime.runMain. It observes main-fiber exit and invokes teardown. A subprocess test checks SIGTERM cleanup, output order, and exit status.Backend shutdown timeouts
apps/desktop/src/backend/DesktopBackendManager.ts,apps/desktop/src/app/DesktopApp.ts,apps/desktop/src/backend/DesktopBackendManager.test.tsThe backend termination grace period changes to 30 seconds, and the pool-instance stop timeout changes to 45 seconds. Tests check the configured grace period and staged shutdown timing. Priority: ⬇️ Low
Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Bug fix
Sequence Diagram(s)#
sequenceDiagram participant DesktopMain as main.ts participant DesktopRuntime as DesktopRuntime.runMain participant MainFiber participant Teardown DesktopMain->>DesktopRuntime: Run DesktopApp.program DesktopRuntime->>MainFiber: Observe exit MainFiber-->>DesktopRuntime: Exit DesktopRuntime->>Teardown: Invoke teardownSuggested reviewers:
juliusmarminge,t3dotgg,ishaanko
Merge Risk: ⚪ Minimal · up to
90229-
No actionable merge-blocking issue is established. The desktop shutdown change is ready for normal checks before merging.
Security architecture risk: 🔵 Low · up to
90229-
The change centralizes desktop shutdown and retains forced backend termination without visibly adding remote access or privileges. Remaining uncertainty concerns abnormal exits, concurrent cleanup, and native platform shutdown behavior.
Retained concerns No architecture-level concerns identified.
Security review details
-
Security Blast Radius
- inferred — The directly affected scope is one desktop process and its active backend pool, including registered secondary instances. Shutdown now permits a longer interval before forced termination; the inspected changes do not expand access to another tenant, service, or credential authority.
Trust Boundaries and Controls
- observed — Stopping an instance sets desiredRunning and ready to false under a mutex, marks the active run as stopping, and interrupts its scheduled restart. Cleanup checks run identity before clearing active state, preserving the existing defense against stale completion affecting a newer run.
Resilience and Maintainability Implications
- observed — Secondary instance scopes remain attached to the pool layer scope, and each instance registers a stop finalizer. Pool enumeration nevertheless snapshots only active entries. These existing ownership mechanisms constrain cleanup loss, but do not establish end-to-end behavior for registration races, repeated stops, or interruption during closure.
- observed — The new runtime regression launches a real Node subprocess, sends SIGTERM after readiness, explicitly releases cleanup, and asserts READY, STOPPING, CLEANED followed by an unsignaled zero exit. It does not exercise native Electron quitting or an independently failed main fiber.
-
Pre-merge checks:
3 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Description check The description provides detailed problem, change, scope, verification, limitations, and follow-up checks. It does not provide explicit maintainer approval evidence or explain why the change qualifies… Add the required Scope and approval information. Link the triaged issue or discussion and identify the explicit maintainer approval comment, or explain why this focused fix qualifies for the approval exemption. Passed checks: 3 passed-
Check name Status Explanation Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Title check The title clearly and concisely describes the primary change: allowing desktop lifecycle shutdown to complete before process exit.
Full details: Description check
-
Explanation
The description provides detailed problem, change, scope, verification, limitations, and follow-up checks. It does not provide explicit maintainer approval evidence or explain why the change qualifies for an approval exemption under the required Scope and approval section.
Resolution
Add the required Scope and approval information. Link the triaged issue or discussion and identify the explicit maintainer approval comment, or explain why this focused fix qualifies for the approval exemption.
-
✨ Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
juliusmarminge assignedjuliusmarminge
Taking over this one as maintainer for batch desktop-app-04. I will preserve the original commits, merge current main, reduce tests to the shutdown regressions, and run separate test, implementation, and adversarial review passes. Native desktop verification and human approval will remain explicit gates; this stays unmerged.
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
juliusmarminge merged this4de822c
juliusmarminge closed this
Reverting this in #22222 together with the other desktop fixes the takeover campaign merged yesterday. Nothing was wrong with the review of this change; it was merged on CI and code audits only, and after #20907 from the same batch broke existing installs the maintainer decided that desktop changes do not ship without being run on a real desktop build. To bring it back: open a fresh PR from the original commit, run it on a real build that already has user state on each platform it touches, and put the before/after proof in the body.
Sign in to join this conversation.
Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — This production change replaces the desktop process runner and extends backend termination windows so lifecycle cleanup can complete before exit. It also changes termination defaults and adds a static-analysis suppression in the new runtime test, warranting human review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).