TryBugHunt
goAsync race conditionMedium

Deadlock on unbuffered channel without receiver

BugFind the bug and fix the code

What it should do

Send worker exit status to a channel and return status. Example: collectWorkerErrors(42) -> 42

What happens instead

fatal error: all goroutines are asleep - deadlock!

package main

import "fmt"

func collectWorkerErrors(code int) int {
	ch := make(chan int)
	ch <- code
	return <-ch
}

func main() {
	fmt.Println(collectWorkerErrors(42))
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the async and race conditions pattern

Goroutines racing on shared data, a WaitGroup used wrong, or a channel that deadlocks.

What you'll practise

A medium Go debugging exercise about async and race conditions: goroutines racing on shared data, a WaitGroup used wrong, or a channel that deadlocks.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

What went wrong: An unbuffered channel blocks on sending until another concurrent goroutine is actively receiving. Sending and receiving sequentially on the same goroutine causes an immediate deadlock.

The fix: Provide a buffer capacity of at least 1 when creating the channel: make(chan int, 1).

Remember: Unbuffered channels synchronize goroutines; a single goroutine cannot send and then receive from an unbuffered channel synchronously.

The pattern behind it: async and race conditions.

More async and race conditions to practise

Seen this error in your own code?

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/70186a08-41ed-4a82-8340-6694eed63bef" title="Debugging challenge: Deadlock on unbuffered channel without receiver" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/70186a08-41ed-4a82-8340-6694eed63bef">TryBugHunt</a></p>