Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(diagnostics): keep the allocator walk off the background thread

Martin Vogel3c3f043247e6
diff --git a/src/foundation/diagnostics.c b/src/foundation/diagnostics.c
index b09578522..91a024a3c 100644
--- a/src/foundation/diagnostics.c
+++ b/src/foundation/diagnostics.c
@@ -497,7 +497,7 @@ static void write_diagnostics(void) {
diag_write_allocator_stats();
cbm_mem_map_t map;
- (void)cbm_mem_map_collect(&map);
+ (void)cbm_mem_map_collect_os(&map);
size_t residual =
map.os_committed_bytes > map.live_bytes ? map.os_committed_bytes - map.live_bytes : 0;
char buckets[CBM_SZ_512];
diff --git a/src/foundation/mem.c b/src/foundation/mem.c
index a1cf18b36..fddae7755 100644
--- a/src/foundation/mem.c
+++ b/src/foundation/mem.c
@@ -494,7 +494,17 @@ static bool mem_map_visit_area(const mi_heap_t *heap, const mi_heap_area_t *area
return true; /* keep walking */
}
+static bool mem_map_collect_impl(cbm_mem_map_t *out, bool walk_allocator);
+
+bool cbm_mem_map_collect_os(cbm_mem_map_t *out) {
+ return mem_map_collect_impl(out, false);
+}
+
bool cbm_mem_map_collect(cbm_mem_map_t *out) {
+ return mem_map_collect_impl(out, true);
+}
+
+static bool mem_map_collect_impl(cbm_mem_map_t *out, bool walk_allocator) {
if (!out) {
return false;
}
@@ -544,7 +554,9 @@ bool cbm_mem_map_collect(cbm_mem_map_t *out) {
* The cost is coverage -- other threads' live blocks and abandoned pages
* are not attributed -- and that is exactly what the residual in mem.h
* exists to carry. An unmeasured map must never read as an empty one. */
- (void)mi_theap_visit_blocks(mi_theap_get_default(), false, mem_map_visit_area, out);
+ if (walk_allocator) {
+ (void)mi_theap_visit_blocks(mi_theap_get_default(), false, mem_map_visit_area, out);
+ }
return true;
}
diff --git a/src/foundation/mem.h b/src/foundation/mem.h
index 38a290adb..a2ae37376 100644
--- a/src/foundation/mem.h
+++ b/src/foundation/mem.h
@@ -133,6 +133,18 @@ size_t cbm_mem_map_bucket_limit(int bucket);
* that the walk contributed nothing rather than reading 0 as "no leak". */
bool cbm_mem_map_collect(cbm_mem_map_t *out);
+/* OS totals only: fills os_rss_bytes / os_committed_bytes and leaves the
+ * allocator walk out entirely, so live_bytes stays 0 and the residual carries
+ * the whole process.
+ *
+ * Required for anything running on a background thread. Reading the
+ * allocator's per-thread bookkeeping (even just mi_theap_get_default) races a
+ * thread exiting concurrently, which rewrites it from _mi_thread_done ->
+ * _mi_theap_default_set; TSan reports that pairing directly. The walk is only
+ * safe where no other thread can be exiting -- tests and explicitly requested
+ * diagnostics -- so the periodic snapshot uses this instead. */
+bool cbm_mem_map_collect_os(cbm_mem_map_t *out);
+
/* ── Allocator ownership audit ───────────────────────────────────────
*
* "Does the allocator own our memory" is not one question, it is one per size