#6466: fix(gateway): add handshake timeout and connection error handling
gateway
Cluster:
Timeouts and Memory Management Fixes
Adds timeout handling for agent CLI connections to prevent hanging when the gateway is unreachable or slow to respond.
## Changes
- Added handshake timeout for connection establishment
- Improved error handling for connection failures
- Clearer error messages when connection times out
## Testing
Tested locally with various network conditions.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds faster failure behavior for gateway CLI connections by:
- Setting a WebSocket `handshakeTimeout` in `GatewayClient` to prevent hanging during connection establishment.
- Plumbing a new `onConnectError` path through `callGateway` so connection failures surface as clearer CLI errors with connection details.
These changes fit into the existing gateway client lifecycle (`GatewayClient.start()` wiring and `callGateway`’s promise-based connect/request flow) and should improve operator UX when the gateway is unreachable or slow.
<h3>Confidence Score: 4/5</h3>
- This PR is reasonably safe to merge and is a focused improvement to connection failure behavior.
- Changes are small and localized to gateway connection setup and error plumbing; the new `handshakeTimeout` is a standard ws option and the new callback is guarded to avoid double-settle. The main concern is message formatting consistency (literal `\\n`), not runtime correctness.
- src/gateway/call.ts (error message formatting), src/gateway/client.ts (ensure handshakeTimeout aligns with desired CLI timeout behavior)
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#6302: fix: Add timeouts to prevent indefinite hangs (issues #4954, #4956,...
by batumilove · 2026-02-01
82.2%
#7615: fix(gateway): add request timeout to GatewayClient
by alamine42 · 2026-02-03
80.7%
#4653: fix(gateway): improve crash resilience for mDNS and network errors
by AyedAlmudarra · 2026-01-30
79.1%
#9178: Fix: GatewayClient queueConnect() setTimeout never fires
by vishaltandale00 · 2026-02-04
78.4%
#7128: feat: add gateway.restart RPC for graceful in-process restart
by AkashaBot · 2026-02-02
78.4%
#5823: fix(config): exit cleanly on invalid config instead of high CPU loop
by gavinbmoore · 2026-02-01
78.2%
#10034: Don't crash gateway on transient unhandled fetch failures
by gigq · 2026-02-06
78.0%
#10636: fix: setTimeout integer overflow causing server crash
by devmangel · 2026-02-06
77.8%
#7563: fix: expand transient network error detection
by kaigritun · 2026-02-03
77.4%
#8713: feat: gateway memory monitor, install linger, docs and failover
by quratus · 2026-02-04
77.2%