Skip to content

Keep Modal sandboxes working through stuck stops, tunnel drops, and daemon exits - #4472

Merged
ymichael merged 7 commits into
mainfrom
bb/machine-errors-with-modal-sandboxes-preservation-thr_cubeswnbsj
Sep 29, 2026
Merged

ymichael merged 7 commits into
mainfrom
bb/machine-errors-with-modal-sandboxes-preservation-thr_cubeswnbsj

Conversation

@ymichael

@ymichael ymichael commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Four defects made Modal sandboxes fall over in ordinary conditions. They came out of reproducing a Discord #bugs report ("Machine errors with Modal Sandboxes") on real Modal sandboxes through the real Account Pooler. That report showed a thread stuck "stopping", about 14 repeated "Machine preservation is interrupting this turn" errors, and a queued message that never sent.

  1. A stop the daemon never answers pinned the thread in stopping forever. settleThreadStopCommandResult settled a failed stop only for unknown_environment and host_unavailable. A stop that timed out (command_timeout) left the thread in stopping, and nothing sweeps that status. Machine preservation stops turns before snapshotting. Every attempt then found the thread still stopping, refused to snapshot, and retried on the Modal plugin's per-minute idle sweep, adding one preservation error per attempt. The queued "Sending when the thread stops" message never sent. On 0.44.0, which does not settle host_unavailable either, this reproduced on a real sandbox by freezing the daemon's connection without closing it: a new error every minute, and the queue stuck until the connection returned.
  2. The daemon's loopback proxy mishandled an upstream response cut mid-body. Agents on remote machines reach the server through machine-auth-proxy, including the Account Pooler for model calls. When the upstream connection dropped after headers, a reset made the proxy end the caller's response cleanly, so a truncated stream looked complete. A clean close left it open, so the caller waited on a dead stream until its own timeout. A simulated 2s bb Connect tunnel drop stalled Claude Code for 297s. The reporting instance's tunnel client logged 18 abnormal closes in 2.5 days.
  3. Nothing restarted a machine daemon without a service manager. On Linux without systemd, including every Modal sandbox, the installer starts the daemon once with nohup, and bb-app host-daemon exits when its daemon exits. A crashed or killed daemon left the machine offline for good. So did a daemon that exited to apply a protocol self-update: it exits expecting a restart that never comes.
  4. The Modal plugin's default sandbox size was Modal's minimum reservation, 0.125 CPU and 128 MiB. bb's own processes in a sandbox already use about 1 GiB at idle.

What changed

  • apps/server/src/services/threads/thread-lifecycle.ts: a stop that times out now settles the thread like an undeliverable stop: the turn is interrupted, the thread goes idle, and the queue drains. When the interrupt got no answer (command_timeout or host_unavailable) and the thread has settled, the stop is done. It no longer sends a follow-up release to a daemon that isn't answering, so preservation snapshots on its first attempt. If the same daemon reconnects still running the turn, the existing reconnect path re-sends the stop.

  • apps/host-daemon/src/machine-auth-proxy.ts: the caller's response is destroyed whenever the upstream response errors or closes incomplete, and when the upstream request errors after headers were sent.

  • bb-app host-daemon --supervise (packages/bb-app/src/launcher.ts) supervises the daemon from its first launch, including before its first successful connection. Readiness polling runs alongside supervision and is cancelled when the launcher stops; a startup update or crash no longer exits the launcher before the restart loop begins. It restarts the daemon when it exits:

    • A protocol self-update now exits with HOST_DAEMON_RESTART_EXIT_CODE (75, in @bb/config/machine-service) instead of 0, and is restarted at once so the new version starts.
    • Other non-zero exits and signals restart after 1s, doubling to 30s while the daemon keeps failing, and back to 1s after it stays up 60s.
    • Exit 0 is a deliberate stop (machine suspension, server move) and ends the launcher, so a suspending machine's snapshot has no live writer.
  • install-machine.sh passes --supervise only on its two nohup launch paths. systemd (Restart=always) and launchd (KeepAlive true) machines are unchanged; both already restart on any exit code. The full-stack bb-app launcher also restarts its daemon on any exit and never propagates the daemon's code, so 75 cannot collide with the app-update shim.

  • The launcher sets BB_HOST_DAEMON_SUPERVISED for the daemon, so a server-move replacement launcher keeps --supervise.

  • Modal sandboxes created without a preset now reserve 1 CPU and 2 GiB (DEFAULT_SANDBOX_SIZE). Modal treats these as reservations: a sandbox can burst above them and is billed for whichever is higher, reservation or use. Existing machines keep the size recorded on their resource. The settings copy, README and Modal skill are updated.

  • The default Modal image now pins Codex 0.159.0, Claude Code 2.1.284, and Pi 0.99.0 (previously 0.153.4, 2.1.263, and 0.84.0). New images contain the updated agents; existing machines and snapshots keep their installed versions.

No server/daemon wire changes, so no HOST_DAEMON_PROTOCOL_VERSION bump.

Not changed:

  • A daemon that replies with a stop error still leaves the thread stopping; an existing test asserts that.
  • A preservation stop is still labelled "Stopped manually".
  • The idle pause still interrupts quiet but busy turns and ignores background tasks, as intended for now.
  • A turn that was running when its daemon died is still lost.
  • After a self-update, the supervising launcher keeps its old code until its next full start.

How you verified

Image refresh: pnpm exec turbo run test --filter=bb-plugin-environment-modal-sandbox passed all 64 tests. Built the updated Dockerfile on Modal and started a fresh sandbox: all three CLIs reported their pinned versions and passed help/startup checks. Codex app-server initialization and model/list succeeded (including gpt-6-sol); Pi RPC get_state succeeded. The debug sandbox was stopped afterward. Authenticated inference with these new agent versions was not exercised.

New tests, each failing without its fix:

  • test/threads/thread-stop-retry.test.ts: a stop the host never answers leaves the thread idle and sends no follow-up release.
  • test/services/machines/machine-preservation-unanswered-stop.test.ts: preservation of a machine whose daemon never answers saves on the first attempt, with one notice, and frees a message queued mid-stop.
  • src/machine-auth-proxy.test.ts: the upstream resets or closes mid-body. Without the fix, the reset case completes the caller's response and the close case hangs.
  • test/host-daemon-supervision.test.ts (bb-app), covering the restart rules:
    • no restart on 0
    • crash delays of 1, 2, 4, 8, 16, 30, 30s
    • an immediate restart on 75 that does not grow the crash delay
    • the delay reset after 60s up
    • no restart once shutdown is requested
  • test/host-daemon-startup.test.mjs (bb-app): builds and runs the real launcher with child-process fixtures. An update exit (75) or crash (1) before readiness restarts and connects; intentional exit (0) and SIGTERM during startup stop without restarting. The update and crash cases failed before the startup-supervision fix.
  • test/app/install-machine-script.test.ts: --start launches an unserviced daemon with --supervise; both skip-service installs pass it.
  • server-move/server-moved.test.ts: the replacement launcher keeps --supervise.
  • plugins/environment-modal-sandbox/server.test.ts: a machine without a preset requests and records 1 CPU / 2048 MiB.

Commands:

  • Startup-supervision fix: pnpm exec turbo run test lint typecheck --filter=bb-app; all 207 package tests and the typecheck passed. Formatting and git diff --check also passed.
  • pnpm exec turbo run typecheck lint --filter='...[origin/main]': 107/107 tasks.
  • Tests: @bb/server 3286, @bb/host-daemon 736, bb-app 207, @bb/config 108, Modal plugin 64, installer 49.

Live checks on real Modal sandboxes (Claude Code on Haiku 4.5 through the Account Pooler). Tunnel drops were simulated by a relay inside the sandbox.

Check Before After
2s drop mid-response stream silent 297s, turn 338s stream silent 6.6s, turn 48s (0.44.0 bundle patched with the proxy change)
SIGKILL the daemon machine offline for good back in about 13s, logged "restarting host daemon in 1s"; a second kill logged "in 2s" (this launcher)
Suspend a supervised machine — the launcher stands down and the snapshot completes
Protocol self-update — see below

For the self-update check:

  • A worktree dev server was reachable only for /install* and /internal/*, through a filtering proxy and a cloudflared quick tunnel.
  • A sandbox enrolled to it through the new installer, which launched the daemon with --supervise.
  • Raising the dev server's protocol from 220 to 221 made the daemon download the new build and exit with 75. The launcher logged "host daemon exited with code 75 - restarting host daemon", and the machine reconnected at protocol 221 about 16s after the server restart.

🤖 Generated with Claude Code

AGENT GENERATED

ymichael and others added 4 commits September 28, 2026 16:12
A thread only left `stopping` when the daemon confirmed the stop, or when
the stop failed with `host_unavailable` or `unknown_environment`. A stop
that timed out (`command_timeout`) left the thread in `stopping` forever:
nothing sweeps that status, so a queued "send when the thread stops"
message never sent. Machine preservation, which stops turns before
snapshotting, hit this every attempt, refused to snapshot, and retried on
the Modal plugin's per-minute idle sweep, adding one "Machine preservation
is interrupting this turn" error per attempt.

A stop that times out now settles the thread like one the host can't
receive: the turn is interrupted, the thread goes idle, and the queue
drains. When the interrupt got no answer and the thread has settled, the
stop is done; it no longer sends a follow-up release to a daemon that
isn't answering. Preservation therefore snapshots on its first attempt.
If the same daemon later reconnects still running the turn, the existing
reconnect path re-sends the stop instead of reviving it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
On a remote machine, agents reach the bb server (including the Account
Pooler that serves their model calls) through the daemon's loopback
auth proxy. When the upstream connection dropped after response headers
were forwarded, the proxy either ended the caller's response cleanly
(connection reset), so a truncated stream looked complete, or never
ended it (connection closed), so the caller waited on a dead stream
until its own timeout.

On a real Modal sandbox behind a simulated 2s bb Connect tunnel drop, a
streaming Claude Code response stalled for 297s before Claude Code gave
up and retried. With this change the proxy destroys the caller's
response whenever the upstream response ends incomplete or errors, so
the caller sees the failure at once and retries. The same test on a
sandbox running a daemon patched with this change resumed streaming
6.6s after the cut (turn 48s instead of 338s).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A machine created without a size preset passed no CPU or memory to
Modal, so it got Modal's minimum reservation of 0.125 CPU and 128 MiB.
bb's own processes in a sandbox (daemon, plugin host workers, launcher,
file watcher) already use about 1 GiB at idle, and agents need a real
core. Modal treats these values as reservations: a sandbox can burst
above them and is billed for whichever is higher, reservation or use.

New machines without a preset now reserve 1 CPU and 2048 MiB. Existing
machines keep the size recorded on their resource when resumed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
On Linux without systemd (Modal sandboxes, containers), the installer
starts the host daemon once with nohup, and `bb-app host-daemon` exits
when its daemon exits. Nothing restarted a daemon that crashed or was
killed, and nothing restarted one that exited to apply a protocol
self-update, so the machine stayed offline until it was suspended and
resumed. On a real Modal sandbox, killing the daemon left the machine
disconnected for good.

`bb-app host-daemon --supervise` now restarts the daemon when it exits:
- A self-update exits with HOST_DAEMON_RESTART_EXIT_CODE (75) instead of
  0 and is restarted at once, so the new version starts.
- Any other non-zero exit or signal is restarted after 1s, doubling to
  30s while it keeps failing, and back to 1s once a daemon stays up 60s.
- Exit 0 is a deliberate stop (machine suspension, server move) and ends
  the launcher, so a suspending machine's snapshot has no live writer.

The installer passes --supervise only on its two nohup launch paths, so
systemd and launchd machines are unchanged; both already restart on any
exit code. The launcher sets BB_HOST_DAEMON_SUPERVISED for the daemon so
a server-move replacement launcher keeps --supervise.

Verified on a real Modal sandbox running this launcher: two SIGKILLs of
the daemon each brought the machine back in about 13s ("restarting host
daemon in 1s", then "in 2s"), and a machine suspend completed with the
launcher standing down instead of restarting the daemon.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ymichael ymichael changed the title Settle unanswered thread stops and fail truncated machine proxy responses Keep Modal sandboxes working through stuck stops, tunnel drops, and daemon exits Sep 29, 2026
ymichael and others added 3 commits September 28, 2026 22:06
An unanswered stop now settles the thread and lets machine preservation
continue to the save step. The save step is what keeps a live daemon's
writers out of the snapshot: it asks the daemon to shut down and cancels
the suspend when the daemon's session does not close. Nothing tested
that. This covers a connected daemon that answers neither the stop nor
the shutdown: the provider snapshot is never taken and the machine
returns to active.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ymichael
ymichael merged commit 6f77c7e into main Sep 29, 2026
19 checks passed
@ymichael
ymichael deleted the bb/machine-errors-with-modal-sandboxes-preservation-thr_cubeswnbsj branch September 29, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant