Skip to content
Latchkey

Go Coverage + Race - Fix "-covermode" Atomic Requirement in CI

Go’s coverage counters are not goroutine-safe in the default set/count modes. Running -cover together with -race requires -covermode=atomic, or the counters themselves race and the coverage totals become unreliable.

What this error means

A go test -race -cover job yields inconsistent coverage totals between runs, or -race reports a data race inside the generated coverage counters. Switching the coverage mode to atomic when racing is the fix.

go test output
# coverage counters race under -race in the default mode:
go test -race -covermode=count ./...
WARNING: DATA RACE  (in generated coverage counter increment)

Common causes

Coverage counters are not race-safe in set/count mode

The default coverage modes increment plain counters. Under -race with parallel tests those increments race, tripping the detector and skewing counts.

Mixing -race and -cover without atomic mode

When coverage and the race detector run in one pass, only -covermode=atomic makes the counters safe under concurrency.

How to fix it

Use atomic coverage mode with -race

Set -covermode=atomic whenever coverage runs alongside the race detector.

Terminal
go test -race -covermode=atomic -coverprofile=coverage.out ./...

Separate the race and coverage jobs

If you do not want atomic mode everywhere, run a race job and a coverage job independently.

.github/workflows/ci.yml
go test -race ./...                               # race job
go test -covermode=count -coverprofile=c.out ./...   # coverage job

How to prevent it

  • Use -covermode=atomic whenever combining -race and -cover.
  • Or split race and coverage into separate jobs.
  • Key coverage thresholds off a deterministic coverage mode.

Frequently asked questions

What causes ""-covermode" with -race"?
The default coverage modes increment plain counters. Under -race with parallel tests those increments race, tripping the detector and skewing counts.
How do I fix "-covermode" with -race?
Set -covermode=atomic whenever coverage runs alongside the race detector.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card