fix: prevent race condition in concurrent task execution - #7
Open
pocikode wants to merge 1 commit into
Open
Conversation
- Add run-once guard to prevent concurrent Run() calls from executing the same tasks simultaneously (data race on Task state) - Use SetState()/SetErr() methods instead of direct field access for consistent mutex protection - Add go.mod file (was missing, required for builds) - Running flag is reset after all goroutines complete via defer Fixes race condition when Run() is called from multiple goroutines concurrently, which could cause two goroutines to execute the same Task.Action() and write to Task fields simultaneously.
Author
|
/claim |
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.
Race condition: if
Run()gets called from two goroutines at the same time, both copy the task list and spawn goroutines for the same tasks. Two goroutines executing the sameTask.Action()simultaneously, both writing toState,Err,History— data race.Fix:
runningflag toRunner, checked under the existing mutex. Second concurrentRun()call returns immediately.SetState()/SetErr()insideRun()for consistent locking.go.modfile (repo could not build without it).The
runningflag resets viadeferafter all goroutines finish, so subsequentRun()calls work fine.