Skip to content

Commit 9e69cb7

Browse files
committed
fix(mem): free the thread locale at exit; a settling gate keeps its latch
Two CI-only lanes went red on 9724d90, both this branch's own doing. The macOS leak lane: every test passed and LeakSanitizer reported 16 to 101 objects of 1,472 bytes per test process, allocated from cbm_arena_sprintf. The formatter creates one C locale per thread on macOS (the vsnprintf locale lock fix) and kept it in a thread-local pointer only, so every worker thread that ever formatted a name leaked its locale at exit. A pthread key destructor now frees it when the thread exits; the main thread keeps its locale until process exit. The TSan lane on Ubuntu arm: pipeline_backpressure_futile_nap_disengages counted 8 nap cycles against a bound of 7, the "gate re-paid per pull" shape the test exists for. The spill shortcut reports "not over" while a sweep is still bringing memory down, and that reading re-armed the futility latch, so the next over-budget pull paid a full nap cycle again. The latch now re-arms only on a genuine under-budget reading; a reading the shortcut produced is not one. Verification: the local macOS leak lane (make -f Makefile.cbm test-lsan) reports zero leaks, 8050 passed / 10 skipped (the CI lane had 16-101 leaked objects per process); the local TSan lane (scripts/test.sh --tsan) passes 1157 / 9 skipped with the nap-cycle test green (the CI lane had it at 8 cycles); pipeline, parallel and arena suites twice and lint-ci on the fixed tree. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9724d90 commit 9e69cb7

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎src/foundation/arena.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum { ARENA_ALIGN = 7, ARENA_GROW_OK = 1 };
1818
#include <string.h>
1919
#include <stdarg.h>
2020
#if defined(__APPLE__)
21+
#include <pthread.h>
2122
#include <xlocale.h>
2223
#endif
2324
#include <stdio.h>
@@ -144,10 +145,30 @@ char *cbm_arena_strndup(CBMArena *a, const char *s, size_t len) {
144145
* object of its own, so no thread ever waits for another, and the common
145146
* short string is formatted once into a stack buffer. */
146147
#if defined(__APPLE__)
148+
/* One locale object per thread, freed when the thread exits: the object is
149+
* heap memory that the thread-local pointer alone kept, so every worker
150+
* thread that ever formatted a name leaked 1,472 bytes at exit (the macOS
151+
* LSan lane on PR #2202, 16-101 objects per test process). A pthread key
152+
* destructor is the one hook that runs at thread exit for a TLS-held
153+
* resource; the main thread keeps its locale until process exit. */
147154
static _Thread_local locale_t tl_c_locale;
155+
static pthread_key_t tl_c_locale_key;
156+
static pthread_once_t tl_c_locale_once = PTHREAD_ONCE_INIT;
157+
static void arena_c_locale_free(void *loc) {
158+
if (loc) {
159+
freelocale((locale_t)loc);
160+
}
161+
}
162+
static void arena_c_locale_key_init(void) {
163+
(void)pthread_key_create(&tl_c_locale_key, arena_c_locale_free);
164+
}
148165
static locale_t arena_c_locale(void) {
149166
if (!tl_c_locale) {
150167
tl_c_locale = newlocale(LC_ALL_MASK, "C", NULL);
168+
if (tl_c_locale) {
169+
pthread_once(&tl_c_locale_once, arena_c_locale_key_init);
170+
(void)pthread_setspecific(tl_c_locale_key, tl_c_locale);
171+
}
151172
}
152173
return tl_c_locale; /* NULL = the global locale, the pre-fix behaviour */
153174
}

‎src/pipeline/pass_parallel.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
10201020
pp_spill_enter(ec, "near_budget");
10211021
}
10221022
}
1023+
bool settling = false;
10231024
if (over) {
10241025
/* Admission control, first response: park what can be parked.
10251026
* Only what is still over budget after that -- the floor --
@@ -1030,6 +1031,7 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
10301031
over = cbm_mem_over_budget();
10311032
if (over && pp_spill_active(ec) && (parked > 0 || pp_spill_work_remains(ec))) {
10321033
over = false; /* memory is still on its way down */
1034+
settling = true;
10331035
}
10341036
}
10351037
bool futile = atomic_load_explicit(&ec->bp_futile, memory_order_relaxed) != 0;
@@ -1046,7 +1048,13 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
10461048
atomic_store_explicit(&ec->bp_futile, 0, memory_order_relaxed);
10471049
}
10481050
}
1049-
} else if (!over && futile) {
1051+
} else if (!over && !settling && futile) {
1052+
/* Re-arm only on a genuine under-budget reading. A reading the
1053+
* spill shortcut produced ("still on its way down") is not
1054+
* one: re-arming on it made the next over-budget pull pay a
1055+
* full nap cycle again -- the gate re-paid per pull that
1056+
* pipeline_backpressure_futile_nap_disengages guards against
1057+
* (TSan lane on PR #2202: 8 cycles against a bound of 7). */
10501058
atomic_store_explicit(&ec->bp_futile, 0, memory_order_relaxed);
10511059
}
10521060
}

0 commit comments

Comments
 (0)