fix(orchestrator): fix race condition in Cleanup causing missed cleanup functions#3136
Open
AdaAibaby wants to merge 4 commits into
Open
fix(orchestrator): fix race condition in Cleanup causing missed cleanup functions#3136AdaAibaby wants to merge 4 commits into
AdaAibaby wants to merge 4 commits into
Conversation
…up functions There was a TOCTOU race between Add()/AddPriority() and run(): 1. Add() checks hasRun (false) outside the lock 2. Add() blocks waiting for mu.Lock() 3. run() acquires lock, sets hasRun=true, executes all cleanups, unlocks 4. Add() acquires lock, appends f — but run() already finished Result: f is never executed, potentially leaking resources (network namespaces, cgroups, file descriptors, etc.). Fix: - Move hasRun.Store(true) inside the lock in run() - Add double-checked locking in Add()/AddPriority(): re-check hasRun after acquiring the lock and execute f inline if cleanup already ran Add race-condition tests that reliably reproduce the bug with -race.
Contributor
There was a problem hiding this comment.
Code Review
Executing the cleanup and priority cleanup functions while holding the mutex lock can lead to deadlocks if the functions attempt to acquire the same lock or perform blocking operations. Releasing the lock before executing these functions avoids this risk.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Avoid holding the mutex while executing cleanup functions in the double-check branch. This prevents potential deadlocks if a cleanup function performs blocking operations or re-enters the lock.
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.
There was a TOCTOU race between Add()/AddPriority() and run():
Result: f is never executed, potentially leaking resources (network namespaces, cgroups, file descriptors, etc.).
Fix:
Add race-condition tests that reliably reproduce the bug with -race.
/cc @jakubno @dobrac @ValentaTomas Would appreciate your review on this change, thanks!