Chapter 0 · Section 5 of 8 · 4 min read

In the wild

Three projects, three answers to the same question: what should the language or toolchain do about a data race? Each case follows the same five questions. Where a project's own documents do not say why it chose something, the case says so.

Go: find races while the program runs

What they chose. The Go toolchain has a built-in data race detector. You switch it on with the -race flag of go test, go run, go build and go install.

Why, in their words. The documentation introduces it like this: "Data races are among the most common and hardest to debug types of bugs in concurrent systems." (Data Race Detector, go.dev.)

What else was possible. The page does not discuss alternatives, and I did not find a design document in the sources I opened. So I will not guess why Go chose a run-time detector over a compile-time rule.

What it costs. The same page says: "for a typical program, memory usage may increase by 5-10x and execution time by 2-20x."

What to copy. Run your tests with -race in continuous integration. It reports unordered accesses even in runs where the wrong answer did not happen, which is the property you need for a bug that depends on timing.

CPython: one lock around the interpreter

What they chose. CPython has a global interpreter lock, the GIL. The Python FAQ says it is "used internally to ensure that only one thread runs in the Python VM at a time", and that Python offers to switch threads only between bytecode instructions. (Python FAQ, "What kinds of global value mutation are thread-safe?", docs.python.org.)

What that does and does not give you. Because each bytecode instruction is atomic, operations such as L.append(x) are atomic. The FAQ also lists operations that are not atomic, and the first one is i = i + 1, our counter. Its advice is: "When in doubt, use a mutex!"

Why the GIL exists. The pages I opened do not explain the original reasoning, so I will not state one.

Why people want to remove it. PEP 703 says the GIL "prevents multiple threads from executing Python code at the same time" and calls it "an obstacle to using multi-core CPUs from Python efficiently." (PEP 703, Status: Final, Python-Version: 3.13.) The FAQ adds that a free-threaded build with the GIL disabled exists since Python 3.13, is officially supported as of 3.14, and is not the default build.

What it costs. Threads cannot run Python code in parallel in the default build, per the quotes above.

What to copy. Do not rely on "the interpreter makes this safe by accident." The docs themselves list i = i + 1 as not atomic, and free-threaded builds change how thread safety is achieved, as PEP 703 describes.

Rust: make the bug a compile error

What they chose. Safe Rust prevents data races at compile time. The Rustonomicon says: "Safe Rust guarantees an absence of data races", defined as two or more threads concurrently accessing a memory location where one or more is a write and one or more is unsynchronized. It adds that "Data races are prevented mostly through Rust's ownership system alone: it's impossible to alias a mutable reference." (Data Races and Race Conditions, The Rustonomicon.)

Why. The page states the reason as undefined behaviour: "A data race has Undefined Behavior, and is therefore impossible to perform in Safe Rust."

What it does not do. The same page says Rust "does not prevent general race conditions", calls that "mathematically impossible in situations where you do not control the scheduler", and notes that Rust can still deadlock or "do something nonsensical with incorrect synchronization". A data race is one specific kind of timing bug. Others remain your job.

What it costs. The page does not list costs. In practice you must express sharing in the type system, with types such as Mutex and atomics, which Chapter 1 shows.

What to copy. Even if you never write Rust, the distinction is useful: a data race is a precisely defined problem that a tool can detect or prevent, while race conditions in general need design and tests.

Putting the three side by side

GoCPythonRust (safe code)
When is the bug handled?While running, with -raceMostly hidden by the GILAt compile time
Does it find the bug for you?Yes, if the racy code runsNo. i = i + 1 stays non-atomicRefuses to compile racy code
Main costSlower runs, more memoryNo parallel Python threads by defaultShared data must go through types such as Mutex