Skip to content

fix: resolve race conditions in concurrent task execution - #8

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

fix: resolve race conditions in concurrent task execution#8
TonyWei041209 wants to merge 1 commit into
mmccl5:mainfrom
TonyWei041209:fix/race-condition

Conversation

@TonyWei041209

Copy link
Copy Markdown

Description

Fixes the race condition in concurrent task execution. When Runner.Run() runs tasks concurrently in separate goroutines, direct access to task.State, task.Err, etc. causes data races with concurrent readers (e.g., tests calling GetState()).

Changes

  1. task.go: Added TransitionTo(state, error) method that atomically sets state, error, history, and timestamps (StartedAt/FinishedAt) under the mutex. This replaces manual lock-write-unlock patterns in Runner.Run().

  2. runner.go: Replaced all direct field access (task.mu.Lock(), task.State = ..., task.Err = ...) with calls to task.TransitionTo(). The new Status() method provides a thread-safe snapshot of all task states.

  3. runner_test.go: Updated tests to use public API (GetState(), GetErr()). Added stress tests:

    • TestRunner_Run_ConcurrentWrites: 50 tasks with concurrent metadata reads
    • TestRunner_Run_Timeout: context deadline test
    • TestRunner_Run_Status: Status() output validation
    • TestRunner_Run_Historical: State history correctness
    • TestTask_ConcurrentAccessors: 20 goroutines reading + 1 writing concurrently
    • TestRunner_Run_MetadataRace: 3 concurrent readers + 50 tasks with metadata
  4. go.mod: Added module definition (was missing).

Verification

All tests pass with -race flag:

go test -race ./...

Closes #1

@TonyWei041209

Copy link
Copy Markdown
Author

Hi @mmccl5, this PR has been open for about 10 hours with all checks passing and no merge conflicts. Just a friendly ping to see if you'd have a moment to review and merge. Thanks!

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.

🎯 Fix Race Condition in Concurrent Task Execution

1 participant