Skip to content

fix: prevent race condition in concurrent task execution - #7

Open
pocikode wants to merge 1 commit into
mmccl5:mainfrom
pocikode:fix/race-condition
Open

fix: prevent race condition in concurrent task execution#7
pocikode wants to merge 1 commit into
mmccl5:mainfrom
pocikode:fix/race-condition

Conversation

@pocikode

@pocikode pocikode commented Jul 12, 2026

Copy link
Copy Markdown

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 same Task.Action() simultaneously, both writing to State, Err, History — data race.

Fix:

  • Added a running flag to Runner, checked under the existing mutex. Second concurrent Run() call returns immediately.
  • Switched from direct field access to SetState()/SetErr() inside Run() for consistent locking.
  • Added the missing go.mod file (repo could not build without it).

The running flag resets via defer after all goroutines finish, so subsequent Run() calls work fine.

- 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.
@pocikode

Copy link
Copy Markdown
Author

/claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant