Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go Plays Nice With Your Computer --- Race Detec...

Avatar for Raghav Roy Raghav Roy
August 28, 2025
4

Go Plays Nice With Your Computer --- Race Detection and Freedom!

When we moved from single to multi-processor systems to run our programs faster, we created significant problems for language designers, compiler writers and programmers. Hardware and compiler optimisations can arbitrarily invalidate programs that ran correctly before, and makes the execution dependent on the machine you're running it on, which is not what you want.

To solve this, we need precise definitions of what is expected from compilers and programmers, serving as a contract between them, and a language's memory model provides just that.

What are the optimisations that are allowed by Go compilers and the different hardware? What synchronisation mechanisms are provided by the language? What guarantees does a program without races have? These are questions that are expected to be answered by the Go memory model. We also look at how a Go developer can use existing Go semantics to achieve data race freedom by following simple rules, and achieve the gold standard of consistency models in their code - Sequential Consistency.

Go is also pre-packaged with a Race Detector based on LLVM's TSAN v2, that can help you find pesky race conditions in your code during run-time. We will dive into the internals of it briefly, as well as discuss the new changes the latest TSAN v3 brings! How TSAN managed to improve it's detector drastically will be highlighted in the presentation as this new version was ported to the later versions of Go.

Finally the demo segment of the talk will dive into the buggy examples covered in the Data Race Freedom section, and we will try to see if the race detector can flag these bugs. We will also put TSAN v2 and v3 head to head, profiling them as they race to find these races to look under the hood at what's changed.

Avatar for Raghav Roy

Raghav Roy

August 28, 2025
Tweet

Transcript

  1. Back in the old days … Unhappy with your processor’s

    performance? Simply wait for an upgrade!
  2. Back in the old days … Unhappy with your processor’s

    performance? Simply wait for an upgrade! “Valid” optimisations = “Valid” Programs
  3. Moore, The Party Pooper Trying to run the processor faster

    doesn't work anymo(o)re Solution? Mo(o)re Processors!
  4. Mo’ Processors, Mo’ Problems Breaks the old assumption: Valid optimisations

    are “invisible” Valid optimisations for single threads can be “visible” to multi-threaded programs
  5. Memory Models - SC The ideal model is “Sequentially Consistent”

    (SC). The result is the same as some “sequential” execution of operations on a single processor
  6. Memory Models - SC The ideal model is “Sequentially Consistent”

    (SC). The result is the same as some “sequential” execution of operations on a single processor This preserves the order within each thread.
  7. Go’s Memory Model Claim: “If your program is free of

    data races, it will behave as if it's sequentially consistent." – Data Race Free - Sequential Consistency
  8. Go’s Memory Model Claim: “If your program is free of

    data races, it will behave as if it's sequentially consistent." – DRF-SC
  9. Go’s Memory Model Claim: “If your program is free of

    data races, it will behave as if it's sequentially consistent." – DRF-SC
  10. Go’s Memory Model Claim: “If your program is free of

    data races, it will behave as if it's sequentially consistent." – DRF-SC Racy
  11. Go’s Memory Model Claim: “If your program is free of

    data races, it will behave as if it's sequentially consistent." – DRF-SC Synced
  12. Ordering Events in a Concurrent World How do we formalize

    "synchronization"? With the Happens-Before relationship.
  13. Go’s Race Detector (-race) It watches every memory access to

    check for conflicting Writes that aren't ordered by a happens-before relationship.
  14. How Do We Order Events? To find data races, we

    need to know if "Event A happened before B". This seems simple
  15. How Do We Order Events? To find data races, we

    need to know if "Event A happened before B". This seems simple, but it's not.
  16. How Do We Order Events? To find data races, we

    need to know if "Event A happened before B". This seems simple, but it's not. We can’t use Physical Clocks – Impossible to perfectly synchronise
  17. Time Is A “Partial” Order Every Event Can’t Be Ordered

    Against Every Other No “Total” Order
  18. Relativity and Happens-Before • Einstein's big idea: The speed of

    light is the absolute speed limit for any information.
  19. Relativity and Happens-Before • Einstein's big idea: The speed of

    light is the absolute speed limit for any information. • Minkowski’s big idea: An event can only influence a specific region of space-time around it at a given time.
  20. Relativity and Happens-Before • Anything outside this cone is causally

    disconnected. It's not in the past, not in the future. It is "Elsewhere".
  21. From Space-Time to Goroutines • As F. Mattern realized, this

    is a perfect model for distributed systems and concurrent programs!
  22. From Space-Time to Goroutines • As F. Mattern realized, this

    is a perfect model for distributed systems and concurrent programs! • The "speed of light" in Go is the transmission of information
  23. From Space-Time to Goroutines • As F. Mattern realized, this

    is a perfect model for distributed systems and concurrent programs! • The "speed of light" in Go is the transmission of information: a channel send, a mutex unlock, starting a new goroutine.
  24. From Space-Time to Goroutines • As F. Mattern realized, this

    is a perfect model for distributed systems and concurrent programs! • The "speed of light" in Go is the transmission of information: a channel send, a mutex unlock, starting a new goroutine. – Sync Events!
  25. The Problem: This creates a “total” order It forces an

    order on events that might have nothing to do with each other! How Do We Find “Elsewhere” Events
  26. The only thing that truly orders events is causality. Could

    Event A have “Caused” Event B? How Do We Find “Elsewhere” Events
  27. How The Detector Tracks Causality It's like giving every goroutine

    its own multi-dimensional clock. Each goroutine G has a clock array: [C1, C2, C3, ...], one entry per goroutine.
  28. How The Detector Tracks Causality It's like giving every goroutine

    its own multi-dimensional clock. Each goroutine G has a clock array: [C1, C2, C3, ...], one entry per goroutine. When G accesses memory, its own clock CG ticks up.
  29. How The Detector Tracks Causality When G1 syncs with G2,

    G2 updates its clock by taking the maximum of its own and G1's clock.
  30. How The Detector Tracks Causality We want a race to

    be detected if a write from G3 is not ordered before/after the last write from G2
  31. How The Detector Tracks Causality We want a race to

    be detected if a write from G3 is not ordered before/after the last write from G2 – Concurrent Writes!
  32. How The Detector Tracks Causality We want a race to

    be detected if a write from G3 is not ordered before/after the last write from G2 – Concurrent Writes! The clocks for W2 and W3 are not ordered – neither is “strictly greater” than the other
  33. How The Detector Tracks Causality We want a race to

    be detected if a write from G3 is not ordered before/after the last write from G2 – Concurrent Writes! The clocks for W2 and W3 are not ordered – neither is “strictly greater” than the other W2:[1,2,0] vs W3:[0,0,2]
  34. How The Detector Tracks Causality We want a race to

    be detected if a write from G3 is not ordered before/after the last write from G2 – Concurrent Writes! The clocks for W2 and W3 are not ordered – neither is “strictly greater” than the other W2:[1,2,0] vs W3:[0,0,2] – Race Detected!
  35. Conclusions • We can’t always write DRF code --- we

    need a powerful Race Detector • The race detector is awesome, TSAN v3 --- even more so
  36. Conclusions • We can’t always write DRF code --- we

    need a powerful Race Detector • The race detector is awesome, TSAN v3 --- even more so • Vector Clocks model Einsteinian Time --- which is why they work
  37. Conclusions • We can’t always write DRF code --- we

    need a powerful Race Detector • The race detector is awesome, TSAN v3 --- even more so • Vector Clocks model Einsteinian Time --- which is why they work