Skip to content

Pi subagent fails: DatabaseLockedError - another context-mode server is already running #562

@dashanlkk

Description

@dashanlkk

Bug Report

Platform

  • Pi coding agent (Windows)
  • context-mode v1.0.129

Problem

When spawning subagents via Pi's subagent() tool (parallel mode), each child agent tries to load the context-mode extension. The extension's db-lock.js (acquireDbLock) detects the parent process's PID lockfile and throws DatabaseLockedError:

Failed to load extension "context-mode/build/adapters/pi/extension.js": 
Another context-mode server is already running (PID: 4524). Stop it before starting a new instance.

This makes all subagents fail immediately — they cannot even start.

Root Cause

db-lock.js enforces single-writer via a PID-based lockfile (<dbPath>.lock). This is correct for standalone instances but breaks when:

  1. Parent agent starts context-mode (holds the lock)
  2. Parent spawns child subagents that inherit the same extension config
  3. Each child tries to acquire the same lock → DatabaseLockedError

Current Workaround

Create a custom agent with extensions: "" (empty) to skip context-mode entirely:

subagent({ action: "create", config: { name: "ida-decompiler", extensions: "", ... } })

This works but means child agents lose all context-mode benefits (ctx_execute, ctx_search, etc.)

Proposed Fix Options

  1. Read-only mode for child processes: Detect that the lock is held by the parent process and open SQLite in read-only mode instead of throwing
  2. Per-process temp DB: Child agents already use tmpdir-based DBs (which isUnderTmpdir skips) — but the shared project DB path triggers the lock
  3. Graceful skip: If extension loads in a subagent context (detect via env var or Pi API), skip DB initialization entirely rather than crashing
  4. Share the lock: Allow child processes to share the parent's DB connection via IPC instead of opening their own

Reproduction

  1. Start Pi with context-mode extension enabled
  2. Run subagent({ tasks: [{ agent: "delegate", task: "hello" }, { agent: "delegate", task: "world" }], concurrency: 2 })
  3. Both subagents fail with DatabaseLockedError

Related: #233 (ctx_commands injection in subagent sessions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions