Tech News
← Back to articles

Go’s race detector has a mutex blind spot

read original related products more articles

Go's race detector has a mutex blind spot

28 Jul, 2025

I recently read Ralf Jung's blog post "There is no memory safety without thread safety" which mentions that Go is not a memory safe language in the presence of data races.

"But Go comes with a built in data race detector " some might say.

This reminded me of a quirk in Go's dynamic data race detection that causes it to miss data races in executed code that could easily be spotted by a human .

Here is the code the data race detector struggles with:

package main import ( "fmt" "sync" ) var counter int var mutex sync . Mutex func increment ( wg * sync . WaitGroup , id int ) { defer wg . Done () mutex . Lock () counter ++ fmt . Printf ( "Counter: %d

" , counter ) mutex . Unlock () if id == 1 { counter ++ ; } } func main () { var wg sync . WaitGroup wg . Add ( 2 ) go increment ( & wg , 0 ) go increment ( & wg , 1 ) wg . Wait () fmt . Printf ( "Final: %d

" , counter ) }

Threads 0 and 1 increment a shared counter, guarded by a lock. Thread 1 then performs an additional increment on the counter, not guarded by the lock.

... continue reading