Skip to content

[Bug P1] GatewayClient.request() has no timeout — memory leak + indefinite blocking #45466

@chrislro

Description

@chrislro

Summary

The request() method in src/gateway/client.ts creates a Promise that never rejects if the gateway fails to respond. This causes memory leaks and indefinite blocking, observed as sustained high CPU on the node client during idle reconnect loops.

Root Cause

// src/gateway/client.ts ~line 572
async request<T>(method: string, params?: unknown): Promise<T> {
  const p = new Promise<T>((resolve, reject) => {
    this.pending.set(id, { resolve, reject, expectFinal });
  });
  this.ws.send(JSON.stringify(frame));
  return p; // hangs forever if gateway never responds
}

No setTimeout is attached to reject the promise if the gateway never responds.

Impact

  • this.pending Map grows indefinitely (memory leak)
  • Callers block indefinitely
  • Combined with reconnect loop: CPU stays at 16-18% on idle Mac node (expected <1%)
  • 272 zombie processes accumulated over time in gateway container

Suggested Fix

Add a 30s timeout that rejects and cleans up the pending entry:

const timeout = setTimeout(() => {
  this.pending.delete(id);
  reject(new Error(`Gateway request timeout for ${method}`));
}, 30_000);

this.pending.set(id, {
  resolve: (v) => { clearTimeout(timeout); resolve(v as T); },
  reject: (e) => { clearTimeout(timeout); reject(e); },
  expectFinal,
});

Related Issues

This was identified alongside two other bugs in client.ts:

  1. Tick timeout does not flush pending requests before closing
  2. scheduleReconnect() has no max retry limit — infinite loop

Environment

  • OpenClaw v2026.3.8
  • Mac node (darwin), LaunchAgent managed
  • Gateway on Railway (Linux container)
  • Observed: 2026-03-13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions