Go · Chapter 18 of 26

Locks and the race detector

By the end of this chapter you can say what a data race is, find one with the race detector (go run -race and go test -race), protect shared data with sync.Mutex and sync.RWMutex, count with sync/atomic, run something exactly once with sync.Once, and choose between a lock and a channel. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Concurrency
  • 4 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go are free. This one comes with the all labs plan.

Why you need this

Pooja runs a report every night: the energy of all 3,600 sessions, added up by one goroutine for each session so it finishes in a blink. On Monday it said one number, on Tuesday another, for the same day's data, and both were too small. Tenzin's live counter of sessions per station has the same problem at the evening peak, and once the ingest service simply stopped with fatal error: concurrent map writes. Wrong numbers in a finance report are worse than slow ones: Pooja has to trust every paisa.

From chapter 15 you know how to start goroutines and wait for them, and from chapter 16 how to pass values between them on channels. What you have not had yet is shared memory: one variable, one map, one struct, changed by many goroutines at once. Go lets you do that, and it goes wrong in a way that does not show up every time. This chapter shows why, how Go's race detector finds it, and the tools in the sync package that make it right.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    counter.go: a session counter safe for many goroutines

    Write SessionCounter in counter.go, which counts the sessions started at each station: NewSessionCounter() SessionCounter returns a counter ready to use, (c SessionCounter) Start(station string) adds one session for that station, and (c *SessionCounter) Count(station string) int returns how many have started there (0 for a station with none). Add the fields the struct needs.

    Many goroutines use one counter at once. The Check calls Start 2,000 times from 50 goroutines, 400 times for each of ST001 to ST005, while 10 more goroutines call Count, all under the race detector: every Count must then be 400, Count("ST240") must be 0, and there must be no data race.

  2. Task 2

    cache.go: a status cache for many readers

    Write StatusCache in cache.go: NewStatusCache(c bijli.Central) StatusCache, (s StatusCache) Get(id string) (bijli.Status, error) and (s StatusCache) Len() int, how many statuses are kept. Get returns the kept status of charger id if it has one; if not, it asks c.ChargerStatus(context.Background(), id), keeps the status and returns it. An error is returned as it is (an unknown id gives an error that wraps bijli.ErrUnknownCharger) and nothing is kept for it, so the next Get asks again.

    Guard the kept statuses with a field of type sync.RWMutex (the Check looks for it), so many Gets can read at the same time. Do not hold the lock while asking the central system: a Get for a kept status must not wait for another charger's call.

    The Check gets C1001 to C1010 (10 calls to the central system), then makes 2,000 Gets of them from 100 goroutines under the race detector: still 10 calls, and Len() is 10. It then asks for C9999 twice (two more calls, Len() still 10). Last, with a central system that takes 300 ms, a Get for a kept charger made while another Get is asking for a new one must return in well under 150 ms.

  3. Task 3

    total.go: fix the racy energy total

    Pooja's TotalEnergy(sessions []bijli.Session) int adds up the EnergyWh of every session, in one goroutine for each session. It returns a different total almost every run, always too small, and the race detector reports a data race.

    Fix it so that it still adds each session in a goroutine of its own, and returns the right total every time, with no data race: TotalEnergy(bijli.Sessions()) must return 44632024. The Check runs it under the race detector, and also with only the first three sessions.

  4. Task 4 · interview question

    Interview: wallet.go, the wallet that went negative

    Tenzin's Wallet has NewWallet(paise int64, ledger io.Writer) Wallet, (w Wallet) Debit(session string, paise int64) error and (w *Wallet) Balance() int64. Debit takes paise from the balance and writes one line to the ledger; when the balance is less than paise it returns an error that wraps ErrNotEnough and writes nothing. Tenzin used sync/atomic, so the race detector reports nothing, but a driver with 1,000 paise who had 30 sessions charged at the same moment, 100 paise each, ended at -2,000 paise.

    Fix the wallet so that it never goes below zero. The Check runs 30 Debits of 100 paise at once on a wallet of 1000, with a ledger that takes 2 ms for each write: exactly 10 must succeed, 20 must return an error that wraps ErrNotEnough, the ledger must get exactly 10 lines and the balance must end at 0. Three Debits of 300 from 1000 then succeed, a fourth returns ErrNotEnough, and the balance is 100.

    Joseph will ask: what does the race detector find, and what can it never find?

What you will learn

  • A data race: two goroutines use the same memory at the same time, and at least one changes it.
  • The race detector: go run -race and go test -race watch the memory and report races.
  • sync.Mutex: one goroutine at a time; Lock, Unlock, and defer to unlock.
  • sync.RWMutex: many readers at once, or one writer.
  • sync/atomic: atomic.Int64, a counter with no lock.
  • sync.Once: run something exactly once, however many goroutines ask.
  • What the detector cannot see, and when to use a channel instead of a lock.

More in Concurrency

Build "Locks and the race detector" yourself

BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. 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 are free. This one comes with the all labs plan.