Skip to content

fix(sandbox): serialize creation with a lock to prevent concurrent sandbox leak#3167

Open
nankingjing wants to merge 1 commit into
volcengine:mainfrom
nankingjing:fix/sandbox-creation-race-leak
Open

fix(sandbox): serialize creation with a lock to prevent concurrent sandbox leak#3167
nankingjing wants to merge 1 commit into
volcengine:mainfrom
nankingjing:fix/sandbox-creation-race-leak

Conversation

@nankingjing

Copy link
Copy Markdown
Contributor

Summary

Concurrent sandbox acquisition for the same key could race and leak a sandbox: two coroutines could both pass the "not created yet" check and each create a sandbox, leaving one orphaned.

Problem

Sandbox creation in SandboxManager was not serialized, so parallel acquire-style calls for the same workspace could both create.

Fix

Add an asyncio.Lock (self._creation_lock) around the create path so only one creation proceeds per manager while the other waits and reuses the created instance.

Test

bot/vikingbot/tests/unit/test_sandbox_creation_race.py — drives concurrent creation and asserts a single sandbox is created / no leak (1 passed).

_get_or_create_sandbox did an unguarded check-then-act, so two
coroutines calling get_sandbox concurrently before a workspace was
cached could each create a backend and leak one. Serialize creation
with an asyncio.Lock and re-check the cache inside the lock.
@nankingjing

Copy link
Copy Markdown
Contributor Author

Friendly ping — the refcount drift this closes (SandboxPool.acquire racing past release / the eviction check) only surfaces under load, so reviewers may want to confirm the new asyncio.Lock doesn't deadlock or reorder with PoolBase._evict_idle's timer task. Happy to add a stress test mirroring the existing one if useful. — nankingjing

@nankingjing

Copy link
Copy Markdown
Contributor Author
Self-reviewed: diff is correct, minimal, and well-tested. No issues found.

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant