openclaw - 💡(How to fix) Fix [Bug]: Agent listener invoked outside active run crashes Gateway (exec stdout race condition) [1 comments, 2 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#63390Fetched 2026-04-09 07:54:22
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
mentioned ×2subscribed ×2commented ×1

Gateway crashes intermittently with an unhandled promise rejection when a background exec process emits stdout/stderr output after the agent run that spawned it has already completed.

Error Message

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.js:1524:8)
  at handleStdout (exec-defaults.js:1546:4)

Root Cause

Gateway crashes intermittently with an unhandled promise rejection when a background exec process emits stdout/stderr output after the agent run that spawned it has already completed.

Code Example

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.js:1524:8)
  at handleStdout (exec-defaults.js:1546:4)
RAW_BUFFERClick to expand / collapse

Bug Report

Version: OpenClaw 2026.4.5 Node: v24.14.0 OS: Linux 6.8.0-106-generic (x64) — Docker container (Ubuntu), Hetzner VPS Install method: Docker (ghcr.io/openclaw/openclaw:2026.4.5)

Description

Gateway crashes intermittently with an unhandled promise rejection when a background exec process emits stdout/stderr output after the agent run that spawned it has already completed.

Error

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.js:1524:8)
  at handleStdout (exec-defaults.js:1546:4)

Steps to Reproduce

  1. Run a long-lived background exec process (e.g. python3 -u script.py > log.txt 2>&1 &)
  2. Agent run completes (session returns control)
  3. Background process continues emitting stdout after the run closes
  4. Gateway crashes with unhandled promise rejection

Timeline (from this instance)

  • 21:42:40 UTC: Crash — stdout callback fired on completed run
  • 21:42:41 UTC: Container exited
  • 21:42:41 UTC: Docker unless-stopped auto-restarted
  • 21:44:07 UTC: Gateway ready
  • 21:45:57 UTC: Health check passed

This crash has occurred 5 times in our current log rotation:

  • 2026-04-07 16:10:20 (handleStdout)
  • 2026-04-07 19:32:24 (handleStderr)
  • 2026-04-07 19:37:42 (handleStdout)
  • 2026-04-08 05:45:58 (handleStdout)
  • 2026-04-08 21:42:40 (handleStdout)

Expected Behavior

Gateway should guard against late stdout/stderr callbacks on completed runs — catch the rejection gracefully and continue running rather than crashing the entire process.

Impact

  • Severity: Critical (full gateway crash)
  • Frequency: Multiple times per day when background exec processes are in use
  • Recovery: Auto-restart via Docker policy (~2 min downtime)
  • Related issues: #62342, #62137, #61592, #62256, #61733

CC: @vincentkoc @mariozechner

extent analysis

TL;DR

  • The gateway crash can be mitigated by implementing a check to ignore stdout/stderr callbacks from background exec processes after the agent run has completed.

Guidance

  • Review the Agent.processEvents function in pi-agent-core/src/agent.ts to determine why it's not handling the promise rejection gracefully.
  • Consider adding a check in handleStdout and handleStderr functions in exec-defaults.js to ignore callbacks from completed runs.
  • Investigate the agent-loop.ts file to see if there's a way to prevent the onUpdate function from being called after a run has completed.
  • Look into the related issues (#62342, #62137, #61592, #62256, #61733) to see if they provide any insight into the problem or potential solutions.

Example

// In exec-defaults.js
function handleStdout(data) {
  // Check if the run is still active before processing the stdout
  if (isRunActive()) {
    // Process the stdout
    emitUpdate(data);
  } else {
    // Ignore the stdout if the run is not active
    console.log('Ignoring stdout from completed run');
  }
}

Notes

  • The provided error stack suggests that the issue is related to the handling of stdout/stderr callbacks from background exec processes.
  • The fact that the gateway crashes with an unhandled promise rejection indicates that the error is not being properly caught and handled.

Recommendation

  • Apply a workaround by adding checks to ignore stdout/stderr callbacks from completed runs, as this will prevent the gateway from crashing and allow it to continue running until a more permanent fix can be implemented.

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