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(store): install the SQLite page-cache slab before SQLite initialises

Martin Vogel5929f6a2e617
diff --git a/src/main.c b/src/main.c
index 33b5a1e96..de98e2f35 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,8 @@
* per-account daemon. One-shot CLI tool calls run in an isolated local server
* and never create or retain a daemon generation.
*/
-#include "cbm.h" // cbm_alloc_init — bind 3rd-party allocators to mimalloc before any sqlite/git init
+#include "cbm.h"
+#include "store/store.h" // cbm_alloc_init — bind 3rd-party allocators to mimalloc before any sqlite/git init
#include "daemon/application.h"
#include "daemon/bootstrap.h"
#include "daemon/frontend.h"
@@ -1705,6 +1706,9 @@ static int main_run_daemon_ctl(int argc, char **argv, const cbm_daemon_ipc_endpo
int main(int argc, char **argv) {
/* Must remain the first statement: see allocator binding contract above. */
cbm_alloc_init();
+ /* Immediately after, and before anything can touch SQLite: the page-cache
+ * slab can only be installed ahead of sqlite3_initialize (#581). */
+ cbm_store_configure_pagecache();
#ifndef _WIN32
pid_t process_initial_ppid = getppid();
#endif
diff --git a/src/store/store.c b/src/store/store.c
index f70bf0f33..81e874d2d 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -736,7 +736,11 @@ enum {
STORE_PAGECACHE_SLOTS = 2048, /* ~8.7 MiB, shared by all connections */
};
-static void store_configure_shared_pagecache(void) {
+/* Must run before ANY sqlite3 call: SQLITE_CONFIG_PAGECACHE is refused once
+ * sqlite3_initialize has run, and being refused is what left every request's
+ * page cache coming from the general allocator (#581). Idempotent, so the
+ * open paths can keep calling it as a backstop. */
+void cbm_store_configure_pagecache(void) {
static atomic_int configured = ATOMIC_VAR_INIT(0);
int expected = 0;
if (!atomic_compare_exchange_strong(&configured, &expected, 1)) {
@@ -757,12 +761,12 @@ static void store_configure_shared_pagecache(void) {
}
cbm_store_t *cbm_store_open_memory(void) {
- store_configure_shared_pagecache();
+ cbm_store_configure_pagecache();
return store_open_internal(":memory:", true, true);
}
cbm_store_t *cbm_store_open_path(const char *db_path) {
- store_configure_shared_pagecache();
+ cbm_store_configure_pagecache();
if (!db_path) {
return NULL;
}
@@ -770,7 +774,7 @@ cbm_store_t *cbm_store_open_path(const char *db_path) {
}
cbm_store_t *cbm_store_open_path_existing(const char *db_path) {
- store_configure_shared_pagecache();
+ cbm_store_configure_pagecache();
if (!db_path) {
return NULL;
}
@@ -841,7 +845,7 @@ static bool build_immutable_uri(const char *path, char *out, size_t out_sz) {
}
cbm_store_t *cbm_store_open_path_query(const char *db_path) {
- store_configure_shared_pagecache();
+ cbm_store_configure_pagecache();
if (!db_path) {
return NULL;
}
diff --git a/src/store/store.h b/src/store/store.h
index 55be4a48b..41f15e6c5 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -208,6 +208,13 @@ typedef struct {
/* ── Lifecycle ──────────────────────────────────────────────────── */
/* Open an in-memory database (for testing). */
+/* Install the process-wide SQLite page-cache slab. Call once, as early in
+ * process startup as possible — before any store is opened. SQLite refuses the
+ * configuration after it initialises itself, and the fallback (a fresh page
+ * cache per connection, from the general allocator) is what made a long-lived
+ * Windows daemon ratchet committed memory (#581). Safe to call repeatedly. */
+void cbm_store_configure_pagecache(void);
+
cbm_store_t *cbm_store_open_memory(void);
/* Open a file-backed database at the given path. Creates if needed. */