Go traps · Chapter 14 of 16
A mutex copied by a value receiver
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Defer
- 3 tasks
- Go
- In the all labs plan
The first 2 chapters of Go traps are free. This one comes with the all labs plan.
Why you need this
go vet knows this trap, but go test does not run that check, so a test of Add passes:
$ go vet .
main.go:13:9: Add passes lock by value: bijligo/chapters/try-w3-e14m.Counter contains sync.Mutex
$ go test
PASS
ok bijligo/chapters/try-w3-e14m 0.001s
The fix is pointer receivers, func (c *Counter) Add, on every method of a type that holds a lock, so all calls share one mutex. With that one change go vet is quiet and the count is 1000 every run.
Copying a mutex has a second trap. A copy taken while it is locked is locked too, and nobody will ever unlock it (output trimmed):
var mu sync.Mutex
mu.Lock()
snapshot := mu
snapshot.Lock()
$ go run .
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [sync.Mutex.Lock]:
So a half fix, one method with a pointer receiver and another still copying, can hang as well as race. A test of concurrent code should not wait for ever: wait for the goroutines in a select with time.After, and fail after a second.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.go: twenty top-ups at once, on purpose
Write reproduce() in repro.go, which returns an int. Make wallets with NewWallets() from wallets.go, start 20 goroutines at once, each calling TopUp("D11006", 50000), wait for all of them, and return Balance("D11006").
The check runs your reproduce() twice, each time in a fresh build of the case's folder with the race detector on: beside the original wallets.go the race detector must report a data race (or the runtime must crash with a concurrent map error), and beside a fixed wallets.go there must be no race and it must return 1000000. So the goroutines must really top up at the same time: start them all before you wait for any. No real waiting and no input: reproduce() must finish within 3 seconds.
- Task 2
wallets.go: every top-up lands in the wallet
Fix Wallets in wallets.go so any number of goroutines can top up and read at once. TopUp(driver string, paise int) int adds paise to the driver's wallet, prints topup <driver>: +<paise> paise, balance <n> and returns the new balance; Balance(driver string) int returns the balance in paise, 0 for a driver with no wallet yet; NewWallets() *Wallets returns empty wallets ready to use. Keep these names and signatures, the struct's fields and TopUp's line.
The check runs under the race detector with its own drivers and amounts: it calls TopUp and Balance from many goroutines at the same time, then checks every balance adds up. Fixing TopUp alone still lets a Balance read the map while a top-up writes it, and the race detector reports that too.
- Task 3
wallets_test.go: a test that would have caught it
Write a Go test file, wallets_test.go, that would have caught this bug before last Thursday's release. The check runs your file three times, each in a fresh build with the race detector on: beside the original wallets.go, where at least one test must fail; beside the fixed wallets.go, where every test must pass; and beside a wrong fix that mends TopUp only, where at least one test must fail.
So in one test, start goroutines that call TopUp and others that call Balance, all before you wait for any, then check the balance adds up. Under the race detector a test fails by itself when two goroutines use the map without holding the same lock. A copy of a locked mutex stays locked, so code halfway through a fix can hang: wait with a select and time.After and fail after a second. Each test must finish within a second or two (the whole file is stopped after 8 seconds).
What you will learn
- repro.go: twenty top-ups at once, on purpose
- wallets.go: every top-up lands in the wallet
- wallets_test.go: a test that would have caught it
More in Defer
Build "A mutex copied by a value receiver" yourself
BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go traps are free. This one comes with the all labs plan.