Skip to content

fix(docker): serialize snapshot commits with the sandbox lifecycle lock - #3127

Open
kittimzhe wants to merge 1 commit into
opensandbox-group:mainfrom
kittimzhe:fix/snapshot-commit-lifecycle-lock
Open

kittimzhe wants to merge 1 commit into
opensandbox-group:mainfrom
kittimzhe:fix/snapshot-commit-lifecycle-lock

Conversation

@kittimzhe

Copy link
Copy Markdown
Contributor

Summary

Fixes #3121 — on the Docker runtime, POST /sandboxes/{id}/snapshots runs docker commit in the background worker without the per-sandbox lifecycle lock that pause, resume, renew, delete and expiry all take. Since docker commit pauses the container by default until the image is written, an unserialized commit against a Running sandbox:

  • makes GET /sandboxes/{id} report Paused mid-snapshot while the commit is still streaming, then flip back — consumers see a bogus state flip;
  • can renew expiration for a sandbox a concurrent delete is tearing down;
  • races the expiry timer (docker commit blocking while expiry deletes the container produces a partial image).

Fix

PersistedSnapshotService gains an optional lifecycle_lock_factory at construction. The Docker assembly (create_snapshot_service) passes docker_service._sandbox_lifecycle_lock, so the background worker holds the source sandbox's lifecycle lock for the whole commit — a concurrent pause/resume/delete/expiry now waits instead of racing the paused container.

Runtimes that snapshot outside the container lifecycle (Kubernetes, Fast Sandbox) pass None and take an unchanged untracked path (factored out so the None case is byte-for-byte the old behavior).

Tests

test_snapshot_worker_serializes_with_lifecycle_lock: the worker holds the per-sandbox lock — a competitor acquiring the same sandbox lock blocks for the whole commit and only proceeds after the worker releases. 26/26 in test_docker_snapshot_runtime.py, 334/334 in test_docker_service.py.

`POST /sandboxes/{id}/snapshots` runs `docker commit` in the background
worker without taking the per-sandbox lifecycle lock that pause, resume,
renew, delete and expiry use — while `docker commit` pauses the container
by default until the image is written (opensandbox-group#3121).

`PersistedSnapshotService` now takes an optional
`lifecycle_lock_factory` at construction. The Docker assembly passes
`docker_service._sandbox_lifecycle_lock`, so the background worker holds
the source sandbox's lifecycle lock for the whole commit; a concurrent
pause/resume/delete/expiry waits instead of racing the paused container.
Runtimes that snapshot outside the container lifecycle (Kubernetes,
Fast Sandbox) pass None and keep the old behavior — the untracked worker
path is factored out so the None case is unchanged.

The regression test proves the worker holds the lock (a competitor
acquiring the same sandbox's lock blocks until the worker releases).
@github-actions github-actions Bot added component/server size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 26, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T06:53:43.414335Z 306b35c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 306b35c952

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +610 to +611
with lock_factory(record.source_sandbox_id):
self._run_snapshot_worker_untracked(record)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Coordinate sandbox status reads with the commit lock

When a running Docker sandbox is snapshotted, container.commit() temporarily exposes Docker's Paused state. This scope only excludes operations that acquire _sandbox_lifecycle_lock; DockerSandboxService.get_sandbox() and list_sandboxes() never acquire it, and _container_to_sandbox() maps container.attrs["State"] directly. Consequently GET/list requests during a long commit still report Paused and then Running, so the user-visible state-flip scenario remains; status reads must either participate in the coordination or mask the commit's internal pause.

AGENTS.md reference: server/AGENTS.md:L42-L42

Useful? React with 👍 / 👎.

Comment on lines +523 to +525
blocked = not acquired_other.wait(timeout=2) # it must be blocked
release.set() # let the worker finish; competitor
return blocked # proceeds after the lock is released

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make the serialization test exercise an actual lock

fake_lock_factory's _Ctx never acquires a synchronization primitive, so the competitor immediately sets acquired_other and blocked is false. That boolean is only returned from the patched _create_snapshot_worker, whose return is discarded, so the test still passes if the lifecycle context is released before the snapshot work; use a real per-ID lock and assert that acquisition remains blocked until the worker releases it.

AGENTS.md reference: server/AGENTS.md:L42-L42

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/server size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docker runtime: snapshot commit is not serialized with pause/resume/delete of the same sandbox

1 participant