Repository navigation
Keep Modal sandboxes working through stuck stops, tunnel drops, and daemon exits - #4472
Merged
ymichael merged 7 commits intoSep 29, 2026
Conversation
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]>
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
deleted the
bb/machine-errors-with-modal-sandboxes-preservation-thr_cubeswnbsj
branch
September 29, 2026 18:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
stoppingforever.settleThreadStopCommandResultsettled a failed stop only forunknown_environmentandhost_unavailable. A stop that timed out (command_timeout) left the thread instopping, and nothing sweeps that status. Machine preservation stops turns before snapshotting. Every attempt then found the thread stillstopping, 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 settlehost_unavailableeither, 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.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.nohup, andbb-app host-daemonexits 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.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_timeoutorhost_unavailable) and the thread has settled, the stop is done. It no longer sends a follow-upreleaseto 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:HOST_DAEMON_RESTART_EXIT_CODE(75, in@bb/config/machine-service) instead of 0, and is restarted at once so the new version starts.install-machine.shpasses--superviseonly on its twonohuplaunch paths. systemd (Restart=always) and launchd (KeepAlivetrue) machines are unchanged; both already restart on any exit code. The full-stackbb-applauncher 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_SUPERVISEDfor 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 Code2.1.284, and Pi0.99.0(previously0.153.4,2.1.263, and0.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_VERSIONbump.Not changed:
stopping; an existing test asserts that.How you verified
Image refresh:
pnpm exec turbo run test --filter=bb-plugin-environment-modal-sandboxpassed 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 (includinggpt-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: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:--startlaunches 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:
pnpm exec turbo run test lint typecheck --filter=bb-app; all 207 package tests and the typecheck passed. Formatting andgit diff --checkalso passed.pnpm exec turbo run typecheck lint --filter='...[origin/main]': 107/107 tasks.@bb/server3286,@bb/host-daemon736,bb-app207,@bb/config108, 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.
For the self-update check:
/install*and/internal/*, through a filtering proxy and acloudflaredquick tunnel.--supervise.🤖 Generated with Claude Code