liveBook · Manning

Race-condition detection

As explained in Go in Practice, Second Edition

Race-condition detection is dynamic analysis of concurrent program execution that identifies conflicting unsynchronized accesses to shared data.

Race-condition detection dynamically analyzes concurrent execution to find conflicting unsynchronized accesses to shared data. In Go, race detection is enabled with the --race flag on tools such as go run and go test. section 5.2.2 section 1.2.4

Use race detection during development to:

  • Run tests with the race detector and include the check in code review or continuous integration. section 5.2.2 section 1.2.4
  • Treat concurrent map writes as a synchronization problem, then protect shared state with a mutex. section 5.2.2

Concurrent word counter

A word-counting command processes multiple files in separate goroutines while updating one words.found map. Running it normally can produce “fatal error: concurrent map writes”; running go run --race reports the read and write stacks and identifies the conflicting access in tallyWords. Adding a sync.Mutex to the words structure and locking around map access prevents simultaneous modification.

How Go in Practice, Second Edition covers it

How deep it goes
Explains conflicting map accesses, shows diagnostic output, and stops short of detector internals.
Examples
Uses a concurrent word counter, a concurrent map-write failure, a race report, and a mutex-protected revision.
What it assumes
Assumes concurrent Go programs with goroutines and shared state, while demonstrating mutex protection for the defect found.
What you can do after
Diagnose/debug concurrent access defects with Go's go run --race and go test --race commands.
How it connects
Connects race detection with goroutines, shared memory, maps, mutexes, testing, and continuous integration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.