Skip to content

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

Open

Description

@pdettori

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

  1. 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.
  2. 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.
  • Spec §5's relay-restart row matches the result.

Activity

  1. added
    bug
    on Oct 6, 2026
  2. added this to the v0.11.0 milestone on Nov 17, 2026
  3. webchang commented on Nov 17, 2026

    @webchang
    Member

    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.
    • The rig or Kind check (a turn sent during a relay restart gets 503 pending, then succeeds on its sandbox) hasn't been run against fix(sandbox-relay): drain parked sessions on SIGTERM so a restart clears presence (#453) #462 as far as I know. The unit test covers the drain itself.

    If you'd rather keep it open for the live check, I can narrow the title to that instead.

  4. modified the milestones: v0.11.0, v0.12.0 on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions