Quit terminates and waits for agent processes so Codex releases its writer lock

Closed
LA
2mo ago
+574-7

Fixes https://github.com/manaflow-ai/cmux/issues/12805

Quit never signalled or waited for the agents cmux spawned. They received only the pty-close SIGHUP as the app process died, and Codex treats SIGHUP as a graceful-only shutdown (codex-rs/app-server/src/lib.rs, shutdown_signal), so it could outlive cmux for seconds or minutes while holding the kernel flock on $CODEX_HOME/thread-writer-locks/<thread>.lock. The relaunched app then ran codex resume <id> against the held lock and Codex opened the thread read-only with "This conversation is open in another app / R to Retry".

Mechanism. deferTerminateForOwnedCleanupAndFreshSnapshot now also enters the terminate-later path when any panel has live recorded agent processes (cheap gate on the cached SharedLiveAgentIndex). Inside the cleanup task the order is: owned runtime cleanup, fresh index load, session snapshot save, then agent termination, then reply. The snapshot must be saved while the agents are alive so it records them as running and the relaunch resumes them. A new AgentQuitTerminationCoordinator terminates every panel's recorded agent processes through the hibernation teardown's existing validated signal path (terminateScopedProcessesForHibernation: exact PID generation, process group, controlling TTY, and cmux-scope checks before SIGTERM) and then waitForScopedProcessGenerationsToExitAfterEscalation (bounded wait, SIGKILL for survivors, bounded post-kill wait). No signal logic is duplicated.

Budget. SIGTERM, wait up to 2.5 s, SIGKILL, wait up to 1 s. Panels are terminated concurrently. The cleanup deadline grows to 5 s plus that budget when agents are present. A deadline that fires during the agent phase terminates without re-saving the snapshot (new .terminateWithSavedSnapshot disposition), because a re-save after the agents died would record them as not running.

Principled. cmux owns the exit of the processes it spawned, and the relaunch reads a snapshot taken while they were alive. The same exact-generation validation that hibernation uses guarantees nothing is signalled that cmux cannot prove is the recorded process. Residual risk: quit takes up to 3.5 s longer when an agent ignores SIGTERM, and an agent whose evidence fails validation is left to the pty-close SIGHUP as before. Half B (restore admission consults the Codex writer lock) is a separate PR.

Tests. cmuxTests/AgentQuitTerminationCoordinatorTests spawns a real agent stand-in on its own pty (controlling TTY plus cmux scope environment) that takes a flock and ignores SIGHUP. It asserts the lock is free after the coordinator returns, that a SIGTERM-ignoring agent is SIGKILLed within the budget, that a stale PID generation is never signalled, and the deadline disposition. The first commit adds the test alone and does not compile on main because the coordinator does not exist there; the second commit adds the fix.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Fixes #12805: Quit now terminates the agent processes cmux spawned and waits for them before replying, so Codex releases its thread writer lock and the relaunch can codex resume without opening the thread read-only.

  • The fresh session snapshot is saved before termination so the relaunch sees the agents as running and resumes them.
  • Agent termination reuses the hibernation teardown's validated signal path (exact PID generation, process group, controlling TTY, cmux scope) with SIGTERM, a 2.5s wait, SIGKILL for survivors, and a 1s post-kill wait; exit is re-checked against the exact signalled generations, since the shared wait can report a survivor that already exited on SIGTERM.
  • A deadline during agent termination terminates without re-saving the snapshot, because re-saving after the agents died would record them as not running.
  • Adds a debug-only quit socket verb that schedules a normal Cmd+Q quit on the next run-loop turn, so tests can drive the real quit path without hanging.
  • Restores the MobilePairingModel preparation deadline dropped by #12754 and fixes two cmuxTests compile errors, so the test bundle builds again (#12827).
  • Testing spawns a real agent stand-in on its own pty that takes a flock and ignores SIGHUP, asserting the lock is released, SIGKILL escalation within budget, stale generations are never signalled, and the deadline disposition.
  • Side effect: quit can take up to 3.5s longer when an agent ignores SIGTERM.

Summary by CodeRabbit

  • Bug Fixes

    • Improved app shutdown by reliably terminating app-launched agent processes.
    • Agents now have time to exit gracefully before remaining processes are forcefully stopped.
    • Added safeguards to avoid affecting agents with outdated or invalid process information.
    • Quit handling waits for agent cleanup within a bounded timeframe while preserving current session state.
    • Prevented shutdown cleanup from blocking the application’s quit response.
    • Improved mobile pairing by handling unavailable hosts and preparation timeouts more reliably.
  • Tests

    • Added coverage for agent termination and mobile pairing behavior.