Skip to content

Browser process becomes orphaned when gateway restarts, causing memory leak #11257

Description

@huaguihai

Bug Description

When openclaw-gateway restarts or exits unexpectedly, the Chromium browser processes spawned for headless browsing are not properly terminated. These processes become orphaned (parent PID becomes 1) and continue running indefinitely, accumulating renderer processes and consuming significant memory over time.

Environment

  • OS: Linux 5.15.0-46-generic (x64)
  • OpenClaw Version: 2026.2.2-3 (also confirmed in 2026.2.6-3)
  • Node.js: v24.13.0
  • Browser: Chromium (snap) with --remote-debugging-port=9222

Steps to Reproduce

  1. Start openclaw-gateway with browser enabled (browser.enabled: true in config)
  2. Let the gateway run for some time (browser will be spawned)
  3. Restart the gateway (openclaw gateway restart) or let it crash
  4. Observe that old Chromium processes are not terminated

Observed Behavior

After running for 8 days, the following was observed:

# Main Chromium process became orphaned (PPID = 1, started Jan 30)
root 2974010 1 0 Jan30 ? 01:30:08 /snap/chromium/.../chrome --headless --remote-debugging-port=9222 ...

# Multiple renderer processes accumulated, each consuming 100-200MB
root 1569768 ... 5.6% mem  /snap/chromium/.../chrome --type=renderer ...
root 1322752 ... 5.2% mem  /snap/chromium/.../chrome --type=renderer ...
root 1301273 ... 5.0% mem  /snap/chromium/.../chrome --type=renderer ...
# ... 10+ renderer processes

Total memory consumed by orphaned browser processes: ~1.5GB

Root Cause Analysis

In chrome.ts, the browser is spawned using spawn():

return spawn(exe.path, args, {
  stdio: "pipe",
  env: { ...process.env, HOME: os.homedir() }
});

When the gateway process exits or restarts:

  1. The spawned Chrome process is not killed
  2. Chrome becomes an orphan process (reparented to PID 1)
  3. Renderer child processes continue to accumulate
  4. Memory usage grows unbounded

Expected Behavior

When openclaw-gateway restarts or exits:

  1. All spawned browser processes should be terminated gracefully
  2. A cleanup handler should kill any lingering Chrome processes on the configured CDP port
  3. On startup, the gateway should detect and clean up any orphaned browser processes from previous runs

Suggested Fixes

  1. Add process cleanup on exit: Register process.on('exit') and process.on('SIGTERM') handlers to call stopOpenClawChrome()

  2. Startup cleanup: On gateway start, check if port 9222 is in use by an orphaned Chrome process and kill it

  3. Process group management: Consider using detached: false and managing the process group to ensure child processes are cleaned up

Workaround

Currently using a cron job to restart the gateway daily:

0 19 * * * openclaw gateway restart >> /var/log/openclaw-restart.log 2>&1

And manually killing orphaned processes:

pkill -9 -f "chromium.*--remote-debugging-port=9222"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked as stale due to inactivity

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions