You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sandbox-relay: no SIGTERM teardown, so a relay restart leaves stale presence records and turns in the gap get an in-turn tool error instead of a 503 #453
Found during the P6.3 acceptance (#425, Task 18 step 4) on the shared KVM rig.
What happens
sandbox-relay has no SIGTERM handler. packages/sandbox-relay/src/main.ts starts the servers and installs no signal handling. So systemctl restart sh-relay (and a pod deletion or rollout on K8s) ends the process on the default signal action, and no attach stream's teardown runs (packages/sandbox-relay/src/relay.ts). As a result:
the presence records in sh:sandbox:records are not removed;
no detach mark (sh:sandbox:detached:<id>) is written.
Spec P6.3 §5, row "Relay restart", says: "On a graceful restart every record disappears (teardown), then the workers re-Hello … Turns in the gap get 503: SandboxAffinityPendingError …". Every real restart is instead the row's other case, "A hard-killed relay can leave stale records, which turns may lease until the worker reattaches".
Measured (rig, VM path, 4 sandboxes)
The relay was stopped at 17:34:50.107 and listening again at 17:34:51.779. Workers reattached 1.8–4.7 s after the stop. A poller on the rig never saw a record disappear or a detach mark appear.
A microVM session's turn sent into that gap was leased to its affine sandbox: the record was still present, but its worker had not reattached. The exec failed with:
2 UNKNOWN: no live worker for sandbox 'moca_microvm_0'
The failure reached the model as the bash tool's output. The model retried and succeeded, so the workspace was intact and the session stayed on its sandbox. The turn never got a 503 with Retry-After.
Why it matters
In the reconnect gap a turn gets an error inside the turn, not a retryable 503. A model may report it as a failure.
The affinity grace clock (detach marks) never starts on a relay restart. This is harmless for a few seconds, but it means the grace path in spec §4 is not what protects sessions on a restart; the stale records are.
Proposed fix
Relay: on SIGTERM, stop accepting new streams and run teardown for every parked session. That removes its record and writes its detach mark, with the remove awaited. Then end the streams and exit, bounded by a short deadline under systemd's TimeoutStopSec and the pod's terminationGracePeriodSeconds.
Optionally, harness: classify no live worker for sandbox from routeExec as saturated, so the turn answers 503 rather than passing the text to the model. This also covers a genuinely hard-killed relay.
Acceptance
A relay integration test: SIGTERM with two attached workers removes both records and writes both marks before exit.
On a rig or Kind: a turn sent during a relay restart gets 503 (pending) and then succeeds on its own sandbox.
Proposing to close this: the fix it asks for is on main.
Item 1, the relay's SIGTERM teardown, landed in fix(sandbox-relay): drain parked sessions on SIGTERM so a restart clears presence (#453) #462.drain() in packages/sandbox-relay/src/relay.ts refuses later Hellos, runs each parked session's teardown (record removed, then the detach mark written, both awaited), and then ends the streams. installShutdownSignals in main.ts runs that stop once on SIGTERM or SIGINT, bounded at 10 s, well under TimeoutStopSec and terminationGracePeriodSeconds. test/relay-drain.test.ts covers the acceptance's first point: two attached workers, both records removed and both marks written before exit. Spec P6.3 §5's "Relay restart" row describes the graceful path, with the hard-kill case kept.
Item 2 was optional. It would classify no live worker for sandbox from routeExec as saturated in the harness, so a turn gets a 503 instead of a tool error. After fix(sandbox-relay): drain parked sessions on SIGTERM so a restart clears presence (#453) #462 that error only follows a hard-killed relay. It arrives partway through a turn, inside a tool call, so it needs its own design. If you want it, I'd open it as its own issue rather than keep this one open for it.
Activity
docs(specs): P6.3 §5 -- a relay restart leaves its records today (no …
fix(sandbox-relay): drain parked sessions on SIGTERM so a restart cle…
Merge pull request #462 from pdettori/fix/453-relay-sigterm-teardown
docs: correct the "no network", caps and changelog claims before v0.11.0
webchang commentedon Nov 17, 2026
Proposing to close this: the fix it asks for is on main.
drain()inpackages/sandbox-relay/src/relay.tsrefuses later Hellos, runs each parked session's teardown (record removed, then the detach mark written, both awaited), and then ends the streams.installShutdownSignalsinmain.tsruns that stop once on SIGTERM or SIGINT, bounded at 10 s, well underTimeoutStopSecandterminationGracePeriodSeconds.test/relay-drain.test.tscovers the acceptance's first point: two attached workers, both records removed and both marks written before exit. Spec P6.3 §5's "Relay restart" row describes the graceful path, with the hard-kill case kept.no live worker for sandboxfromrouteExecas saturated in the harness, so a turn gets a 503 instead of a tool error. After fix(sandbox-relay): drain parked sessions on SIGTERM so a restart clears presence (#453) #462 that error only follows a hard-killed relay. It arrives partway through a turn, inside a tool call, so it needs its own design. If you want it, I'd open it as its own issue rather than keep this one open for it.If you'd rather keep it open for the live check, I can narrow the title to that instead.