From f8e46571507a6b54bb81312d8cb0e5911cd06d9d Mon Sep 17 00:00:00 2001 From: AbhilashG12 Date: Thu, 9 Jul 2026 16:40:05 +0530 Subject: [PATCH 1/2] fix(polymarket-us): prevent websocket connect hangs with garbage-collected timeout --- core/src/exchanges/polymarket_us/websocket.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/core/src/exchanges/polymarket_us/websocket.ts b/core/src/exchanges/polymarket_us/websocket.ts index c5e77276..6ac277c8 100644 --- a/core/src/exchanges/polymarket_us/websocket.ts +++ b/core/src/exchanges/polymarket_us/websocket.ts @@ -145,7 +145,24 @@ export class PolymarketUSWebSocket { socket.on('trade', (msg: SdkTrade) => this.handleTrade(msg)); socket.on('error', (err: Error) => this.handleError(err)); socket.on('close', () => this.handleClose()); - await socket.connect(); + // Wrap connection in a timeout with proper garbage collection + const CONNECTION_TIMEOUT_MS = 15000; // 15s is standard for trading API handshakes + let timeoutHandle: NodeJS.Timeout; + + const timeoutPromise = new Promise((_, reject) => { + timeoutHandle = setTimeout(() => { + reject(new Error(`PolymarketUS WebSocket connection timed out after ${CONNECTION_TIMEOUT_MS}ms`)); + }, CONNECTION_TIMEOUT_MS); + }); + + try { + await Promise.race([ + socket.connect(), + timeoutPromise + ]); + } finally { + clearTimeout(timeoutHandle!); // Always clear the timer so the event loop can breathe + } this.socket = socket; })(); From 9dc8fedb26dd8739f8a10b0e69dff643c3c82b4d Mon Sep 17 00:00:00 2001 From: Samuel Tinnerholm Date: Fri, 10 Jul 2026 06:49:34 +0000 Subject: [PATCH 2/2] fix: remove trailing whitespace in polymarket us websocket --- core/src/exchanges/polymarket_us/websocket.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/exchanges/polymarket_us/websocket.ts b/core/src/exchanges/polymarket_us/websocket.ts index 6ac277c8..a4676693 100644 --- a/core/src/exchanges/polymarket_us/websocket.ts +++ b/core/src/exchanges/polymarket_us/websocket.ts @@ -148,7 +148,7 @@ export class PolymarketUSWebSocket { // Wrap connection in a timeout with proper garbage collection const CONNECTION_TIMEOUT_MS = 15000; // 15s is standard for trading API handshakes let timeoutHandle: NodeJS.Timeout; - + const timeoutPromise = new Promise((_, reject) => { timeoutHandle = setTimeout(() => { reject(new Error(`PolymarketUS WebSocket connection timed out after ${CONNECTION_TIMEOUT_MS}ms`));