Skip to content

Commit 9db4834

Browse files
committed
fix(mem,spill): close two data races the worker threads exposed
TSan failed on all three platforms in PR CI. Both races are in code this branch added, and both were invisible to the local ladder because it has no TSan leg — that lane is CI-only, so "macOS and Linux green" said nothing about either. 1. system_info.c: the cached system info cbm_system_info() published its cache with a plain flag: if (!info_cached) { cached_info = detect_...(); info_cached = 1; } Fine while only the main thread asked. The memory-relief work started calling cbm_mem_system_under_pressure() from the per-file extract loop, so several workers now race on the flag AND on the struct — and a struct assignment is not atomic, so a reader could see half of one. Reported at system_info.c:284 against :274, from extract_worker, on macOS, ubuntu-arm and ubuntu-latest alike. Now a three-state atomic: whichever thread wins the compare-exchange is the only writer of cached_info, a loser returns the copy it detected itself (the same answer — detection only reads the OS), and later readers see the published struct through the acquire/release pair. No mutex, so nothing has to be initialised before first use. 2. pass_parallel.c: the spill pointer pp_spill_active() tested ec->pctx->spill BEFORE its atomic spill_mode gate, so it read an 8-byte pointer with no synchronisation while pp_spill_enter was publishing that pointer under spill_mu. pp_spill_enter assigns the pointer and only then release-stores spill_mode, so gating on the atomic first makes the pointer read ordered by that release. Short-circuit order is load bearing in that expression now, and the comment says so. Found by running the TSan lane locally with halt_on_error=0 rather than CI's halt_on_error=1: the first setting stops at the first race, so each fix would have revealed the next one a CI round trip later. Enumerating instead showed the second race was the last one in that suite. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9db5217 commit 9db4834

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

‎src/foundation/system_info.c‎

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ enum { DEFAULT_CORES = 1, MIN_WORKERS = 1, CBM_WORKERS_MAX = 256 };
1717
#include "foundation/mem_core.h" /* cbm_alloc: one accounted allocation path */
1818
#include "foundation/platform.h"
1919
#include "foundation/system_info_internal.h"
20-
#include <stdint.h> // uint64_t
21-
#include <stdlib.h> // strtol
20+
#include <stdatomic.h> // the system-info cache is published across worker threads
21+
#include <stdint.h> // uint64_t
22+
#include <stdlib.h> // strtol
2223
#include <string.h>
2324

2425
#ifndef _WIN32
@@ -267,23 +268,51 @@ static cbm_system_info_t detect_system_windows(void) {
267268

268269
/* ── Public API ──────────────────────────────────────────────────── */
269270

270-
static int info_cached = 0;
271+
/* The cache is published once, by whichever thread gets there first.
272+
*
273+
* This used to be a plain `if (!info_cached) { cached_info = detect(); }`, which
274+
* was fine while only the main thread asked. It stopped being fine when the
275+
* memory-relief path started calling cbm_mem_system_under_pressure() from the
276+
* extract workers: several threads then raced on the flag AND on the struct,
277+
* and a struct assignment is not atomic, so a reader could see half of one.
278+
* TSan caught it on all three platforms (system_info.c:284 against :274, from
279+
* extract_worker).
280+
*
281+
* Three states rather than a flag, so the winner of the compare-exchange is the
282+
* only writer of cached_info: a loser returns the copy it detected itself,
283+
* which is the same answer, and a later reader sees the published struct
284+
* through the acquire/release pair. No mutex, so nothing has to be initialised
285+
* before first use, and no reader can observe a half-written struct. */
286+
enum { INFO_EMPTY = 0, INFO_CLAIMED = 1, INFO_READY = 2 };
287+
static _Atomic int info_state = INFO_EMPTY;
271288
static cbm_system_info_t cached_info;
272289

273-
cbm_system_info_t cbm_system_info(void) {
274-
if (!info_cached) {
290+
static cbm_system_info_t detect_system_now(void) {
275291
#ifdef _WIN32
276-
cached_info = detect_system_windows();
292+
return detect_system_windows();
277293
#elif defined(__APPLE__)
278-
cached_info = detect_system_macos();
294+
return detect_system_macos();
279295
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
280-
cached_info = detect_system_bsd();
296+
return detect_system_bsd();
281297
#else
282-
cached_info = detect_system_linux();
298+
return detect_system_linux();
283299
#endif
284-
info_cached = SKIP_ONE;
300+
}
301+
302+
cbm_system_info_t cbm_system_info(void) {
303+
if (atomic_load_explicit(&info_state, memory_order_acquire) == INFO_READY) {
304+
return cached_info;
305+
}
306+
/* Detection only reads the OS (sysconf/sysctl/GetSystemInfo), so racing
307+
* threads doing it twice during startup costs a little and changes nothing. */
308+
cbm_system_info_t local = detect_system_now();
309+
int expected = INFO_EMPTY;
310+
if (atomic_compare_exchange_strong_explicit(&info_state, &expected, INFO_CLAIMED,
311+
memory_order_acq_rel, memory_order_relaxed)) {
312+
cached_info = local;
313+
atomic_store_explicit(&info_state, INFO_READY, memory_order_release);
285314
}
286-
return cached_info;
315+
return local;
287316
}
288317

289318
int cbm_default_worker_count(bool initial) {

‎src/pipeline/pass_parallel.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,16 @@ static void pp_spill_enter(extract_ctx_t *ec, const char *reason) {
832832
}
833833

834834
static bool pp_spill_active(const extract_ctx_t *ec) {
835-
return ec->pctx && ec->pctx->spill &&
836-
atomic_load_explicit(&ec->pctx->spill_mode, memory_order_acquire) != 0;
835+
/* The atomic gates the pointer, not the other way round. pp_spill_enter
836+
* assigns ec->pctx->spill under spill_mu and only THEN release-stores
837+
* spill_mode, so a reader that has acquired a non-zero spill_mode is
838+
* guaranteed to see the finished pointer. Testing the pointer first read it
839+
* with no synchronisation at all while another worker was publishing it —
840+
* a genuine data race on an 8-byte write, which TSan caught at
841+
* pass_parallel.c:817 against this line. Short-circuit order is load
842+
* bearing here; do not reorder these terms. */
843+
return ec->pctx && atomic_load_explicit(&ec->pctx->spill_mode, memory_order_acquire) != 0 &&
844+
ec->pctx->spill;
837845
}
838846

839847
CBMFileResult *cbm_pipeline_result_acquire(const cbm_pipeline_ctx_t *ctx, CBMFileResult **cache,

0 commit comments

Comments
 (0)