Fix race condition in Command.IsRunning() check#310
Open
circleci-app[bot] wants to merge 3 commits into
Open
Conversation
The test "TestOrchestrator/error: task agent encountered fatal error" was failing because of a race condition where IsRunning() was being called before the isCompleted flag was set, even though Wait() had returned. The issue was that isCompleted was set in the defer of wait(), which runs after the function returns but with no guarantee of when relative to the calling goroutine receiving the return value. This caused a race where: 1. executeAgent() calls taskAgent.Wait() which blocks 2. The process exits and wait() returns an error 3. The defer sets isCompleted based on ProcessState 4. executeAgent returns the error to the main goroutine 5. shutdown() checks IsRunning() before the defer completes 6. IsRunning() returns true because isCompleted is still false 7. This generates an incorrect "task agent process is still running" error The fix is to set isCompleted=true immediately after Wait() returns, before any error handling or defer cleanup. This ensures IsRunning() returns false as soon as Wait() completes, regardless of whether there was an error or what the ProcessState is. This eliminates the race condition and ensures correct behavior in the shutdown phase.
f6b4612 to
69256f8
Compare
Base automatically changed from
renovate/go-github.com-modelcontextprotocol-registry-vulnerability
to
main
May 22, 2026 06:17
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
Fixes a race condition in the task agent command execution that was causing test failures in
TestOrchestrator/error: task agent encountered fatal error.Problem
The test was failing with an unexpected error message:
This was caused by a race condition where
IsRunning()was called in the shutdown phase before theisCompletedflag was set, even thoughWait()had already returned.Root Cause
The
isCompletedflag was being set in the defer of thewait()function:The defer runs after
wait()returns, but there's no guarantee about timing relative to the calling goroutine. This created a race where:Wait()returns an errorexecuteAgent()returns the error to the main goroutineshutdown()checksIsRunning()isCompletedis still falseIsRunning()incorrectly returns trueSolution
Set
isCompleted = trueimmediately afterWait()returns, before any error handling:This ensures
IsRunning()returns false as soon asWait()completes, eliminating the race condition.Testing
TestOrchestrator/error: task agent encountered fatal errorOriginal failing job
View this task in the CircleCI web app →