A multithreaded C++ server crashes only under load; how do you use GCC's sanitizers to find the cause, and why need two separate builds?
answer
- memory errors versus races
- thread cannot join address or leak
- unsynchronized accesses, not wrong results
- 5x to 15x slower
- instrument everything, replay the load
basics
~10 sBuild two instrumented binaries and replay the load against each: -fsanitize=address,undefined for memory errors, -fsanitize=thread for data races. GCC rejects -fsanitize=thread together with address or leak, so one build cannot run both.
solid answer
~40 sA crash that only appears under load is usually a data race or a lifetime bug that depends on timing, so I would not start in a debugger. I would build one binary with `-fsanitize=address,undefined -g -fno-omit-frame-pointer` and one with `-fsanitize=thread -g`, then replay the load test against each. ASan catches a use-after-free that a race can cause; TSan catches the race itself, reporting two conflicting accesses with their stacks even on runs where nothing crashed. They must be separate builds: GCC's manual says `-fsanitize=thread` "cannot be combined with `-fsanitize=address`, `-fsanitize=leak`". Expect TSan to be slow, about 5x to 15x per Clang's docs, so lower the load or lengthen the run, and instrument as much of the program as possible, because uninstrumented code can hide races.
code
cpp · 17 lines#include <cstdio>
#include <thread>
#include <vector>
struct Stats { long requests = 0; };
int main() {
Stats stats;
std::vector<std::thread> workers;
for (int t = 0; t < 4; ++t)
workers.emplace_back([&stats] {
for (int i = 0; i < 10000; ++i)
++stats.requests; // data race: undefined behaviour
});
for (auto& w : workers) w.join();
std::printf("requests=%ld\n", stats.requests);
}go deeper
Know that ThreadSanitizer finds data races and AddressSanitizer finds memory errors, and that they are separate builds.
Explain what a TSan report contains and why it can fire on runs that did not crash.
Plan the investigation: two builds, replayed load, adjusted timeouts, full instrumentation, and how to read ASan and TSan results together.
Decide when a timing-dependent crash justifies building and running permanent sanitizer load tests, given the slowdown and memory costs.
## Why this crash needs sanitizers A multithreaded server that crashes only under load, with a different stack each time, has the signature of a **timing-dependent bug**: two threads touching the same data without synchronization, or one thread freeing an object another still uses. A core dump shows where the damage was noticed, not where it was done. Sanitizers instrument the program so the bug is reported **at the bad access**, with stacks. ## Two builds, two questions | Build | Flags | Answers | |---|---|---| | Memory | `-fsanitize=address,undefined -g -fno-omit-frame-pointer -O1` | Is memory used after free or out of bounds? Any undefined operations? | | Races | `-fsanitize=thread -g -O1` | Do two threads access the same location without synchronization? | GCC's manual is explicit on the combinations: - `-fsanitize=address` "cannot be combined with `-fsanitize=thread` or `-fsanitize=hwaddress`". - `-fsanitize=thread` "cannot be combined with `-fsanitize=address`, `-fsanitize=leak`". - `-fsanitize=leak` "cannot be combined with `-fsanitize=thread`". UndefinedBehaviorSanitizer has no such restriction and can join either build. Apple clang 21 also refused the address and thread combination with an error, so the rule holds on both compilers. ## What ThreadSanitizer reports **ThreadSanitizer (TSan)** instruments memory accesses and synchronization calls. It reports a **data race**: two accesses to the same location from different threads, at least one a write, with no synchronization ordering them. The report gives both accesses with their stacks, where the memory lives, and where each thread was created. The key property for a flaky bug: TSan reports the race **on a run where nothing went wrong**. The example below increments a shared counter from four threads; on the Apple clang 21 run, TSan reported the write-write race at line 13 on the first execution, with nothing crashing. You do not need to reproduce the crash, only to execute the racy code. ## Making it work on a real server 1. **Replay realistic load** against the instrumented binary: the bug is only found on paths that run. 2. **Lower the rate or extend the run.** Clang's documentation puts TSan's typical slowdown at about 5x-15x and memory overhead at about 5x-10x; timeouts tuned for production will fire. 3. **Instrument everything you can.** Clang's TSan docs warn that code not compiled with `-fsanitize=thread`, such as pre-compiled shared libraries, can make it miss races or report false positives. 4. **Use `-g`**, which GCC's manual recommends for meaningful TSan output, and tune the run time with `TSAN_OPTIONS`. 5. **Fix the race, not the symptom.** Once TSan names the two accesses, make them synchronized with a mutex, an atomic or ownership transfer, then re-run until the report is gone. ## Reading the two results together - **TSan reports a race, ASan is clean:** the race may corrupt data without touching freed memory; fix the race. - **ASan reports use-after-free, TSan reports a race on the same object:** the race is the cause, the use-after-free is its effect. - **Both clean under load:** the bug may be on a path the replay did not exercise, or in code that was not instrumented; widen the test before concluding. ## Costs to plan for - ASan: Clang's documentation gives a typical slowdown of about 2x. - TSan: about 5x-15x slower and 5x-10x more memory. - Two builds double compile time in the investigation, but each answers a different question, and neither can replace the other.
- TSan is clean but the server still crashes under load; what next?Check coverage first: TSan only sees code paths that ran and code that was instrumented, so a prebuilt library or an unexercised path can hide the race. Then run the ASan and UBSan build under the same load, because a lifetime bug or undefined operation can crash without any data race.
- Can LeakSanitizer run in the TSan build?No. GCC's manual says `-fsanitize=leak` cannot be combined with `-fsanitize=thread`. Leak checking belongs in the AddressSanitizer build, where LeakSanitizer is part of the same run-time.
- Build once with -fsanitize=address,thread to catch everything
- TSan only reports a race when the run actually crashes
- If the sanitizer run cannot reproduce the crash, sanitizers are useless here
- Uninstrumented libraries never affect ThreadSanitizer results
- Running under the full production load rate is required for TSan