Go concurrency exercises
Go makes concurrency simple with goroutines and channels, but subtle traps lurk: sending on an unbuffered channel in the same goroutine causes an instant deadlock, loop variables captured in goroutines share state, and forgetting wg.Done() halts execution.
Trace the concurrent execution paths, fix the synchronization bug, and confirm all goroutines complete cleanly.
The concurrency bugs you will meet
- Sending or receiving on an unbuffered channel blocks until another goroutine is ready.
- Calling wg.Wait() on the main goroutine before workers call wg.Done() freezes forever.
- Closing a nil channel or closing an already-closed channel triggers an immediate panic.
- Reading from a closed channel returns the zero value and false without blocking.
- Goroutines declared inside range loops capture the loop variable by reference in older Go versions.
The exercises
Medium
- Unexpected output in concurrent range loop processingAsync and race conditions
- Stalled Goroutine: Data Not Received as ExpectedAsync and race conditions
- Goroutines finish before expected despite using sync.WaitGroupAsync and race conditions
- Increasing memory usage in periodic task schedulerLogic
- Consistent Panic on Concurrent Atomic Value UpdateAsync and race conditions
- Worker function not exiting properly causing timeoutsAsync and race conditions
- Consistent Panic in Channel HandlingAsync and race conditions
- Panic during channel operation in concurrent Go programAsync and race conditions
- Unexpected zero value in concurrent Go programAsync and race conditions
- Concurrent access to mutex in struct causes data raceAsync and race conditions
- Unpredictable behavior in concurrent message handlingAsync and race conditions
- Deadlock on unbuffered channel without receiverAsync and race conditions
- Deadlock in range loop over unclosed channelInfinite loops
- Panic on closing already closed Go channelAsync and race conditions
- WaitGroup Add inside goroutine creates race conditionAsync and race conditions
- Loop variable captured by reference in goroutineAsync and race conditions
- Missing mutex unlock on early error returnAsync and race conditions
- Select default branch starves channel communicationAsync and race conditions
Errors you might be searching for
More JavaScript practice by topic
Questions
How does BugHunt detect Go deadlocks?
The Go runtime includes a built-in deadlock detector. When all goroutines are sleeping on locks or channels, it halts with 'fatal error: all goroutines are asleep - deadlock!'.