claude-flow icon indicating copy to clipboard operation
claude-flow copied to clipboard

FATAL OOM → better-sqlite3 “database connection is not open” at session-manager.js:642 (macOS ARM, Node 23.3.0)

Open syntaxsurge opened this issue 4 months ago • 3 comments

Summary

Running claude-flow hits a V8 out-of-memory error. During shutdown, the app attempts to log a session event and better-sqlite3 throws TypeError: The database connection is not open (likely after the DB handle is closed or never opened due to the crash).

Error output

<--- Last few GCs --->

[44225:0x118008000]   253638 ms: Scavenge (reduce) (interleaved) 4088.3 (4099.4) -> 4085.9 (4094.1) MB, pooled: 0 MB, 4.79 / 0.00 ms  (average mu = 0.200, current mu = 0.193) allocation failure; 
[44225:0x118008000]   254365 ms: Mark-Compact (reduce) 4086.2 (4094.1) -> 4085.3 (4093.9) MB, pooled: 0 MB, 709.54 / 0.00 ms  (+ 152.4 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 881 ms) (average mu = 0.165, 

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----

 1: 0x10456f768 node::OOMErrorHandler(char const*, v8::OOMDetails const&) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 2: 0x104717b64 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 3: 0x104717b18 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 4: 0x1048d5638 v8::internal::Heap::CallGCPrologueCallbacks(v8::GCType, v8::GCCallbackFlags, v8::internal::GCTracer::Scope::ScopeId) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 5: 0x1048d733c v8::internal::Heap::DevToolsTraceEventScope::~DevToolsTraceEventScope() [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 6: 0x1048d5d00 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags)::$_1::operator()() const [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 7: 0x1048d59b0 void heap::base::Stack::SetMarkerAndCallbackImpl<v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags)::$_1>(heap::base::Stack*, void*, void const*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 8: 0x104480028 PushAllRegistersAndIterateStack [/opt/homebrew/Cellar/node/23.3.0/bin/node]
 9: 0x1048d3f54 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
10: 0x104861d9c v8::internal::StackGuard::HandleInterrupts(v8::internal::StackGuard::InterruptLevel) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
11: 0x104bd9d84 v8::internal::Runtime_StackGuardWithGap(int, unsigned long*, v8::internal::Isolate*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
12: 0x10435f9f4 Builtins_CEntry_Return1_ArgvOnStack_NoBuiltinExit [/opt/homebrew/Cellar/node/23.3.0/bin/node]
13: 0x1305a5f0c 
14: 0x130ad3d9c 
15: 0x130965c88 
16: 0x130b789e0 
17: 0x1305c2b08 
18: 0x1305c2208 
19: 0x130b29da0 
20: 0x130ad6664 
21: 0x130667014 
22: 0x130b144c8 
23: 0x104305e20 Builtins_AsyncFunctionAwaitResolveClosure [/opt/homebrew/Cellar/node/23.3.0/bin/node]
24: 0x1043d3298 Builtins_PromiseFulfillReactionJob [/opt/homebrew/Cellar/node/23.3.0/bin/node]
25: 0x1042f5214 Builtins_RunMicrotasks [/opt/homebrew/Cellar/node/23.3.0/bin/node]
26: 0x1042c63f0 Builtins_JSRunMicrotasksEntry [/opt/homebrew/Cellar/node/23.3.0/bin/node]
27: 0x10483a410 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
28: 0x10483aae0 v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
29: 0x10485fbcc v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
30: 0x10485f95c v8::internal::MicrotaskQueue::PerformCheckpointInternal(v8::Isolate*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
31: 0x1042caaac Builtins_CallApiCallbackOptimizedNoProfiling [/opt/homebrew/Cellar/node/23.3.0/bin/node]
32: 0x1307c575c 
33: 0x1042c650c Builtins_JSEntryTrampoline [/opt/homebrew/Cellar/node/23.3.0/bin/node]
34: 0x1042c61b0 Builtins_JSEntry [/opt/homebrew/Cellar/node/23.3.0/bin/node]
35: 0x10483a438 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
36: 0x104839d98 v8::internal::Execution::Call(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, int, v8::internal::Handle<v8::internal::Object>*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
37: 0x10472a830 v8::Function::Call(v8::Isolate*, v8::Local<v8::Context>, v8::Local<v8::Value>, int, v8::Local<v8::Value>*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
38: 0x1044816d4 node::InternalCallbackScope::Close() [/opt/homebrew/Cellar/node/23.3.0/bin/node]
39: 0x104481b94 node::InternalMakeCallback(node::Environment*, v8::Local<v8::Object>, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*, node::async_context, v8::Local<v8::Value>) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
40: 0x104499b54 node::AsyncWrap::MakeCallback(v8::Local<v8::Function>, int, v8::Local<v8::Value>*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
41: 0x1046740d0 node::StreamBase::CallJSOnreadMethod(long, v8::Local<v8::ArrayBuffer>, unsigned long, node::StreamBase::StreamBaseJSChecks) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
42: 0x104674354 node::EmitToJSStreamListener::OnStreamRead(long, uv_buf_t const&) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
43: 0x10467a8a8 node::LibuvStreamWrap::OnUvRead(long, uv_buf_t const*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
44: 0x10467a5b0 node::LibuvStreamWrap::ReadStart()::$_1::__invoke(uv_stream_s*, long, uv_buf_t const*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
45: 0x108cff074 uv__stream_io [/opt/homebrew/Cellar/libuv/1.49.2/lib/libuv.1.dylib]
46: 0x108d06114 uv__io_poll [/opt/homebrew/Cellar/libuv/1.49.2/lib/libuv.1.dylib]
47: 0x108cf6184 uv_run [/opt/homebrew/Cellar/libuv/1.49.2/lib/libuv.1.dylib]
48: 0x104482488 node::SpinEventLoopInternal(node::Environment*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
49: 0x1045ba2c0 node::NodeMainInstance::Run(node::ExitCode*, node::Environment*) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
50: 0x1045ba014 node::NodeMainInstance::Run() [/opt/homebrew/Cellar/node/23.3.0/bin/node]
51: 0x10452fce8 node::Start(int, char**) [/opt/homebrew/Cellar/node/23.3.0/bin/node]
52: 0x18a0e0274 start [/usr/lib/dyld]
/opt/homebrew/lib/node_modules/claude-flow/node_modules/better-sqlite3/lib/methods/wrappers.js:5
	return this[cppdb].prepare(sql, this, false);
	                   ^

TypeError: The database connection is not open
    at Database.prepare (/opt/homebrew/lib/node_modules/claude-flow/node_modules/better-sqlite3/lib/methods/wrappers.js:5:21)
    at HiveMindSessionManager.logSessionEvent (file:///opt/homebrew/lib/node_modules/claude-flow/src/cli/simple-commands/hive-mind/session-manager.js:642:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Node.js v23.3.0

Where it happens

  • File: src/cli/simple-commands/hive-mind/session-manager.js
  • Line: ~642 (logSessionEvent calling db.prepare(...))

syntaxsurge avatar Aug 03 '25 08:08 syntaxsurge

Thank you for reporting this critical issue. I've analyzed the error and identified two interconnected problems:

Analysis

1. Primary Issue: Memory Exhaustion

The V8 heap is running out of memory, which suggests either:

  • A memory leak accumulating over time
  • Processing too much data in memory without proper cleanup
  • Circular references preventing garbage collection

2. Secondary Issue: Database Error During Shutdown

When the OOM occurs, the application attempts to log a shutdown event but fails because the SQLite connection is already closed.

Immediate Workaround

To prevent crashes while we investigate the memory leak:

# Increase Node.js memory limit
NODE_OPTIONS='--max-old-space-size=4096' npx claude-flow@alpha

Proposed Fixes

1. Quick Fix for Database Error

Add connection state checking in session-manager.js:~642:

logSessionEvent(event, data) {
  try {
    if (\!this.db || this.db.state \!== 'open') {
      console.warn('Database connection closed, skipping session event logging');
      return;
    }
    // existing code...
  } catch (error) {
    console.error('Failed to log session event:', error.message);
  }
}

2. Memory Leak Investigation

We need to:

  • Add memory profiling to identify which operations are consuming excessive memory
  • Review swarm coordination for potential data accumulation
  • Check for event listener leaks or unclosed resources

3. Graceful Shutdown Handler

Implement proper cleanup:

process.on('SIGTERM', async () => {
  try {
    await this.cleanup();
  } catch (error) {
    console.error('Cleanup error:', error);
  }
  process.exit(0);
});

Questions

  1. How long does the application run before the OOM occurs?
  2. Are you running any specific commands or workflows when this happens?
  3. Does the memory usage gradually increase over time or spike suddenly?

This information will help us identify the memory leak source more quickly.

ruvnet avatar Aug 03 '25 13:08 ruvnet

Same error, but in my case is happening on the hive-mind spawn command.

Environment:

  • Docker container for claude code and claude-flow
  • node v23 (tried with 20 and 22 as well)
  • docker-flow version 84 - installed using npm -g docker-flow@alpha
  • running the Docker container with root access

Error:

✓ Hive Mind prompt saved to: .hive-mind/sessions/hive-mind-prompt-swarm-1754278357770-c5p5ov0vi.txt
🔓 Using --dangerously-skip-permissions by default for seamless hive-mind execution

✓ Claude Code launched with Hive Mind coordination
  The Queen coordinator will orchestrate all worker agents
  Use MCP tools for collective intelligence and task distribution
  Prompt file saved at: .hive-mind/sessions/hive-mind-prompt-swarm-1754278357770-c5p5ov0vi.txt

💡 Pro Tips:
──────────────────────────────
• Use --auto-spawn to launch instances automatically
• Add --verbose for detailed coordination context
• Monitor with: claude-flow hive-mind status
• Share memories: mcp__ruv-swarm__memory_usage
--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons

✗ Claude Code exited with code 1
/usr/local/lib/node_modules/claude-flow/node_modules/better-sqlite3/lib/methods/wrappers.js:5
        return this[cppdb].prepare(sql, this, false);
                           ^

TypeError: The database connection is not open
    at Database.prepare (/usr/local/lib/node_modules/claude-flow/node_modules/better-sqlite3/lib/methods/wrappers.js:5:21)
    at HiveMindSessionManager.logSessionEvent (file:///usr/local/lib/node_modules/claude-flow/src/cli/simple-commands/hive-mind/session-manager.js:642:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Node.js v23.11.1

Any workaround for a Docker environment?

ggimenes avatar Aug 04 '25 03:08 ggimenes

Nevermind, I was running into several permission issues.

Now I'm able to run it using devcontainers and then installing docker-flow (latest version) inside the container: https://docs.anthropic.com/en/docs/claude-code/devcontainer

ggimenes avatar Aug 04 '25 04:08 ggimenes