openclaw - 💡(How to fix) Fix Unhandled promise rejection: Agent listener invoked outside active run (exec/PTY) [2 comments, 3 participants]

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…
GitHub stats
openclaw/openclaw#62137Fetched 2026-04-08 03:08:32
View on GitHub
Comments
2
Participants
3
Timeline
4
Reactions
0
Timeline (top)
commented ×2cross-referenced ×2

Gateway crashes intermittently with an unhandled promise rejection: Error: Agent listener invoked outside active run. This occurs in the exec/PTY subsystem when a backgrounded exec session emits stdout after the parent agent run has already completed.

The crash has occurred multiple times today, requiring manual restart each time. systemd shows the service stopping unexpectedly (not via SIGTERM from a user command).

Error Message

[openclaw] Unhandled promise rejection: Error: Agent listener invoked outside active run at Agent.processEvents (pi-agent-core/src/agent.ts:533:10) at pi-agent-core/src/agent.ts:380:21 at Object.onUpdate (pi-agent-core/src/agent-loop.ts:539:7) at emitUpdate (exec-defaults-uj0McX2k.js:1524:8) at handleStdout (exec-defaults-uj0McX2k.js:1546:4) at Object.onSupervisorStdout [as onStdout] (exec-defaults-uj0McX2k.js:1607:4) at exec-defaults-uj0McX2k.js:1007:21 at exec-defaults-uj0McX2k.js:749:4 at EventEmitter2.fire (node-pty-linux-x64/lib/eventEmitter2.js:41:22) at ReadStream.<anonymous> (node-pty-linux-x64/lib/terminal.js:92:61)

Root Cause

  1. Run multiple agents (4 Discord bot accounts) with heartbeat-driven exec tasks
  2. Agent heartbeats spawn background exec sessions (e.g., python3 scripts, claude --print via PTY)
  3. The agent run completes and returns a response to the user
  4. A backgrounded exec session emits stdout after the run completes
  5. The onUpdate callback fires into Agent.processEvents which throws because no run is active
  6. The unhandled promise rejection crashes the gateway

Fix Action

Workaround

Manual gateway restart after each crash. The systemd service does not auto-restart reliably in this case.

Code Example

[openclaw] Unhandled promise rejection: Error: Agent listener invoked outside active run
    at Agent.processEvents (pi-agent-core/src/agent.ts:533:10)
    at pi-agent-core/src/agent.ts:380:21
    at Object.onUpdate (pi-agent-core/src/agent-loop.ts:539:7)
    at emitUpdate (exec-defaults-uj0McX2k.js:1524:8)
    at handleStdout (exec-defaults-uj0McX2k.js:1546:4)
    at Object.onSupervisorStdout [as onStdout] (exec-defaults-uj0McX2k.js:1607:4)
    at exec-defaults-uj0McX2k.js:1007:21
    at exec-defaults-uj0McX2k.js:749:4
    at EventEmitter2.fire (node-pty-linux-x64/lib/eventEmitter2.js:41:22)
    at ReadStream.<anonymous> (node-pty-linux-x64/lib/terminal.js:92:61)

---

{
  "logLevelName": "ERROR",
  "date": "2026-04-06T16:04:23.622Z",
  "0": "[openclaw] Unhandled promise rejection: Error: Agent listener invoked outside active run..."
}
RAW_BUFFERClick to expand / collapse

Bug Report

Version

  • OpenClaw: v2026.4.5 (stable channel)
  • Node: v24.13.0
  • OS: Linux 6.17.0-14-generic (x64) — ThinkPad P72

Description

Gateway crashes intermittently with an unhandled promise rejection: Error: Agent listener invoked outside active run. This occurs in the exec/PTY subsystem when a backgrounded exec session emits stdout after the parent agent run has already completed.

The crash has occurred multiple times today, requiring manual restart each time. systemd shows the service stopping unexpectedly (not via SIGTERM from a user command).

Error Stack Trace

[openclaw] Unhandled promise rejection: Error: Agent listener invoked outside active run
    at Agent.processEvents (pi-agent-core/src/agent.ts:533:10)
    at pi-agent-core/src/agent.ts:380:21
    at Object.onUpdate (pi-agent-core/src/agent-loop.ts:539:7)
    at emitUpdate (exec-defaults-uj0McX2k.js:1524:8)
    at handleStdout (exec-defaults-uj0McX2k.js:1546:4)
    at Object.onSupervisorStdout [as onStdout] (exec-defaults-uj0McX2k.js:1607:4)
    at exec-defaults-uj0McX2k.js:1007:21
    at exec-defaults-uj0McX2k.js:749:4
    at EventEmitter2.fire (node-pty-linux-x64/lib/eventEmitter2.js:41:22)
    at ReadStream.<anonymous> (node-pty-linux-x64/lib/terminal.js:92:61)

Steps to Reproduce

  1. Run multiple agents (4 Discord bot accounts) with heartbeat-driven exec tasks
  2. Agent heartbeats spawn background exec sessions (e.g., python3 scripts, claude --print via PTY)
  3. The agent run completes and returns a response to the user
  4. A backgrounded exec session emits stdout after the run completes
  5. The onUpdate callback fires into Agent.processEvents which throws because no run is active
  6. The unhandled promise rejection crashes the gateway

Frequency

Occurred twice in one day:

  • 2026-04-06 12:04 PM ET
  • 2026-04-06 4:12 PM ET

Both have identical stack traces through the PTY/exec path.

Expected Behavior

Late stdout from backgrounded exec sessions should be safely ignored or queued if no agent run is active, rather than throwing an unhandled exception that crashes the gateway.

Workaround

Manual gateway restart after each crash. The systemd service does not auto-restart reliably in this case.

Configuration Context

  • 4 agents (main, calvin, mara, rook) each with their own Discord bot accounts
  • Agents use heartbeat-driven cron jobs that spawn exec sessions
  • tools.exec.ask: "off", tools.exec.security: "full", tools.exec.strictInlineEval: false
  • Multiple concurrent sessions active at time of crash

Log Excerpts

Gateway log shows the error logged at ERROR level right before the crash, with no recovery:

{
  "logLevelName": "ERROR",
  "date": "2026-04-06T16:04:23.622Z",
  "0": "[openclaw] Unhandled promise rejection: Error: Agent listener invoked outside active run..."
}

Systemd journal shows clean start/stop cycles around crash times, confirming the process died and had to be restarted manually.

extent analysis

TL;DR

The gateway can be stabilized by implementing a check to ignore or queue late stdout emissions from backgrounded exec sessions when no agent run is active.

Guidance

  • Review the Agent.processEvents function in pi-agent-core/src/agent.ts to understand how it handles events outside of an active run and consider adding a check to safely ignore or queue these events.
  • Investigate the onUpdate callback in exec-defaults-uj0McX2k.js to ensure it properly handles cases where the agent run has completed before emitting stdout.
  • Consider adding error handling for unhandled promise rejections in the gateway to prevent crashes and allow for more graceful recovery.
  • Verify that the systemd service configuration is set up to restart the gateway service automatically in case of a crash, despite the current issues with reliable auto-restart.

Example

// Example of how to check if an agent run is active before processing events
if (agentRunIsActive()) {
  Agent.processEvents(event);
} else {
  // Queue or ignore the event
  queueEvent(event);
}

Notes

The provided stack trace and error message suggest that the issue is related to the handling of events outside of an active agent run. However, without more information about the specific requirements and constraints of the system, it's difficult to provide a more detailed solution.

Recommendation

Apply a workaround to ignore or queue late stdout emissions from backgrounded exec sessions when no agent run is active, as this will prevent the gateway from crashing due to unhandled promise rejections. This can be achieved by modifying the Agent.processEvents function or the onUpdate callback to handle these cases more robustly.

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