Fix TOCTOU race condition in Docker image download logic #10736
Merged
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.
Fix TOCTOU race condition in Docker image download logic
Issue: The
StartDockerImageDownloadfunction has a time-of-check-time-of-use (TOCTOU) race condition where the availability check happens outside the lock, creating a race window for duplicate downloads.Changes Made:
isDockerImageAvailableUnlockedhelper function - Internal function that checks image availability without taking locks, allowing it to be called while holding a write lockIsDockerImageAvailable- Now uses the unlocked helper with proper RLock for external callersStartDockerImageDownload- Moved both availability and downloading checks inside a single lock scope usingdefer, eliminating the TOCTOU race conditionTestStartDockerImageDownload_ConcurrentCalls- Verifies only one goroutine starts downloadTestStartDockerImageDownload_ConcurrentCallsWithAvailableImage- Verifies no download starts for available imagesTestStartDockerImageDownload_RaceWithExternalDownload- Verifies race condition handlingTesting:
IsDockerImageAvailableuses RLock (safe to call inside write lock)StartDockerImageDownloadto move availability check inside the lockIsDockerImageAvailabletakes RLock by creating unlocked helperSuccess Criteria Met:
✅ Availability check moved inside mutex lock
✅ All tests in
pkg/cli/docker_images_test.gopass✅ Added concurrency test with multiple goroutines
✅ No performance regression (same lock behavior, just better ordering)
✅ No deadlocks (verified by avoiding nested RLock/Lock calls)
✅ Merged main branch and recompiled workflows
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.