codex icon indicating copy to clipboard operation
codex copied to clipboard

app-server daemon: auto-update force-kills active turns after a 60s drain budget, and cannot be disabled

Open yazzaoui opened this issue 3 months ago • 7 comments

app-server daemon: auto-update force-kills active turns after a 60s drain budget, and cannot be disabled

Summary

On a long-lived codex remote-control daemon, the auto-updater restarts the app-server as soon as a new release lands. The app-server implements a correct graceful drain that waits for running assistant turns — but the daemon only grants it 60 seconds before sending SIGKILL. For any workload where a turn routinely lasts minutes (agents running build/test suites), the drain can essentially never complete, so every auto-update destroys in-flight work across every thread on the host.

There is also no supported way to turn auto-update off, so an operator cannot opt out of the behavior.

Environment

  • codex-cli / app-server 0.149.0 → 0.150.0 (standalone managed install), Debian 13 x86_64
  • daemon started via codex remote-control start; multiple TUI clients attached over an SSH-forwarded unix socket
  • workload: 13 long-lived threads; typical turn runs a Go build/test suite lasting 3–30 minutes

What happened

2026-08-26, unattended:

  • 20:16 — updater installed 0.150.0 (symlink ~/.codex/packages/standalone/current re-pointed).
  • 20:16 → 20:17:51 — app-server restarted; every attached client connection dropped simultaneously; turns that were mid-execution were terminated. The ~90s gap matches the grace period elapsing before the forced kill.
  • Recovery required reconnecting each client and interrupting/continuing each thread; turns that were awaiting an exec at kill time exhibited the hang described in #40306.

Mechanism (source refs, rust-v0.150.0)

The graceful path exists and is well-implemented:

  • app-server/src/lib.rs:250-253 — on SIGTERM the server logs "received shutdown signal; entering graceful restart drain (connections=…, runningAssistantTurns=…, requests still accepted until no assistant turns are running)".
  • app-server/src/lib.rs:257-262 — update() returns ShutdownAction::Finish only when self.forced || running_turn_count == 0, and otherwise logs "waiting for N running assistant turn(s) to finish".

The daemon then overrides it:

  • app-server-daemon/src/backend/pid.rs:520 — stop() sends SIGTERM.
  • app-server-daemon/src/backend/pid.rs:23-24 — STOP_GRACE_PERIOD = 60s, STOP_TIMEOUT = 70s.
  • app-server-daemon/src/backend/pid.rs:253-277 — after STOP_GRACE_PERIOD elapses, force_terminate_process(pid) → SIGKILL (pid.rs:535).

Restart eligibility is version-only and has no turn awareness:

  • app-server-daemon/src/lib.rs:804-818 — restart_decision compares info.app_server_version against the managed version.
  • app-server-daemon/src/lib.rs:363-372 — the RestartIfRunningOutcome::Busy branch reflects the daemon operation lock, not running turns.

Auto-update cannot be disabled:

  • app-server-daemon/src/settings.rs:11-13 — DaemonSettings has exactly one field, remote_control_enabled; settings.json cannot express an update preference.
  • app-server-daemon/src/lib.rs:608-612 — bootstrap unconditionally starts pid_update_loop_backend; there is no gate.
  • app-server-daemon/src/lib.rs:619 — auto_update_enabled: true in the status payload is a hardcoded literal, not a setting, which makes the reported field misleading.

No CLI flag or environment variable appears to control it. The only working workaround is to kill the codex app-server daemon pid-update-loop process after every daemon start (it does not respawn on its own, and killing it does not disturb the running app-server), or to bypass codex app-server daemon entirely and supervise codex app-server directly.

Requests

  1. Make the drain budget configurable, and allow it to be unbounded. A server operator should be able to say "never SIGKILL an app-server that is still draining turns" (or set a budget in the tens of minutes). The 60s default is sensible for an interactive desktop session and unusable for turns that run build/test suites. The forced path should remain available explicitly (e.g. a second signal / --force), as on_signal already models.
  2. Provide a supported way to disable auto-update — a DaemonSettings field plus codex app-server daemon disable-auto-update (mirroring the existing enable-remote-control / disable-remote-control pair) would fit the current surface. Operators running unattended fleets need updates to be a deliberate, scheduled action.
  3. Optionally: defer rather than kill. Since the updater already loops with RESTART_RETRY_INTERVAL on Busy, treating "turns are running" as a retryable condition — rather than starting a drain that will be killed — would make auto-update safe by default without any configuration.

Also worth fixing regardless: autoUpdateEnabled in the status output currently reports a constant, so tooling cannot detect the actual state.

Related

  • #40306 — turns awaiting an exec hang indefinitely when their client connection dies; the forced kill in this issue is one way to trigger that state.

yazzaoui avatar Aug 26 '26 20:08 yazzaoui

Verified your source refs against current main (37a5149822, 2026-08-27) — they all hold. Updated line numbers (your rust-v0.150.0 refs → main):

Your ref (v0.150.0) Current main Symbol
pid.rs:23-24 pid.rs:23-24 STOP_GRACE_PERIOD = 60s, STOP_TIMEOUT = 70s
pid.rs:520 pid.rs:240 PidBackend::stop() — SIGTERM via terminate_process (pid.rs:517)
pid.rs:253-277 pid.rs:255, 274 grace deadline + force_terminate_process → SIGKILL (pid.rs:532)
lib.rs:804-818 lib.rs:804 restart_decision — version-only comparison, no turn awareness
lib.rs:363-372 lib.rs:363 try_restart_if_running — Busy = operation lock only
lib.rs:608-612, 619 lib.rs:587, 619 bootstrap always starts the update loop; auto_update_enabled: true literal (unchanged)
app-server/lib.rs:250-262 app-server/lib.rs:235, 251, 256, 276 ShutdownState::on_signal / update() drain

Two additions to the mechanism picture, both relevant to your request 3 (defer rather than kill):

  1. The daemon currently has no running-turn visibility. ProbeInfo (client.rs:38-41) carries only app_server_version — the restart decision literally cannot see turns. "Turns running → defer" therefore needs a small additive protocol change: expose the running-turn count over the control socket. The good news is the app-server already maintains exactly this number for the drain — ThreadWatchManager::running_turn_count_tx (thread_status.rs:22, 235) → subscribe_running_assistant_turn_count (thread_processor.rs:3514, message_processor.rs:819) → consumed at app-server/src/lib.rs:918. Extending the probe (or adding a status request) to surface it is straightforward.
  2. The retry cadence in the update loop can't support a minutes-long defer. update_once (update_loop.rs:109-135) installs the new standalone before the restart decision and only re-loops with RESTART_RETRY_INTERVAL = 50ms on Busy. A turn-drain defer needs its own longer interval (e.g. 30–60s) so the daemon doesn't spin at 20 Hz against the control socket for the whole drain.

Design notes:

  • Unbounded drain is safe only because of the second-signal force path. ShutdownState::on_signal sets forced on a second SIGTERM and update() returns ShutdownAction::Finish immediately (app-server/src/lib.rs:235-245, 261-271). So "no automatic SIGKILL" should still keep an explicit force — a second SIGTERM (not SIGKILL) for the operator, with the daemon's STOP_TIMEOUT as a final fallback. This matters because of the #40306 hang class (a turn awaiting exec with a dead client never completes): an unbounded drain on such a turn would otherwise wedge the daemon forever.
  • A configurable grace affects user-initiated stop too — Daemon::stop and the update path share PidBackend::stop(). Keeping the default at 60/70s leaves behavior unchanged unless the operator opts in.

Implementation gotcha for your request 2: DaemonSettings (settings.rs:9-12) derives Deserialize without #[serde(default)], so adding a non-Option field would break loading of existing settings.json files (missing-field error). New fields (auto_update_enabled, grace/timeout) need #[serde(default)] or Option for backward compatibility — and the same applies to the stop_grace_period/stop_timeout fields in request 1.

Happy to implement this. The smallest high-value slice (mirroring requests 1+2, both low-risk and fully testable in the daemon crate):

  • configurable stop_grace_period_secs / stop_timeout_secs in DaemonSettings (null = never auto-SIGKILL), plumbed into PidBackend, default 60/70 unchanged;
  • auto_update_enabled setting + enable-auto-update / disable-auto-update mirroring the existing remote-control pair, gating the update loop at bootstrap and replacing the hardcoded autoUpdateEnabled status literal.

Defer-on-by-default (your request 3) is the nicer long-term behavior but needs the probe extension above; happy to take that on as a follow-up once maintainers confirm the intended default semantics.

argszero avatar Aug 26 '26 22:08 argszero

I've prepared an implementation for requests 1 + 2 (configurable/unbounded drain budget + auto-update toggle). Design summary:

Settings (settings.json, daemon-managed)

field meaning
stopGracePeriodSecs seconds after SIGTERM before force-terminating (SIGKILL). Absent → 60 (unchanged); 0 → unbounded (never auto-SIGKILL while draining); n → n seconds
stopTimeoutSecs total stop budget before giving up. Absent → 70 (unchanged); 0 → wait indefinitely
autoUpdateEnabled false → update loop not started at bootstrap; running loop stopped by the toggle

0 = "no limit" follows the systemd TimeoutStopSec=0 convention. New fields are optional with skip_serializing_if — existing settings.json files keep working unchanged, and the default behavior (60/70, auto-update on) is bit-for-bit today's.

Design decisions worth confirming

  1. Unbounded drain stays interruptible: the app-server's existing second-SIGTERM force path (ShutdownState::forced → immediate ShutdownAction::Finish) is preserved, so an operator can still force-exit an unbounded drain explicitly. This matters because of the #40306 hang class — a turn awaiting exec with a dead client never completes on its own, so a truly unkillable drain would wedge the daemon.
  2. disable-auto-update stops the running update loop immediately (mirrors your workaround of killing the pid-update-loop process, but as a supported command pair enable-auto-update / disable-auto-update).
  3. No re-bootstrap resurrection: is_bootstrapped is extended to also treat a running app-server backend as bootstrapped, so codex app-server daemon start after disabling auto-update won't re-bootstrap and silently restart the update loop.
  4. Configurable grace applies to user-initiated stop too (both paths share PidBackend::stop()), default unchanged.

Scope

Touch: DaemonSettings (settings.rs), PidBackend::stop() policy (backend/pid.rs), BackendPaths plumbing (backend/mod.rs), bootstrap gating + set_auto_update (lib.rs), CLI subcommand pair (cli/main.rs). Request 3 (defer rather than kill by default) is intentionally out of this slice — it needs the probe protocol change from my earlier comment and is a natural follow-up.

I can push this as a PR — I have the patch ready. (Note: my fork push has been intermittently blocked by a local sandbox restriction; will land as soon as it clears.)

argszero avatar Aug 27 '26 00:08 argszero

Implementation update — requests 1 + 2 are now built and tested on a fork branch (no PR yet; still waiting on the invitation gate for PR creation).

Branch: argszero/codex:fix/app-server-daemon-stop-policy-and-auto-update-toggle @ 293a744e9a

What's in it:

Configurable stop policy (settings.json, absent = current behavior, 0 = unbounded):

{
  "remoteControlEnabled": false,
  "stopGracePeriodSecs": 0,
  "stopTimeoutSecs": 0,
  "autoUpdateEnabled": false
}
  • stopGracePeriodSecs — grace after SIGTERM before SIGKILL; default 60, 0 = never auto-SIGKILL while draining. Second-signal force is preserved, so an operator can still interrupt an unbounded drain.
  • stopTimeoutSecs — total stop budget; default 70, 0 = wait indefinitely.
  • autoUpdateEnabled — default true. Disables the update loop at bootstrap; codex app-server daemon disable-auto-update / enable-auto-update stop/start a running loop and persist the setting.

Key semantics

  • bootstrap_locked loads existing settings instead of replacing them → a disabled auto-update survives re-bootstrap (this was the trap in the current updater.start() unconditional path).
  • is_bootstrapped treats a running app-server backend as bootstrapped → ensure_remote_control_started won't re-bootstrap and resurrect the update loop.
  • The update-loop process always uses the default stop policy (no turns to drain) so an unbounded drain setting can never wedge disable-auto-update.
  • PidBackend::new/new_update_loop signatures unchanged (zero test churn); only with_stop_policy is added.

Tests: cargo test -p codex-app-server-daemon 36 passed; cargo test -p codex-cli --bin codex 279 passed (new parse tests included); clippy + fmt clean.

Request 3 ("defer rather than kill") still needs the probe-protocol change (expose running-turn count in the daemon's probe) so the daemon can choose to defer restart — separate follow-up.

argszero avatar Aug 28 '26 03:08 argszero

Design for request 3 — "defer rather than kill" on auto-update (follow-up to my P1 implementation comment)

Key facts that shape the design

  1. The app-server's graceful drain has no internal timeout: on first SIGTERM it enters graceful-restart drain and waits indefinitely for running turns (ShutdownState::update → ShutdownAction::Noop until running_turn_count == 0; only a second signal forces). So with P1's stopGracePeriodSecs: 0, auto-update already stops killing — the app-server drains on the old binary, exits on its own when idle, and the daemon then restarts with the new binary.
  2. What request 3 still needs on top of P1:
    • Visibility: the daemon silently waits while turns drain — no signal that an update is pending.
    • Bounded defer: a stuck turn (the #40306 hang class — turn awaiting exec with a dead client never completes) would wedge the update forever with unbounded drain. Operators need "wait for idle, but not forever", with a fallback to the configured stop policy.

Proposed design

1. Probe protocol — expose the running-turn count

  • Add an optional field to InitializeResponse: #[serde(default, skip_serializing_if = "Option::is_none")] running_turn_count: Option<usize> (camelCase: runningTurnCount).
  • Plumbing is cheap: the app-server already publishes this via message_processor::subscribe_running_assistant_turn_count() → watch::Receiver<usize>. Pass a cloned receiver into InitializeRequestProcessor (it derives Clone, and watch::Receiver::borrow() gives the latest value per request).
  • Backward compatible: older app-servers omit the field → the daemon treats it as "unknown".

2. Daemon side — bounded defer on restart

  • ProbeInfo gains running_turn_count: Option<usize>; probe() parses it.
  • New optional setting deferRestartTimeoutSecs (absent → 0 → no defer, current P1 behavior):
    • > 0 and auto-update restart is pending with runningTurnCount > 0 → poll the probe every ~2–5 s until the count reaches 0 or the deadline; log a warn! per poll ("auto-update deferred: waiting for N running assistant turn(s)") so the pending update is visible.
    • On deadline: warn!("deferred restart timed out with N running assistant turn(s); proceeding with configured stop policy") and fall through to the normal graceful stop (SIGTERM → drain → stopGracePeriodSecs → SIGKILL).

3. Escape hatches compose, not conflict

  • deferRestartTimeoutSecs only delays the start of the graceful stop; it is orthogonal to stopGracePeriodSecs / stopTimeoutSecs.
  • Stuck-turn recovery stays as-is: second SIGTERM forces the app-server to exit; stopGracePeriodSecs: 0 still means "never auto-SIGKILL".

Why this shape

  • One optional protocol field, no new JSON-RPC method (the probe already does initialize).
  • Old binaries + default settings → behavior identical to today (and to P1).
  • The defer window is bounded by default opt-in, avoiding the unbounded-wedge regression while still giving long-turn operators the "finish on the old binary" behavior they asked for.

I can implement this on the same branch pattern as P1 (fork branch + tests) once it looks reasonable — happy to adjust the knob shape or naming if you'd prefer waitForIdleTimeoutSecs or similar.

argszero avatar Aug 28 '26 05:08 argszero

Reproduced again on macOS with the current standalone release transition 0.152.0 -> 0.152.1. This affected every active CLI/TUI client attached to the shared remote-control app-server on the host.

Environment

  • macOS 26.6.2 (25G83), arm64
  • Standalone managed Codex CLI/app-server: 0.152.0 -> 0.152.1
  • Shared socket: unix:///<home>/.codex/app-server-control/app-server-control.sock
  • Managed processes included codex app-server daemon pid-update-loop and codex app-server --remote-control --listen unix://

Observed timeline (2026-09-01, CST / UTC-6)

  • 17:07:13 - the 0.152.1 standalone release was staged locally.
  • 17:08:04 - last observed log activity from the 0.152.0 app-server (PID 51151).
  • 17:08:13.945 - macOS unified logs recorded the old app-server exiting.
  • 17:08:14.065 - replacement app-server PID 7055 started as 0.152.1; its PID file and socket were recreated at 17:08:14.
  • 17:08:16 - the replacement briefly received 409 Remote app server already online.
  • 17:08:17 - the replacement connected successfully.

At the replacement boundary, multiple clients failed simultaneously with:

remote app server at `unix:///<home>/.codex/app-server-control/app-server-control.sock`
transport failed: WebSocket protocol error: Connection reset without closing handshake
Disconnected from this task. Work may still be running.

One interrupted session had been running for approximately 4h45m. This was not caused by an operator-initiated update: codex update was only run later, around 17:25, after the disconnects had already occurred. The pid-update-loop supervisor had remained alive since Aug 30 while its app-server child was replaced.

Current mitigation gap

check_for_update_on_startup = false can disable the ordinary client startup check, but it is not a daemon auto-update control. On 0.152.1, codex app-server daemon --help still exposes no disable-auto-update command or daemon drain policy. Therefore users cannot reliably prevent this host-wide interruption through the supported daemon interface.

Required behavior

A device-wide shared daemon must not be replaced while active clients or turns depend on it. The updater should do at least one of the following:

  1. Defer installation/restart until no active turns remain.
  2. Run old and new daemons side-by-side, allowing existing connections to drain on the old version.
  3. Transparently reconnect clients and preserve active turn/event-stream state.
  4. Require explicit operator confirmation before force-terminating active work.

The safe default should be defer, never force-kill active turns for an unattended update. A bounded force path may exist for explicit operator actions, but it should not be the automatic update policy. Resumable task metadata is not an adequate substitute for uninterrupted execution and session continuity.

This also corroborates #23954's exact Unix-socket WebSocket-reset symptom, but with a version transition and process timestamps that identify the managed update/restart path directly.

MagnetonIO avatar Sep 01 '26 23:09 MagnetonIO

Reproduced on Linux, 12 November 2026. Installed CLI: 0.167.0; managed server updated to 0.167.2 during an active interactive conversation.

Updater log, UTC:

08:45:37.558 package_selected: 0.167.2, trigger=scheduled
08:45:37.714 restart_requested: shutdownGraceSeconds=60
08:46:37.752 shutdown_forced
08:46:37.888 replacement spawned
08:46:38.219 readiness succeeded

The conversation was interrupted and subsequently resumed. No update was requested during the session.

Please provide a persistent manual-only update setting and prevent scheduled updates from forcibly terminating active turns.

Tregaron1 avatar Nov 12 '26 09:11 Tregaron1

This issue covers one side of the problem: a fixed 60 s budget kills active turns. The other side shows up when nothing kills the drain, and both point to the same missing feature: suspend running turns when a drain starts and resume them after the update, under a policy the user controls.

Today a restart for an update handles running turns in one of two ways, and neither fits long agent work:

  • The managed daemon allows a 60 s drain, then force-kills active turns (this issue, #56474). In-flight work is lost.
  • A server run without that budget (for example codex app-server --remote-control in a supervising terminal, drained by a console Ctrl+C or SIGHUP) waits for every running turn to finish. Meanwhile it refuses new connections and turn/steer, so nobody can reach the threads that are holding it up.

Observed 2026-11-18 (0.171.0-alpha.11 → alpha.13, Windows): one turn had been started from the phone and was running a multi-stage installer acceptance test. It held the drain for 53 minutes. During that time:

  • every other TUI got turn/start failed: Server is draining; retry after reconnecting (code -32600) {"reason":"serverShuttingDown"};
  • the phone app showed "can't load task right now" for that thread;
  • the turn had asked the user a question (request_user_input_async), which no client could display;
  • turn/steer asking the agent to reach a checkpoint was refused with the same draining error.

The only lever left was turn/interrupt. It ended the turn but killed the tool process mid-stage, which left the test's sandbox VM running without cleanup.

Proposal: a native suspend/resume path for server restarts, with a policy the user controls.

  1. Suspend at drain start. The server tells each running turn that a restart is pending, as a system message on the turn, and gives it a bounded window to reach a checkpoint: finish or record the current tool call, then end the turn. A turn waiting on user input is suspended immediately, and its pending question is persisted with the thread.
  2. Resume after the update. The new server lists suspended threads to every client that connects. Depending on policy, it either resumes them with a "resumed after server update" turn or leaves them for the user, and it re-surfaces pending questions in both cases.
  3. Keep control reachable while draining. Refuse new turns, but keep accepting connections for thread/read, answering pending questions, turn/steer and turn/interrupt. Then the phone and TUIs can still see and steer the threads the drain waits on.
  4. Configuration, for example:
[app_server.restart]
# what happens to running turns when the server restarts for an update
on_running_turns = "suspend"    # suspend | wait | interrupt
checkpoint_window = "10m"       # suspend: time a turn gets to reach a checkpoint before it is suspended anyway
resume = "ask"                  # ask | auto: what the new server does with suspended threads

plus a per-thread override, settable from the TUI or the app (e.g. /restart-policy wait or a thread setting), for threads that must not be interrupted. Example: a long migration or install that must run to completion. An update then waits for that thread while the others suspend.

This covers both preferences: users who want sessions to finish cleanly first can choose wait, and users who want updates to land promptly can choose suspend. It also replaces the hard-coded budget this issue describes.

Related: #56474 (update forces active-turn shutdown, TUIs stranded).

ilepn avatar Nov 18 '26 09:11 ilepn