Skip to content

[Bug]: Race condition in subagent/session cleanup causes late child stdout to hit cleared active run, leading to announce timeout, missing-session-entry, and orphaned child processes #61741

Description

@jsompis

Bug type

Crash (process/app exits or hangs)

Beta release blocker

No

Summary

We are seeing a recurring lifecycle failure in OpenClaw where a subagent or exec child still emits stdout/update events after the parent run/session has already been ended, patched away, or deleted.

The first hard error is:

Agent listener invoked outside active run

After that, the failure cascades into symptoms such as:

  • subagent announce timeout
  • gateway websocket handshake timeout / closed before connect
  • Subagent orphan run pruned ... reason=missing-session-entry

In at least one observed case, a Claude Code child process continued running after its wrapper/supervision chain disappeared and was reparented to PID 1. This suggests the runtime lost ownership of the child before stdout/update draining and completion handoff were fully finished.

This does not appear to require a plugin update or a gateway restart to trigger. The same error chain also appears in incident windows without a nearby restart.

Steps to reproduce

  1. Start a subagent or exec-backed child process that can continue emitting stdout near the end of a run.
  2. Let the parent run/session reach a completion or cleanup edge quickly, for example:
    • subagent completion
    • agent.wait
    • a run ending with stopReason=toolUse
    • a no-reply/completion path
    • session patch/delete/cleanup while child completion is still in flight
  3. Allow the child/supervisor to emit stdout or update events slightly after the parent active-run/session state has already been cleared.
  4. Observe that the runtime throws Agent listener invoked outside active run, then downstream subagent announce/resume/gateway behavior starts failing.

Expected behavior

If a child process emits late stdout/update events near the end of a run:

  • the runtime should drain those updates before clearing active-run ownership, or
  • safely ignore/drop late post-run updates without throwing

Subagent completion/announce should remain reliable, and child processes should not become orphaned.

Actual behavior

Late child/supervisor stdout reaches the runtime after the parent active run has already been cleared, causing:

  • Agent listener invoked outside active run

This then cascades into:

  • subagent announce timeout
  • websocket handshake timeout / closed before connect
  • Subagent orphan run pruned ... reason=missing-session-entry

In at least one case, the underlying Claude child process continued running after its wrapper/parent disappeared and was observed with PPID=1.

OpenClaw version

2026.4.5

Operating system

macOS 26.4

Install method

npm

Model

openai-codex/gpt-5.4

Provider / routing chain

openclaw->openai-codex/gpt-5.4

Additional provider/model setup details

Environment

  • OpenClaw 2026.4.5
  • macOS 26.4
  • launchd-managed gateway
  • local websocket/gateway traffic on 127.0.0.1
  • reproduced during real subagent + Claude Code workflows

Logs, screenshots, and evidence

2026-04-06T14:13:41.117+07:00 [openclaw] Unhandled promise rejection: Error: Agent listener invoked outside active run
    at Agent.processEvents (file:///opt/homebrew/lib/node_modules/openclaw/node_modules/@mariozechner/pi-agent-core/src/agent.ts:533:10)
    at file:///opt/homebrew/lib/node_modules/openclaw/node_modules/@mariozechner/pi-agent-core/src/agent.ts:380:21
    at Object.onUpdate (file:///opt/homebrew/lib/node_modules/openclaw/node_modules/@mariozechner/pi-agent-core/src/agent-loop.ts:539:7)
    at emitUpdate (file:///opt/homebrew/lib/node_modules/openclaw/dist/exec-defaults-uj0McX2k.js:1524:8)
    at handleStdout (file:///opt/homebrew/lib/node_modules/openclaw/dist/exec-defaults-uj0McX2k.js:1546:4)
    at Object.onSupervisorStdout [as onStdout] (file:///opt/homebrew/lib/node_modules/openclaw/dist/exec-defaults-uj0McX2k.js:1610:3)
    at file:///opt/homebrew/lib/node_modules/openclaw/dist/exec-defaults-uj0McX2k.js:1007:21
    at Socket.<anonymous> (file:///opt/homebrew/lib/node_modules/openclaw/dist/exec-defaults-uj0McX2k.js:568:4)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:561:12)

Impact and severity

High severity.

User-visible impact:

  • subagent completions fail to announce reliably
  • websocket/gateway failures surface as abnormal closure symptoms
  • runs may appear lost or partially completed

Operational impact:

  • child processes can become orphaned and continue running without ownership
  • session/run state becomes inconsistent (missing-session-entry)
  • completion handoff becomes unreliable
  • long-running automation workflows become fragile around cleanup/completion timing

This looks like a lifecycle race in parent run/session cleanup versus late child stdout/update delivery. Even if local wrappers are hardened to reduce orphan symptoms, the upstream runtime sequencing issue remains.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbug:crashProcess/app exits unexpectedly or hangs

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions