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 stability: SQLite lock race, stale SHM recovery, OOM guard, CPU reduction

Martin Vogelfac0c5735993
diff --git a/cmd/codebase-memory-mcp/main.go b/cmd/codebase-memory-mcp/main.go
index 51b40640c..2aa4d2434 100644
--- a/cmd/codebase-memory-mcp/main.go
+++ b/cmd/codebase-memory-mcp/main.go
@@ -67,13 +67,14 @@ func main() {
log.Fatalf("config err=%v", err)
}
- // Apply GOMEMLIMIT from config (e.g. "2G", "512M").
- if memLimit := cfg.Get(store.ConfigMemLimit, ""); memLimit != "" {
- if limit, parseErr := parseByteSize(memLimit); parseErr != nil {
- log.Printf("config: invalid mem_limit %q: %v", memLimit, parseErr)
- } else {
- debug.SetMemoryLimit(limit)
- }
+ // Apply GOMEMLIMIT from config (e.g. "2G", "512M"), default 2GB.
+ // Prevents OOM kills on systems without user-configured limits.
+ memLimitStr := cfg.Get(store.ConfigMemLimit, "2G")
+ if limit, parseErr := parseByteSize(memLimitStr); parseErr != nil {
+ log.Printf("config: invalid mem_limit %q: %v", memLimitStr, parseErr)
+ debug.SetMemoryLimit(2 << 30) // fallback: 2GB
+ } else {
+ debug.SetMemoryLimit(limit)
}
srv := tools.NewServer(router, tools.WithConfig(cfg))
diff --git a/internal/pipeline/pipeline.go b/internal/pipeline/pipeline.go
index c4ba68e43..0e5efc2a1 100644
--- a/internal/pipeline/pipeline.go
+++ b/internal/pipeline/pipeline.go
@@ -73,6 +73,11 @@ func ProjectNameFromPath(absPath string) string {
// Clean and normalize separators (backslash is not a separator on non-Windows)
cleaned := filepath.ToSlash(filepath.Clean(absPath))
cleaned = strings.ReplaceAll(cleaned, "\\", "/")
+ // Normalize Windows drive letter casing: "D:/foo" → "d:/foo"
+ // Prevents duplicate DBs for same path with different drive letter case.
+ if len(cleaned) >= 2 && cleaned[1] == ':' {
+ cleaned = strings.ToLower(cleaned[:1]) + cleaned[1:]
+ }
// Replace slashes and colons with dashes
name := strings.ReplaceAll(cleaned, "/", "-")
name = strings.ReplaceAll(name, ":", "-")
diff --git a/internal/pipeline/pipeline_test.go b/internal/pipeline/pipeline_test.go
index 96a4a4699..a85ac69fa 100644
--- a/internal/pipeline/pipeline_test.go
+++ b/internal/pipeline/pipeline_test.go
@@ -793,10 +793,10 @@ func TestProjectNameFromPath(t *testing.T) {
{"/Users/martin/projects/myapp", "Users-martin-projects-myapp"},
{"/home/user/repo", "home-user-repo"},
{"/single", "single"},
- // Windows paths (#20)
- {"C:/Users/project", "C-Users-project"},
- {"D:\\Projects\\myapp", "D-Projects-myapp"},
- {"C:\\Temp\\codebase-memory-mcp", "C-Temp-codebase-memory-mcp"},
+ // Windows paths (#20) — drive letter normalized to lowercase (#50)
+ {"C:/Users/project", "c-Users-project"},
+ {"D:\\Projects\\myapp", "d-Projects-myapp"},
+ {"C:\\Temp\\codebase-memory-mcp", "c-Temp-codebase-memory-mcp"},
}
for _, tt := range tests {
got := ProjectNameFromPath(tt.path)
diff --git a/internal/store/router.go b/internal/store/router.go
index 9b42953aa..881aeed48 100644
--- a/internal/store/router.go
+++ b/internal/store/router.go
@@ -194,6 +194,8 @@ func (r *StoreRouter) AllStores() map[string]*Store {
}
// ListProjects scans .db files and queries each for metadata.
+// Uses AcquireStore to prevent the evictor from closing stores mid-query.
+// Individual DB failures are logged and skipped (never block the full list).
func (r *StoreRouter) ListProjects() ([]*ProjectInfo, error) {
entries, err := os.ReadDir(r.dir)
if err != nil {
@@ -214,10 +216,12 @@ func (r *StoreRouter) ListProjects() ([]*ProjectInfo, error) {
DBPath: filepath.Join(r.dir, e.Name()),
}
- // Try to get root_path from the projects table
- s, err := r.ForProject(name)
- if err == nil {
+ // Try to get root_path from the projects table.
+ // AcquireStore increments refs so the evictor can't close mid-query.
+ s, release, acqErr := r.AcquireStore(name)
+ if acqErr == nil {
projects, listErr := s.ListProjects()
+ release()
if listErr == nil && len(projects) > 0 {
info.RootPath = projects[0].RootPath
}
diff --git a/internal/store/store.go b/internal/store/store.go
index b6d80d76a..3ac0244ba 100644
--- a/internal/store/store.go
+++ b/internal/store/store.go
@@ -81,8 +81,12 @@ func OpenInDir(dir, project string) (*Store, error) {
// OpenPath opens a SQLite database at the given path.
func OpenPath(dbPath string) (*Store, error) {
+ // Recover from stale SHM left by SIGKILL: if WAL is empty/missing but SHM exists,
+ // the SHM has stale lock state that can deadlock new connections. Safe to remove.
+ recoverStaleSHM(dbPath)
+
dsn := dbPath + "?_journal_mode=WAL" +
- "&_busy_timeout=5000" +
+ "&_busy_timeout=10000" +
"&_foreign_keys=1" +
"&_synchronous=NORMAL" +
"&_txlock=immediate"
@@ -97,7 +101,7 @@ func OpenPath(dbPath string) (*Store, error) {
// PRAGMAs not supported in mattn DSN — set via Exec after Open.
ctx := context.Background()
_, _ = db.ExecContext(ctx, "PRAGMA temp_store = MEMORY")
- _, _ = db.ExecContext(ctx, "PRAGMA mmap_size = 268435456") // 256 MB
+ _, _ = db.ExecContext(ctx, "PRAGMA mmap_size = 67108864") // 64 MB
// Adaptive cache: 10% of DB file size, clamped to 2-64 MB.
cacheMB := adaptiveCacheMB(dbPath)
@@ -114,6 +118,32 @@ func OpenPath(dbPath string) (*Store, error) {
return s, nil
}
+// recoverStaleSHM removes stale SHM files left by unclean shutdowns (SIGKILL).
+// If the WAL file is empty or missing but the SHM file exists, the SHM contains
+// stale lock state that can deadlock new connections. Removing it lets SQLite
+// rebuild clean shared memory on next open.
+func recoverStaleSHM(dbPath string) {
+ shmPath := dbPath + "-shm"
+ walPath := dbPath + "-wal"
+
+ shmInfo, shmErr := os.Stat(shmPath)
+ if shmErr != nil {
+ return // no SHM file — nothing to recover
+ }
+
+ walInfo, walErr := os.Stat(walPath)
+ walEmpty := walErr != nil || walInfo.Size() == 0
+
+ if walEmpty && shmInfo.Size() > 0 {
+ slog.Info("store.recover_shm", "path", dbPath, "shm_bytes", shmInfo.Size())
+ _ = os.Remove(shmPath)
+ // Also remove empty WAL to let SQLite start fresh
+ if walErr == nil {
+ _ = os.Remove(walPath)
+ }
+ }
+}
+
// adaptiveCacheMB returns a cache size in MB proportional to the DB file size.
// 10% of DB size, clamped to [2, 64] MB. Returns 2 for missing/small files.
func adaptiveCacheMB(dbPath string) int {
diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go
index 4dce88b8f..a8e5fdf97 100644
--- a/internal/watcher/watcher.go
+++ b/internal/watcher/watcher.go
@@ -42,7 +42,7 @@ func (s watchStrategy) String() string {
}
const (
- baseInterval = 1 * time.Second
+ baseInterval = 5 * time.Second
maxInterval = 60 * time.Second
fullSnapshotInterval = 5 // polls between forced full snapshots
projectsCacheTTL = 60 * time.Second
@@ -185,25 +185,27 @@ func (w *Watcher) pollAll() {
now := time.Now()
for _, info := range projectInfos {
- st, stErr := w.router.ForProject(info.Name)
+ state, exists := w.projects[info.Name]
+ if exists && now.Before(state.nextPoll) {
+ continue // not due yet
+ }
+
+ // AcquireStore increments refs so the evictor can't close mid-query.
+ st, release, stErr := w.router.AcquireStore(info.Name)
if stErr != nil {
continue
}
proj, projErr := st.GetProject(info.Name)
+ release()
if projErr != nil || proj == nil {
continue
}
- state, exists := w.projects[info.Name]
if !exists {
state = &projectState{}
w.projects[info.Name] = state
}
- if exists && now.Before(state.nextPoll) {
- continue // not due yet
- }
-
w.pollProject(proj, state)
}
}
@@ -579,9 +581,9 @@ func snapshotsEqual(a, b map[string]fileSnapshot) bool {
}
// pollInterval computes the adaptive interval from file count.
-// 1s base + 1s per 500 files, capped at 60s.
+// 5s base + 1s per 500 files, capped at 60s.
func pollInterval(fileCount int) time.Duration {
- ms := 1000 + (fileCount/500)*1000
+ ms := 5000 + (fileCount/500)*1000
if ms > 60000 {
ms = 60000
}
diff --git a/internal/watcher/watcher_test.go b/internal/watcher/watcher_test.go
index c11a3f2f1..7aec630fe 100644
--- a/internal/watcher/watcher_test.go
+++ b/internal/watcher/watcher_test.go
@@ -163,13 +163,13 @@ func TestPollInterval(t *testing.T) {
files int
expected time.Duration
}{
- {0, 1 * time.Second},
- {70, 1 * time.Second},
- {499, 1 * time.Second},
- {500, 2 * time.Second},
- {2000, 5 * time.Second},
- {5000, 11 * time.Second},
- {10000, 21 * time.Second},
+ {0, 5 * time.Second},
+ {70, 5 * time.Second},
+ {499, 5 * time.Second},
+ {500, 6 * time.Second},
+ {2000, 9 * time.Second},
+ {5000, 15 * time.Second},
+ {10000, 25 * time.Second},
{50000, 60 * time.Second},
{100000, 60 * time.Second},
}