Skip to content

sandbox lifecycle: an unhealthy running sandbox is never paused until the 10-day delete #626

Closed

Description

@aivong-openhands

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 decide only checks it when the live status is RUNNING;
  • the row matches the sweep every minute, and each check waits 15s for /health to 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.

  1. Run the backend and worker with OH_SANDBOX_LIFECYCLE_IDLE_SECONDS=120 and OH_SANDBOX_LIFECYCLE_DELETE_AFTER_SECONDS=240.
  2. Start a sandbox (POST /api/v1/sandboxes) and wait for RUNNING.
  3. Freeze the agent server without stopping the container: docker exec <id> kill -STOP <agent-server pids>.
  4. 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

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.py covers ERROR with idle due, max session due, and neither due. test_sandbox_lifecycle_tasks.py covers a skipped pause.

This issue was drafted by an AI agent (OpenHands) on behalf of @aivong.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions