Repository navigation
Description
Summary
Follow-up to #603 (the worker pauses and deletes idle sandboxes). Line references are to 672180b.
When a v1_remote_sandbox row says lifecycle_state = running but the provider reports ERROR, the worker never pauses the sandbox. One way to get there is a Docker container that is still up but whose agent server no longer answers /health. Until the delete rule removes it, 10 days later by default:
- the container keeps running and using CPU, memory and host ports;
- the 12-hour max-session rule never applies, because
decideonly checks it when the live status isRUNNING; - the row matches the sweep every minute, and each check waits 15s for
/healthto time out, holding one of the worker's 10 job slots.
How often this happens in real installs is unknown. The reproduction below freezes the agent server on purpose, and no deployed install runs the worker yet.
Reproduction (Docker, 672180b)
This needs the auth workaround from #625.
- Run the backend and worker with
OH_SANDBOX_LIFECYCLE_IDLE_SECONDS=120andOH_SANDBOX_LIFECYCLE_DELETE_AFTER_SECONDS=240. - Start a sandbox (
POST /api/v1/sandboxes) and wait forRUNNING. - Freeze the agent server without stopping the container:
docker exec <id> kill -STOP <agent-server pids>. - Watch the worker log.
Observed:
- Checks ran at 21:29, 21:30 and 21:31. Each took 15.0s, saw
ERROR, and did nothing. - There was no pause, even though the idle period had passed.
- At 21:31:25 the delete rule removed the sandbox, as expected with a 240s delete period. With the defaults, the container would have kept running for 10 days.
Where
lifecycle/rules.py:97-101: any live status other thanRUNNINGorPAUSEDgetsNOTHINGuntil the delete is due.lifecycle/sweep.py:27-39(_due): arunningrow whose recorded activity is older than the idle period always matches.docker_sandbox_service.py:319-351:get_sandboxcalls/health, and afterstartup_grace_secondsa failure counts asERROR.services/httpx_client_injector.py:18: the 15s default timeout.worker/app.py:13: the worker runs 10 jobs at once by default.
Proposed fix
In decide, apply the pause rules to a running row the provider reports as ERROR. Pause it once the activity recorded on the row is older than the idle period, or once max session is due. The agent server's idle_time can't be read in this state, so the decision uses the row's own timestamps. On Docker the pause stops the container. Once the row is paused, the sweep stops matching it under the pause rules, so the repeated checks stop too. Resuming restarts the container.
Watch out: pause_sandbox returns True when the provider hook returns SKIPPED (on Docker, a container that is created or dead), and check_sandbox then logs sandbox_lifecycle.paused even though nothing changed. Also check what the E2B and k8s pause hooks do for a sandbox in ERROR.
Out of scope:
STARTING: a Docker container in that state would just be skipped.MISSING: nothing is running, and each check costs one quick lookup.- A shorter health-check timeout: not needed once hung sandboxes get paused.
Acceptance criteria
- A Docker sandbox whose agent server hangs after startup is paused (container stopped) within about one idle period of its last recorded activity, not after 10 days.
- After that pause, the sweep doesn't match the row again until the delete rule is due.
- A pause the provider skips isn't logged as
sandbox_lifecycle.paused. test_sandbox_lifecycle_rules.pycoversERRORwith idle due, max session due, and neither due.test_sandbox_lifecycle_tasks.pycovers a skipped pause.
This issue was drafted by an AI agent (OpenHands) on behalf of @aivong.
Activity