Restarting the gateway used to freeze it for 30 minutes — and then kill your work

You change a provider key, run hermes gateway restart to pick it up, and go back to the Telegram thread where a long refactor is 20 minutes in. Your next message comes back with a refusal. You try again — queued. You open the CLI and start a new session: refused too. For the next half hour the whole machine is a brick, and at the end of it the work you were waiting on is simply gone, killed by a timer that decided it had waited long enough.
That is the old restart path. It was designed to be safe — do not restart while work is in flight — but it implemented that as “stop accepting everything now, and hope the in-flight work finishes before the deadline”. On a busy gateway, it rarely does.
What “draining” actually did
The restart request set the gateway’s draining state up front. Draining sounds gentle; in practice it was an admission refusal:
- New sessions were rejected with
runtime_draining. - A follow-up on an existing session was accepted into a queue and never ran.
- Background process completion notices were refused —
Watch notification not admitted … runtime_draining— which is particularly nasty, because the parent was waiting for exactly that notice to continue. - Subagent results had the same problem: children finished, and their results could not be delivered.
Then came the deadline. agent.restart_after_turn_timeout (default 1800 seconds, i.e. 30 minutes) was treated as a hard stop, and at that point stop() interrupted whatever was still live: held turns, delegations, tool subprocesses.
The CLI side had a matching flaw. hermes gateway restart and hermes update waited for the gateway, and when their own wait budget ran out they escalated — supervisor stop, systemctl restart, kickstart -k, and in the worst case SIGKILL. So the last line of defence against “restart lost my work” was itself a kill.
The new model: restart when idle, serve until then
PR #155532 replaces the state machine. The new module is gateway/restart_when_idle.py, and the rule is exactly what the name says:
- A restart request marks the restart as pending but leaves admission open.
- The gateway keeps accepting turns, continuations, notices and delegation results, normally.
- It checks for idleness continuously. When nothing is in flight, admission closes and
stop()happens in the same loop tick, so no new turn can slip in between “we are idle” and “we are restarting”. agent.restart_after_turn_timeoutis no longer a stop deadline. It is a warning interval.
What counts as “in flight” is deliberate and wider than “a turn is running”:
- any awaitable foreground work,
- any live background delegation,
- any notify-watched background process,
- any delegation completion queued or mid-delivery,
because each of those will produce a new turn later. A notice that no configured adapter can deliver does not hold the restart — it stays in the delivery ledger and replays after the restart, which is the correct behaviour: don’t block a restart on a message you cannot send anyway.
Wedged work gets an escape hatch too. A chat turn or managed-worker turn that has been silent past agent.gateway_timeout (default 1800 seconds) does not hold the restart forever. An open approval or clarify prompt does hold it, because that is a human decision the restart would otherwise destroy.
The cap now warns instead of killing
When the warning interval elapses and work is still running, the gateway says so, loudly and repeatedly, and names the units by kind, id and session — no turn content:
WARNING Restart still waiting after 20s; live work is NOT interrupted.
Waiting on: chat(session=...); process(process_id=proc_269..., notify=true)
And it keeps waiting. The only thing that stops live work on purpose is an explicit force — the updater’s pause-for-update with {"force": true}. SIGTERM keeps its immediate-stop meaning, because that is what SIGTERM means and nobody expects otherwise.
The CLI callers were fixed on the same principle: when hermes update or hermes gateway restart runs out of its own wait budget, it no longer escalates to a kill. It leaves the gateway running with the restart pending, prints one line naming what it is waiting on, and the fleet check reports restart pending.
Before and after, measured
The PR’s live A/B run compared origin/main with the branch on the same scenario — a restart requested while a turn, a background process and a delegation were all in flight:
| Event | Before | After |
|---|---|---|
| New session B during the wait | refused, runtime_draining |
admitted, ran |
| Follow-up on session A | queued, never ran |
completed |
| Background completion notice | Watch notification not admitted … runtime_draining |
admitted and ran as a turn |
| At the 20-second cap | wait timed out … proceeding to stop(), held turn interrupted, two tool subprocesses killed |
warning naming the chat and the process; keeps waiting |
| Restart | exit 75, 23 seconds after SIGUSR1, work lost | exit 75, 61 seconds after SIGUSR1, everything finished |
hermes update wait expiring on a busy gateway |
supervisor stop / systemctl restart / SIGKILL | gateway left running, restart pending |
The restart took longer in the “after” column — 61 seconds instead of 23 — and that is the whole point. Those extra 38 seconds are the work that used to be destroyed.
What you will notice in daily use
Three changes are visible without reading any code:
Restarts are no longer a “stop the world” moment. You can request a restart from one client and keep working from another. The restart lands between tasks, not on top of one.
hermes update on a busy machine may look like it did nothing. It exits with the restart pending instead of killing the gateway. Check with the gateway’s own status or fleet check; restart pending is the expected state, not a failure.
A genuinely stuck turn no longer blocks upgrades forever. The agent.gateway_timeout escape hatch means a turn that has been silent for 30 minutes stops counting as live work. If you have long-running turns that legitimately produce no output for that long, raise agent.gateway_timeout rather than relying on the old forced kill.
Two knobs are worth understanding together:
agent:
restart_after_turn_timeout: 1800 # how often to warn, no longer a kill deadline
gateway_timeout: 1800 # silence after which a turn stops holding the restart
The first is a notification cadence now. The second is the real liveness bound, and it is the one that decides whether a wedged turn can hold a restart.
Release status and the honest caveats
This is on main only (merged November 14, 2026) and not in v0.23.1, which was tagged November 9. On a stable release, the old behaviour applies: a restart still drains immediately and can still kill in-flight work at the cap.
Update (2026-11-17): these changes shipped in v0.23.2 (November 16) —
hermes updatebrings them in. The “not in v0.23.1” wording above is a snapshot from publication time.
Two details the PR does not claim, and neither should you:
- It is not a zero-downtime restart. The gateway still stops and starts; sessions still reconnect. What changed is when it stops — at idle, not on a timer.
- A force still wins. If you pass force to the updater, live work is stopped on purpose. That is an explicit instruction, not an accident.
If your gateway runs long delegated jobs, this pairs well with the loop watchdog (which handles the “turn never ends” failure mode) and the update upgrade guide (which covers the update path end to end). For the command surface itself, see the hermes gateway command reference.
The design lesson
“Graceful shutdown” is usually implemented as “stop accepting new work, then wait for existing work”. That ordering is what turns a restart into an outage: the moment you stop accepting, every client that tries to talk to you experiences a failure, even though the machine is perfectly healthy and could have served them.
Inverting it — keep serving, wait for idle, then close admission and stop in one atomic step — is a small change in ordering with a large change in behaviour. The gateway does not become more available because it restarts faster; it becomes more available because it stops refusing work it could still do.
If you have been scheduling restarts for quiet hours to avoid the freeze, you can stop doing that. Restart at 2pm; it will wait for the gap instead of creating one.