Skip to content

Commit 9724d90

Browse files
committed
fix(mem): dedicated SQLite heap only in the worker; guard the committed stat
The daemon leaked 180 KB per query. The Linux soak's quick leg went from 11 to 144 MB of RSS in ten minutes (13.1x, 628 MB/hr), mimalloc's committed bytes tracking RSS almost byte for byte and the growth linear in the query count, file descriptors flat -- where the same leg had been flat at 15 MB in July. The cause is the previous commit's SQLite heap: one mimalloc heap per thread, created on first use. The daemon runs a thread per connection, so every query ran on a fresh thread with a fresh heap, and every block the shared connection keeps beyond the request -- page cache pages, the statement cache, schema objects -- was allocated in that heap's pages. When the thread exited, its heap was deleted and those pages were left behind holding a few live blocks each: committed for as long as the connection lives, a few pages per query, never reused by anyone. The dedicated heap exists for one reason: the index worker's default heap holds the graph (40M+ blocks), and SQLite churn on that heap paid a page walk per allocation during the coverage publish (132 s vs 9.7 s on the kernel). That process has long-lived threads and exits when the index is done. So the dedicated heap is now a switch, off by default, that the index worker turns on at its entry (main.c, --index-worker); everywhere else SQLite allocates from the calling thread's default heap exactly as before the previous commit. The switch exists in every build and changes nothing where the allocator binds are compiled out. Linux soak, quick leg, before -> after: RSS 11 -> 144 MB in ten minutes (13.1x, 628 MB/hr) -> 10.6 -> 11.5 MB and flat from the first minute over 564 queries with reindexes every two minutes; query-leak leg: 11.6 MB flat over 1,376 queries; committed heap 6.8 MB flat. The same soak showed a second thing. From its second sample on, the query-leak leg reported a committed heap of 2^64 - 121 MB: mimalloc's committed statistic is a signed counter merged per thread at thread exit, and a process whose long-lived thread commits what its short-lived threads free reads it negative -- cast to size_t on the way out. That figure is one of the two the budget takes the larger of. cbm_mem_allocator_committed now reports 0 for a reading above SIZE_MAX/2, so the charge falls back to the OS number instead of a 16 EB budget breach, and the diagnostics report the committed pair through it (the peak never undercuts the current value and never carries a wrapped one), the way they already did for RSS. Detector: the soak's quick and query-leak legs (scripts/soak-legs.sh), the release-gating sequence in every venue -- RED on the previous tree (13.1x), GREEN on this one. A unit test cannot see this class: the test build keeps SQLite on libc so the sanitizers see every block. Signed-off-by: Martin Vogel <[email protected]>
1 parent a2f29b3 commit 9724d90

5 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,20 @@ static TSParser *get_thread_parser(const TSLanguage *ts_lang, CBMLanguage lang)
300300
* to mimalloc would mismatch ASan/CRT frees — there these binds compile to
301301
* no-ops and the build stays unchanged. */
302302

303+
/* SQLite on a dedicated mimalloc heap per thread: ON only in the index worker,
304+
* whose default heap holds the graph (SQLite churn on that heap paid a page
305+
* walk per allocation: 132 s vs 9.7 s on the kernel's coverage publish). OFF
306+
* everywhere else: the daemon runs a thread per connection, and a heap
307+
* created per such thread pins every SQLite block the shared connection
308+
* keeps (page cache, statement cache) to pages nobody's heap owns any more --
309+
* the Linux soak grew 180 KB per query, 11 -> 144 MB in ten minutes, where
310+
* the default thread heap had been flat (2026-09-14). */
311+
static _Atomic int g_sqlite_dedicated_heap;
312+
313+
void cbm_sqlite_dedicated_heap(bool on) {
314+
atomic_store_explicit(&g_sqlite_dedicated_heap, on ? 1 : 0, memory_order_relaxed);
315+
}
316+
303317
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
304318
#include <assert.h>
305319

@@ -322,15 +336,20 @@ static TSParser *get_thread_parser(const TSLanguage *ts_lang, CBMLanguage lang)
322336
* thread's heap is released with the thread. */
323337
static _Thread_local mi_heap_t *tl_sqlite_heap;
324338

339+
/* NULL = the calling thread's default heap (mi_malloc); see the switch above. */
325340
static mi_heap_t *sqlite_heap(void) {
341+
if (!atomic_load_explicit(&g_sqlite_dedicated_heap, memory_order_relaxed)) {
342+
return NULL;
343+
}
326344
if (!tl_sqlite_heap) {
327345
tl_sqlite_heap = mi_heap_new();
328346
}
329-
return tl_sqlite_heap ? tl_sqlite_heap : mi_heap_main();
347+
return tl_sqlite_heap;
330348
}
331349

332350
static void *cbm_sqlite_malloc(int n) {
333-
void *block = mi_heap_malloc(sqlite_heap(), (size_t)n);
351+
mi_heap_t *heap = sqlite_heap();
352+
void *block = heap ? mi_heap_malloc(heap, (size_t)n) : mi_malloc((size_t)n);
334353
if (block) {
335354
cbm_mem_class_add_external(CBM_MEM_CLASS_STORE, mi_usable_size(block));
336355
}
@@ -344,7 +363,8 @@ static void cbm_sqlite_free(void *p) {
344363
}
345364
static void *cbm_sqlite_realloc(void *p, int n) {
346365
size_t old_size = p ? mi_usable_size(p) : 0;
347-
void *grown = mi_heap_realloc(sqlite_heap(), p, (size_t)n);
366+
mi_heap_t *heap = sqlite_heap();
367+
void *grown = heap ? mi_heap_realloc(heap, p, (size_t)n) : mi_realloc(p, (size_t)n);
348368
if (grown) {
349369
cbm_mem_class_remove_external(CBM_MEM_CLASS_STORE, old_size);
350370
cbm_mem_class_add_external(CBM_MEM_CLASS_STORE, mi_usable_size(grown));

‎internal/cbm/cbm.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,12 @@ typedef struct {
669669
// also calls it so non-main entry points (pipeline passes) still get the binds.
670670
// In the test build (no CBM_BIND_TS_ALLOCATOR) this is a no-op.
671671
void cbm_alloc_init(void);
672+
/* SQLite allocates from a dedicated mimalloc heap per thread while on; the
673+
* index worker turns it on (its default heap holds the graph). Off elsewhere:
674+
* a thread-per-connection daemon would pin connection-lifetime blocks to
675+
* dead threads. The switch exists in every build; it changes nothing where
676+
* the allocator binds are compiled out. */
677+
void cbm_sqlite_dedicated_heap(bool on);
672678

673679
// Initialize the library. Call once at startup. Returns 0 on success.
674680
int cbm_init(void);

‎src/foundation/diagnostics.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <mimalloc.h>
2424
#include <stdatomic.h>
2525
#include <stdio.h>
26+
#include <stdint.h>
2627
#include <stdlib.h>
2728
#include <string.h>
2829
#include <time.h>
@@ -538,6 +539,20 @@ static void write_diagnostics(void) {
538539
* macOS/Windows, and peak is reconciled to never undercut current. */
539540
current_rss = cbm_mem_rss();
540541
peak_rss = cbm_mem_peak_rss();
542+
/* The committed pair has the same failure: the counter is signed and
543+
* merged per thread at thread exit, so a thread-per-connection daemon
544+
* reads it negative once short-lived threads have freed what a long-lived
545+
* one committed (query-leak soak, Linux 2026-09-14: heap_committed_bytes
546+
* printed 2^64 - 121 MB from the second sample on). cbm_mem_allocator_
547+
* committed reports 0 for a negative reading; the peak never undercuts
548+
* the current value and never carries a wrapped one. */
549+
current_commit = cbm_mem_allocator_committed();
550+
if (peak_commit > (SIZE_MAX >> 1)) {
551+
peak_commit = 0;
552+
}
553+
if (peak_commit < current_commit) {
554+
peak_commit = current_commit;
555+
}
541556

542557
int fds = count_open_fds();
543558
time_t now = time(NULL);

‎src/foundation/mem.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,15 @@ void cbm_mem_set_budget_for_tests(size_t bytes) {
515515
size_t cbm_mem_allocator_committed(void) {
516516
size_t commit = 0;
517517
mi_process_info(NULL, NULL, NULL, NULL, NULL, &commit, NULL, NULL);
518+
/* The statistic behind this is a signed counter merged per thread at
519+
* thread exit; a process whose long-lived thread commits what its
520+
* short-lived threads free reads it NEGATIVE, cast to size_t here. The
521+
* daemon's query-leak soak reported 2^64 - 121 MB from the second sample
522+
* on (Linux, 2026-09-14). A negative reading is no reading: report 0 so
523+
* the charge falls back to the OS number instead of a 16 EB budget breach. */
524+
if (commit > (SIZE_MAX >> 1)) {
525+
return 0;
526+
}
518527
return commit;
519528
}
520529

‎src/main.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,11 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
854854
const char *worker_marker = cli_strip_flag_value(&argc, argv, CBM_INDEX_WORKER_MARKER_ARG);
855855
const char *worker_quarantine =
856856
cli_strip_flag_value(&argc, argv, CBM_INDEX_WORKER_QUARANTINE_ARG);
857+
if (index_worker) {
858+
/* The graph lives on this process's heaps: SQLite gets heaps of its
859+
* own here, and only here (cbm_sqlite_dedicated_heap). */
860+
cbm_sqlite_dedicated_heap(true);
861+
}
857862
cbm_index_set_worker_role_options(index_worker, response_out, worker_single_thread,
858863
worker_marker, worker_quarantine,
859864
cbm_index_worker_memory_budget_bytes());

0 commit comments

Comments
 (0)