Go race detector flaky: one pass missed a real race

8 min read | KEIBIDROP Series | November 2026

By Marius-Florin Cristian

A test that makes the Go race detector flaky can hold a real race that shows only in some runs, and a single run of the test does not show it. A data race in one test showed in 1 of 60 runs under Go's race detector on an Intel MacBook Pro; those 60 runs ran in one process, where the detector reports the same race once, so the count shows that the race happened and not how often. The local pass of the race lane missed it, and the first CI run of the pull request failed on it. At the pull request head the same test ran 300 times under the race detector with no race, on 29 November 2026, and in the same pull request removing 5 of 15 new guards failed no test until new tests were written for them.

A test swapped a package variable while an engine goroutine read it

The race was a test writing a package variable while a goroutine it did not join read it. In the 0.6.0 pull request of KEIBIDROP, the test TestCreateRoom_RefreshAdoptsThePairsPickAndClosesTheParkedLeg called a helper that wrote the package variable bridgeHintApexes, the domains a relay may suggest a bridge under, once at the start and once in its cleanup. Meanwhile the engine's bridge leg goroutine read the same variable in validBridgeHintAddr, with no lock, and the test did not wait for that goroutine to end. Whether a run shows the race depends on how the two goroutines are scheduled in that run.

The detector can only report what happens in the run it watches. Go's documentation says it directly:

"The race detector only finds races that happen at runtime, so it can't find races in code paths that are not executed."

A single pass of the race lane can miss a race that shows in some runs

One pass is one chance to see the race, because our race lane, make test-race, runs every test of five packages once under -race, and one pass took 156 to 178 s on our Mac. The local pass of the lane showed nothing, and the first CI run of the pull request, the same lane on GitHub's Linux runner, failed on the race.

The command that showed the race on the Mac asked for many runs of that test alone:

go test -race -count=60 -run TestCreateRoom_RefreshAdoptsThePairsPickAndClosesTheParkedLeg ./pkg/logic/common/

It failed once in the 60 runs, and on the same Mac 300 runs of the fixed test took 304 s, so 60 runs cost about a minute.

The detector reports a race once per process, so count runs in separate processes

Within one process the race detector reports a racing pair of accesses once, so -count shows whether a race happens and not how often. We checked it on Go 1.26.9 with a test that writes one variable from two goroutines on every run: under go test -race -count only the first run failed, and the same test binary run as separate processes failed every time. Go's testing package marks a test as failed when the process's count of race reports grows (checkRaces in src/testing/testing.go), and a report that the detector does not repeat does not grow it.

A test that races on every run One process, -count=5 reported once, the rest pass Five processes, -count=1 each run reports it Filled: the run failed with a race report. Go 1.26.9, macOS.
The same racing test, run in one process and in separate processes. A count of failures inside one process says that the race happened; a count across processes says how often.

So the 1 failure in 60 runs says that the race happened in at least one of them, and we have no rate for it before the fix. To measure one, run the test in a new process each time, as in the commands at the end of this post.

The fix puts every value a test swaps behind an atomic pointer

Each value that a test swaps while engine goroutines read it is now a seam.Value, and tests swap it through one helper. The type holds one value behind an atomic pointer, from internal/seam/seam.go at the head of pull request 243:

// Value holds one swappable value. A zero Value loads the zero T.
type Value[T any] struct{ p atomic.Pointer[T] }

func (s *Value[T]) Load() T {
	if p := s.p.Load(); p != nil {
		return *p
	}
	var zero T
	return zero
}

func (s *Value[T]) Store(v T) { s.p.Store(&v) }

The helper in internal/testkit stores the new value and puts the old one back in the test's cleanup:

func Swap[T any](t testing.TB, s *seam.Value[T], v T) {
	t.Helper()
	old := s.Load()
	s.Store(v)
	t.Cleanup(func() { s.Store(old) })
}

The pull request converted 30 package variables that tests swap: 21 in the engine core, 5 in the session layer, 2 in the file system layer and 2 in the gRPC service, and bridgeHintApexes is one of them. At the pull request head the race test ran 300 times under the race detector with no race, and inside one process that count still holds, because the first race would have been reported. A new test, TestAllowLoopbackBridgeHints_SwapWhileALegChecksAHint, checks a hint on a goroutine while the test swaps the list.

Removing 5 of 15 new guards failed no test

A guard counts when its test fails with the guard removed. Removing 5 of 15 new guards failed no test; after new tests, removing any of the 7 guards checked again failed its test, those 5 among them. We checked each guard of the same pull request in a scratch copy: remove the one guard, run the tests that cover it, and look for a failure by assertion. A guard whose removal fails nothing has no test, whatever the test names say.

Check, by removing each guard aloneGuards whose test failed when removedGuards with no failing test
The fixes for the busy word and the invites10 of 155 of 15
The same five after new tests, and two more7 of 70 of 7
The earlier fixes (the running session's peer, invite trust, the listener bound and others)11 of 110 of 11; a twelfth is benchmark code only

Checked on 29 November 2026 on a Mac, each guard removed alone. One of the five was the line go kd.breakAccept(p) in busy.go, which ends a connect that waits in Accept when the friend answers that they are busy. With the line removed every test still passed, and since the new test TestBusy_WordEndsAConnectParkedInAccept exists, removing the line fails that test.

Marius said the same in his Pass the SALT talk, about the bugs a file system hides:

"It's hard because it's not always obvious. You think it works but then you catch it."

The rule now sits in the seam package and the race lane

The package doc of seam states the rule, the helper puts each value back, and the race lane covers five packages. The doc, at the head:

"Package seam holds the values a test swaps while engine goroutines read them: a delay it shrinks, a function it replaces. Load and Store are atomic, so a swap is not a data race."

internal/seam/seam.go, pull request 243

On main the race lane covered four packages with a timeout of 180 s per package. At the head it covers five, the codec included, with 600 s, because under -race the slowest packages take over two minutes on a fast desktop and a small CI runner is slower.

A race shows in a run, so run the test in many processes

# the race lane: every test of five packages, once
make test-race

# one test, 60 separate processes: the number of lines printed is the number of runs that failed
for i in $(seq 60); do
  go test -race -count=1 -run '^TestCreateRoom_RefreshAdoptsThePairsPickAndClosesTheParkedLeg$' ./pkg/logic/common/ >/dev/null || echo failed
done | wc -l

For a guard, copy the tree, delete the guard, and run the tests that cover it: a green result means the guard has no test yet. How the engine's parts fit together is in the docs: keibidrop.com/docs/explanation/how-keibidrop-works.html.

Limits. 300 runs with no race bound the chance of the race; they do not prove that no race is left. Running a test many times under -race, and a -race build under real work, is the way to find the next one.

Some package variables that tests set are still plain variables, because their readers take the value before a goroutine starts: tokens.go hands claimPollTick to its goroutine as an argument, and a test assigns it directly. Each one moves into a seam when a goroutine starts to read it later.

The observations come from one Intel MacBook Pro and from GitHub's Linux runner. Another machine schedules goroutines differently and can show the same race more or less often.

A race report fails the test even when its assertions pass

Race detected during execution of test

Go's testing package prints this line when the race detector reported a race while the test ran, and it marks the test as failed even if every assertion passed (src/testing/testing.go in Go 1.26). The report above the line names the two accesses and the goroutines that made them.

What is a data race?

The Go documentation defines it: "A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write." In the race above, the test wrote a package variable and an engine goroutine read it.

How do you fix race conditions?

Put the two accesses in an order: Go's race detector article names a channel or a mutex as the typical fix, and the sync/atomic package to stay lock-free. For a value that tests replace, we hold it behind an atomic pointer and swap it through one helper that restores it. Then run the test in many processes under -race to see whether the race still shows.

Checked on 30 November 2026 against pull request 243 of KEIBIDROP.

Related: how KEIBIDROP works, the series by theme, pin GitHub Actions to a commit SHA, not a tag, testing peer-to-peer code without external dependencies, pull request 243