alitto / pond

🔘 Minimalistic and High-performance goroutine worker pool written in Go
MIT License
1.43k stars 60 forks source link

Synchronize read & write of `TaskGroupWithContext`'s `err` variable #37

Closed thekondor closed 1 year ago

thekondor commented 1 year ago

Context

That is not stated whether Wait() & Submit() for TaskGroupWithContext are allowed for usage from different execution threads. As end user I expected that it is.

Problem

// thread 1
group.Submit(func() error {
   return errors.New("failed")
})

// thread 2
group.Wait()

leads to race condition because of non-synchronized (write & read) operations to .err.

The problem is detectable in 100% cases by a built-in race checker.

PR

The change introduces usage of mutex to synchronize access to this variable at the above mentioned code flows.