openclaw - 💡(How to fix) Fix Concurrency Race Condition: active-memory Writes State during before_prompt_build Hook Triggering Lock Violations

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

When running OpenClaw with the active-memory extension enabled, the execution periodically crashes with a concurrency error during the prompt formulation or embedding generation phases: 4. Because the before_prompt_build hook performs an out-of-band write to the session SQLite file while the exclusive lock is unlocked/relaxed, the Gateway detects that the session state was modified externally. This triggers a protection check, immediately aborting execution with the lock violation error.

Root Cause

  1. The active-memory extension implements the before_prompt_build lifecycle hook to query vector databases (like QMD) and retrieve long-term context to inject into the upcoming prompt.
  2. During this hook, the extension executes persistPluginStatusLines(...) to write active memory search results, summaries, and debug metadata back to the session state (SQLite).
  3. Concurrently, the OpenClaw Gateway temporarily releases or relaxes its exclusive write lock on the session file during the async embedding assembly and prompt formulation stages.
  4. Because the before_prompt_build hook performs an out-of-band write to the session SQLite file while the exclusive lock is unlocked/relaxed, the Gateway detects that the session state was modified externally. This triggers a protection check, immediately aborting execution with the lock violation error.

Fix Action

Fix / Workaround

Workaround / Local Patch

We resolved this stability issue by patching persistPluginStatusLines in /app/extensions/active-memory/index.ts (and its compiled JS equivalent in /app/dist/) to return early as a no-op, preventing database mutation during this phase.

Code Example

Agent failed before reply: session file changed while embedded prompt lock was released

---

async function persistPluginStatusLines(params: {
  api: OpenClawPluginApi;
  agentId: string;
  sessionKey?: string;
  statusLine?: string;
  debugSummary?: string | null;
  searchDebug?: ActiveMemorySearchDebug;
}): Promise<void> {
  // Disabling session file modification during before_prompt_build phase
  // to avoid lock release race conditions ("session file changed while embedded prompt lock was released").
  return;
}
RAW_BUFFERClick to expand / collapse

Concurrency Race Condition: active-memory Writes State during before_prompt_build Hook Triggering Lock Violations

Bug Description

When running OpenClaw with the active-memory extension enabled, the execution periodically crashes with a concurrency error during the prompt formulation or embedding generation phases:

Agent failed before reply: session file changed while embedded prompt lock was released

Root Cause Analysis

  1. The active-memory extension implements the before_prompt_build lifecycle hook to query vector databases (like QMD) and retrieve long-term context to inject into the upcoming prompt.
  2. During this hook, the extension executes persistPluginStatusLines(...) to write active memory search results, summaries, and debug metadata back to the session state (SQLite).
  3. Concurrently, the OpenClaw Gateway temporarily releases or relaxes its exclusive write lock on the session file during the async embedding assembly and prompt formulation stages.
  4. Because the before_prompt_build hook performs an out-of-band write to the session SQLite file while the exclusive lock is unlocked/relaxed, the Gateway detects that the session state was modified externally. This triggers a protection check, immediately aborting execution with the lock violation error.

Workaround / Local Patch

We resolved this stability issue by patching persistPluginStatusLines in /app/extensions/active-memory/index.ts (and its compiled JS equivalent in /app/dist/) to return early as a no-op, preventing database mutation during this phase.

async function persistPluginStatusLines(params: {
  api: OpenClawPluginApi;
  agentId: string;
  sessionKey?: string;
  statusLine?: string;
  debugSummary?: string | null;
  searchDebug?: ActiveMemorySearchDebug;
}): Promise<void> {
  // Disabling session file modification during before_prompt_build phase
  // to avoid lock release race conditions ("session file changed while embedded prompt lock was released").
  return;
}

Proposed Fix

  • State Deferral: Status logs or search metadata updates produced during before_prompt_build should be cached in-memory and deferred until a safe post-turn or post-execution hook when the session lock is explicitly re-acquired.
  • Lock Coordination: Alternatively, the lock state machine should allow cooperative writes from registered extensions during prompt formulation, or run status line updates under a unified write transaction.

Environment Details

  • OpenClaw Host: macOS / Apple Silicon
  • Runtime: Docker Container (openclaw-sandbox) & Bare-Metal setups
  • Gateway Version: 25.x+

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix Concurrency Race Condition: active-memory Writes State during before_prompt_build Hook Triggering Lock Violations