Found with ThreadSanitizer over the whole suite, not by reading:
- ack: sp_ack_dir() returned one static buffer. The waiter and the
answerer (the gateway is both) sprintf'd into each other's path, so an
approval request could be looked for in the wrong place. One buffer per
thread now; test_ack asserts the two threads get different addresses.
- mcp: `stopping` was a plain volatile int with no happens-before, so a
server thread could read 0 after shutdown killed its child and keep
using buffers that teardown frees. Acquire/release accessors.
- foreign: fr.ready was set before the paths it guards were filled, so a
second thread returned early and read empty paths; the flag and the host
handle are now an atomic pair (publish) plus a mutex (the init itself).
- embed: catch_up() read c->path, c->offset and c->id outside the lock. A
session switch mid-index published the old file's byte offset into the
new session's index - everything below that offset was never searched
again. A generation counter makes that run drop its batch, and the
search path takes its snapshot under the same lock.
Proof, both directions:
- make check-tsan: 45/45 test binaries free of races (new target; the
tree is rebuilt under TSan and back, like check-asan).
- test_embed's new gated test (a server that holds the batch until the
session has been switched): without the generation check short.emb ends
with 4 records, 1 of them leaked from the other session; with it, 1
and 0. The test is deterministic, so it fails on a plain build too.
- test_ack: reverting to `static char root[]` fails
"the two threads do not share one buffer".
- make check: all tests passed; make check-asan: 45/45 clean.