MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

SQLite mutex crash on concurrent initialization — missing busy_timeout in WAL mode

Open xxjj1111 opened this issue 8 months ago • 3 comments

Environment

  • Package: @memtensor/memos-local-openclaw-plugin v1.0.7
  • Node: v22.22.0 / better-sqlite3 v12.8.0
  • OS: macOS (arm64)

Problem

When the OpenClaw gateway restarts, the memos plugin is initialized concurrently by multiple workers. Without a busy_timeout, SQLite's WAL mode throws a native mutex crash instead of waiting for the lock to be released:

libc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument

This causes the entire gateway process to crash repeatedly on restart.

Root Cause

src/storage/sqlite.ts sets journal_mode = WAL but does not set busy_timeout. When two or more workers try to open the same database simultaneously, better-sqlite3 crashes instead of queuing.

Fix

Add one line after journal_mode = WAL:

this.db.pragma("journal_mode = WAL");
this.db.pragma("busy_timeout = 10000"); // wait up to 10s instead of crashing
this.db.pragma("foreign_keys = ON");

Verified — applied this patch locally, concurrent restarts no longer crash.

xxjj1111 avatar Mar 25 '26 19:03 xxjj1111

Root Cause Analysis & Resolution

Confirmed this is not a memos-local / better-sqlite3 bug — it's triggered by unstable pipeline LLM API connections. Closing.

Setup

3 OpenClaw bots on one machine, each with its own memos-local instance. The memos pipeline (summarize/dedup/filter) calls external LLMs ~600-700 times/day via imageModel. Pipeline model stability is critical to bot operation.

Problem

Both bots crashed 10-40+ times/day with mutex lock failed: Invalid argument. Watchdog auto-restart only masked the issue.

What Didn't Work

  • Singleton connection pool, busy_timeout=10000, WAL mode, MALLOC_NANO_ZONE=0 — none stopped the crashes
  • Fixing retired proxy ports — reduced but didn't eliminate crashes

Root Cause

Pipeline LLM API instability + multi-level fallback cascade:

Pipeline API fails (429/timeout)
→ Fallback #2 fails → Fallback #3 fails
→ Rapid error callback burst → better-sqlite3 native mutex corrupted → SIGABRT

We used free-tier APIs with strict rate limits. Our 3-level fallback config meant one failure cascaded into 3-4 near-simultaneous error callbacks, corrupting the native mutex.

Fix

  1. Switched to stable free APIs (Qwen OAuth primary + SiliconFlow Qwen2.5-7B fallback)
  2. Reduced fallback to 1 level — skip pipeline cycle on failure instead of cascading. Skipping is safe (compaction safeguard handles it)
  3. Fixed retired proxy ports

Result: zero crashes on both bots after fix.

Suggestion for memos-local

  • Consider debouncing/rate-limiting pipeline error callbacks to prevent storm patterns
  • Try-catch at the better-sqlite3 native binding boundary could gracefully handle error bursts

Thanks for the great plugin 🙏

xxjj1111 avatar Mar 27 '26 06:03 xxjj1111

Closing — root cause was our pipeline API instability, not a memos-local bug. See comment above for full analysis.

xxjj1111 avatar Mar 27 '26 06:03 xxjj1111

Hey @xxjj1111! Thanks for the contribution — already solved the issue, thanks for helping us to hunt the bug! 🐞

Btw if you enjoyed contributing and want to stay connected with the project, we run a Discord server for contributors, a chill space to discuss features, ask questions, or pick up new issues  👉🏻 MemOS Discord Server (Contributor Exclusive)

Thanks again for shipping this, and happy MemOSing 🧠✨

angarchie avatar May 13 '26 12:05 angarchie