Ray's Knowledge Base
Unlock

Stale gcov data makes tests crash at exit after a rebuild

PitfallVerified 28 Sep 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

In a coverage build, many tests fail after a large rebuild although every test case passes. The test prints its passing doctest summary, then dies at process exit:

[doctest] test cases: 21 | 21 passed | 0 failed | 0 skipped
... Bus error***Exception
profiling: .../trace_loader.cpp.gcda: cannot merge previous GCDA file: mismatched number of counters (2)
profiling: .../trace_loader.cpp.gcda: cannot merge previous GCDA file: corrupt arc tag (0x01d4f934)

Tests that do not touch the changed code crash too, and the same binary run alone may exit 0 once and 138 the next time.

Cause#

gcov writes counters at exit and merges them into the .gcda files the earlier runs left. When the objects were rebuilt with a different shape (new functions, changed code), the old .gcda files no longer match, and the merge reads them as corrupt; in this case the runtime then crashed. Parallel ctest runs writing the same .gcda files make it worse.

Fix#

Delete the coverage data before running the tests again; the next run writes fresh files:

find build/build-tests -name '*.gcda' -delete

For timing runs of a coverage build, send the data elsewhere with GCOV_PREFIX=<scratch dir> so it never lands next to the objects.

Evidence#

  • dftracer-utils, ctest --preset tests after adding a variant alternative to a core type (2026-09-27): 13 of 17 duql tests reported Bus error or SegFault after passing. After find build/build-tests -name '*.gcda' -delete, test_http_parser exited 0 and the same ctest run had no exit crashes.

Also found by searching

  • profiling: trace_loader.cpp.gcda: cannot merge previous GCDA file: mismatched number of counters, Bus error at exit
  • in a coverage build tests print all passed then crash at exit after a big rebuild
  • how do I fix tests crashing at exit with gcov after rebuilding with --coverage
  • corrupt arc tag gcda, exit code 138 intermittent, need to delete stale .gcda files before running ctest
  • profiling: trace_loader.cpp.gcda: cannot merge previous GCDA file: corrupt arc tag
  • ctest tests pass then Bus error at exit in coverage build, exit code 138 intermittently
  • how do I fix gcov crashes at process exit after rebuilding with code changes
  • mismatched number of counters gcda doctest passed but segfault at exit lcov build