Learning center

Is a ThreadSanitizer race real when the synchronization is code TSan cannot see?

Not necessarily: TSan reports a race whenever it sees no synchronization between two accesses, including synchronization it cannot model. Rebuild with that synchronization visible to TSan; if the report survives, the race is real.

Why TSan misses synchronization#

ThreadSanitizer (TSan) decides that two accesses race when neither happens before the other in the synchronization it recorded. It records synchronization from instrumented atomic operations and from the threading functions it intercepts. Anything else is invisible to it. The ThreadSanitizer C++ manual states the requirement: "To not produce false positive reports ThreadSanitizer has to see all synchronization in the program, some synchronization operations (namely, atomic operations and thread-safe static initialization) are intercepted during compilation (and can only be intercepted during compilation)."

Two common gaps follow. Standalone fences order memory without being an operation on one particular atomic variable, and GCC documents that TSan does not support them; the clang 18 build below behaves the same way. And atomics compiled in a library built without -fsanitize=thread never reach the runtime at all. In both cases, correct code can produce a report.

A fence-based handoff that TSan reports#

This constructed producer and consumer hand off a plain struct with the fence pattern: relaxed atomic operations on the flag, ordered by a release fence before the store and an acquire fence after the load. The fences are what order the plain writes before the consumer's reads.

Message slot;                       // plain data, published by `ready`
std::atomic<bool> ready{false};

void producer() {
  slot.id = 7;                                          // line 11
  std::snprintf(slot.text, sizeof slot.text, "hello");
  std::atomic_thread_fence(std::memory_order_release);
  ready.store(true, std::memory_order_relaxed);
}

void consumer() {
  while (!ready.load(std::memory_order_relaxed)) {}
  std::atomic_thread_fence(std::memory_order_acquire);
  std::printf("%d %s\n", slot.id, slot.text);           // line 20
}

Built with Ubuntu clang 18.1.3 as clang++ -O1 -g -fsanitize=thread fence.cc and run, it prints the right value, reports a race on every run (five of five), and exits 66 (frames from std::thread and the thread-creation stacks trimmed):

WARNING: ThreadSanitizer: data race (pid=2300)
  Read of size 4 at 0x... by thread T1:
    #0 consumer() fence.cc:20:31 (fence_clang+0x...)

  Previous write of size 4 at 0x... by thread T2:
    #0 producer() fence.cc:11:11 (fence_clang+0x...)

  Location is global 'slot' of size 20 at 0x... (fence_clang+0x...)

SUMMARY: ThreadSanitizer: data race fence.cc:20:31 in consumer()
==================
7 hello
ThreadSanitizer: reported 1 warnings

Clang compiled this with no warning. GCC 13.3.0 says it at compile time, for each fence:

/usr/include/c++/13/bits/atomic_base.h:144:26: warning: 'atomic_thread_fence' is not supported with '-fsanitize=thread' [-Wtsan]

The GCC 13.3.0 manual documents that warning: "ThreadSanitizer does not support std::atomic_thread_fence and can report false positives." The GCC build reports the same race at run time.

Make the synchronization visible and rerun#

Reasoning about the memory model is not enough to dismiss a report like this. The evidence is a build in which TSan can see the ordering and no longer reports. Here the fences can move onto the flag itself, which orders this handoff the same way:

  ready.store(true, std::memory_order_release);         // producer, replaces fence + relaxed store
  while (!ready.load(std::memory_order_acquire)) {}     // consumer, replaces relaxed load + fence

Rebuilt the same way, it prints 7 hello and exits 0 with no report. That rerun is the evidence for the dismissal: same accesses, same order, synchronization now expressed in a form TSan models. Record it in the dismissal, and prefer committing the acquire/release version over keeping the fences with a suppression. The sanitizer project's page on data races gives the same advice for fixes in general: "try to avoid using raw atomic primitives and memory fences when fixing data races."

If the fences have to stay (a lock-free structure that genuinely needs them for several variables at once), keep the evidence that a TSan-visible equivalent ran clean, and limit any suppression to the function that holds the fence.

The same gap with an uninstrumented library#

The second gap needs no fences. In this constructed pair, flag.cc stands in for a library built without TSan, and its atomics are correct acquire and release operations:

// flag.cc, built with: clang++ -O1 -g -c flag.cc
void publish(std::atomic<bool>* f) { f->store(true, std::memory_order_release); }
void wait_for(std::atomic<bool>* f) { while (!f->load(std::memory_order_acquire)) {} }

// app.cc, built with -fsanitize=thread and linked with flag.o
std::thread c([] { wait_for(&ready); std::printf("%d\n", payload); });  // line 11
std::thread p([] { payload = 42; publish(&ready); });                   // line 12

Linked with the uninstrumented object, the program prints 42 and TSan reports a race between app.cc:11:60 and app.cc:12:30 on global payload, exit 66. Rebuilding flag.cc with -fsanitize=thread and relinking prints 42 with no report and exit 0. The clang ThreadSanitizer documentation warns that its ignorelist creates the same situation on purpose: "ignored functions are not instrumented at all. This can lead to false positives due to missed synchronization via atomic operations and missed stack frames in reports."

So before reading the race, check how every object in the link was built. A report whose two stacks sit in instrumented code, while the code that orders them lives in a prebuilt library, a plugin, or a function on the ignorelist, is a candidate for this false positive.

The disconfirming case: a real race in the same shape#

The test above flips just as easily the other way. This variant uses the acquire/release flag that TSan understands, and adds one line in the producer:

void producer() {
  slot.id = 7;
  std::snprintf(slot.text, sizeof slot.text, "hello");
  ready.store(true, std::memory_order_release);
  slot.id = 8;                                          // line 14, written after publishing
}

Three runs, three reports, each printing 8 hello and exiting 66:

WARNING: ThreadSanitizer: data race (pid=2744)
    #0 consumer() real_race.cc:19:31 (real_race+0x...)
    #0 producer() real_race.cc:14:11 (real_race+0x...)
SUMMARY: ThreadSanitizer: data race real_race.cc:19:31 in consumer()
8 hello

The write at line 14 comes after the release store, so no acquire in the consumer orders it, whatever primitive is used. The output also shows the hazard: the consumer read a value written after the handoff. The difference between this report and the fence report is where the reported write sits. In the fence case, the previous write (line 11) comes before the release operation in program order. Here it comes after. Check that position in the "Previous write" stack before suspecting the tool; once the synchronization is visible to TSan, a surviving report is a real race.

Decide from the rebuild#

What the report saysEvidence to collectDecision
Race across a handoff ordered by atomic_thread_fenceGCC's -Wtsan warning, and a run with acquire/release on the atomic itselfDismiss only if the TSan-visible version is clean; prefer committing it
Race whose ordering atomics live in an uninstrumented libraryHow each object in the link was built, and a run with that library rebuilt with TSanDismiss if the full build is clean; record which object was uninstrumented
Race involving a function on the TSan ignorelistThe ignorelist entry and whether that function synchronizesRemove the entry or move the synchronization out of it, then rerun
Previous write after the release operation in program orderThe write's line against the publishing storeFix: the race is real regardless of instrumentation
Report persists in a fully instrumented buildThe two stacks and the synchronization the author intendedFix

For a detector report in Go, which has its own coverage limit, see the guide to Go race reports. A reviewer's race comment without a sanitizer run needs different evidence, covered in the guide to race condition comments.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading