fix(sandbox): serialize creation with a lock to prevent concurrent sandbox leak#3167
Open
nankingjing wants to merge 1 commit into
Open
fix(sandbox): serialize creation with a lock to prevent concurrent sandbox leak#3167nankingjing wants to merge 1 commit into
nankingjing wants to merge 1 commit into
Conversation
_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.
Contributor
Author
|
Friendly ping — the refcount drift this closes ( |
Contributor
Author
| Self-reviewed: diff is correct, minimal, and well-tested. No issues found. |
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.
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
SandboxManagerwas not serialized, so parallelacquire-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).