Why Go's Race Detector Breaks in Git Bash

go dev.to

I am working on a small concurrent seat booking system in Go to learn about race conditions, mutexes, and concurrent testing. The project itself was straightforward, but running the race detector led me down a very unexpected debugging rabbit hole.

The problem

A normal test run worked perfectly:

"go test ./..."

But enabling the race detector with just adding a "-race" resulted in:

==2684==ERROR: ThreadSanitizer failed to allocate ...
FAIL github.com/.../internal/booking

The confusing part was that the error appeared before any test started running. Even replacing my test with a simple dummy test didn't help.

Along the way I checked and fixed several things:

  1. Verified my Go installation, even ended up reinstalling it.

  2. Fixed an incorrect GOPROXY configuration.

  3. Replaced an outdated MinGW installation with MSYS2 GCC, realized my PATH was completely broken, MinGW being on top of MYSYS2 made the Go program direct to MinGW as default as it took precedence.

  4. Confirmed that a minimal Go project passed go test -race, mind you this was on powershell. I had absolutely NO CLUE why this worked on powershell but not Git Bash.

The Solution

[drum roll please ;)]

This made me realize that the issue wasn't my booking system at all.

I was running the tests from Git Bash/MSYS2.
The POSIX emulation layer interfered with the race detector's virtual memory allocation, causing ThreadSanitizer to fail during initialization.
This was a typical memory allocation conflict where the emulation layer has already taken up memory addresses the race detector was trying to access, resulting in inadequate virtual memory space.

Switching the default terminal to PowerShell immediately resolved the problem.

After that:

go test -race ./...

worked as expected.

Source: dev.to

arrow_back Back to Tutorials