From 893279ceecd3fbdcfde13672cde1b72280eb6b86 Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Thu, 5 Nov 2026 12:07:17 -0500 Subject: [PATCH 1/3] fix(socket-mode): stop waiting on a close handshake Slack never completes undici marks the socket CLOSING when the close handshake completes but only fires "close" once the peer ends TCP, which Slack never does. disconnect() therefore waited for the pong watchdog (about clientPingTimeout) or the 30s fallback timer. - Lower the close-handshake timeout to 1s and log it at debug: it is the expected path against Slack. - Stop the ping/pong heartbeat once the close frame is sent, so the timeout owns the wait and the spurious "A pong wasn't received" warning is gone. - When Slack starts the close handshake, terminate on the next heartbeat tick; undici's ping() silently does nothing while CLOSING. Fixes #2752. Supersedes #2697. Co-authored-by: Grmiade Co-authored-by: Claude --- .../socket-mode-close-handshake-timeout.md | 5 ++ .../socket-mode/src/SlackWebSocket.test.ts | 85 ++++++++++++++++++- packages/socket-mode/src/SlackWebSocket.ts | 21 ++++- 3 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 .changeset/socket-mode-close-handshake-timeout.md diff --git a/.changeset/socket-mode-close-handshake-timeout.md b/.changeset/socket-mode-close-handshake-timeout.md new file mode 100644 index 000000000..a89b4e40e --- /dev/null +++ b/.changeset/socket-mode-close-handshake-timeout.md @@ -0,0 +1,5 @@ +--- +"@slack/socket-mode": patch +--- + +Fix `disconnect()` taking 5 seconds or more ([#2752](https://github.com/slackapi/node-slack-sdk/issues/2752)). Slack does not close the TCP connection after the close handshake, so `disconnect()` waited until the pong watchdog gave up and logged "A pong wasn't received". The close handshake now times out after 1 second, the heartbeat stops once the close frame is sent, and a close handshake started by Slack is cleaned up on the next heartbeat tick. diff --git a/packages/socket-mode/src/SlackWebSocket.test.ts b/packages/socket-mode/src/SlackWebSocket.test.ts index 00f0bce04..3e1b68b84 100644 --- a/packages/socket-mode/src/SlackWebSocket.test.ts +++ b/packages/socket-mode/src/SlackWebSocket.test.ts @@ -151,7 +151,10 @@ describe('SlackWebSocket', () => { assert.strictEqual(sws.readyState, DeadPeerWS.CLOSING, 'socket should be CLOSING after sending close frame'); assert.strictEqual(closeEmitted, false, 'close must not fire before the handshake completes or times out'); - clock.tick(60000); + clock.tick(999); + assert.strictEqual(closeEmitted, false, 'close must not fire before the handshake timeout elapses'); + + clock.tick(1); assert.strictEqual( closeEmitted, @@ -161,6 +164,86 @@ describe('SlackWebSocket', () => { }); }); + describe('heartbeat while closing against a peer that never ends TCP (issue #2752)', () => { + class HalfClosedPeerWS extends EventTarget { + static CONNECTING = 0; + static OPEN = 1; + static CLOSING = 2; + static CLOSED = 3; + readyState = HalfClosedPeerWS.OPEN; + close() { + this.readyState = HalfClosedPeerWS.CLOSING; + } + send(_data: string) {} + } + + function open(clientPingTimeoutMS: number) { + const ws = new HalfClosedPeerWS(); + const ping = sandbox.spy(); + const SWS = proxyquire.load('./SlackWebSocket', { + undici: { + WebSocket: class Fake { + static CONNECTING = 0; + static OPEN = 1; + static CLOSING = 2; + static CLOSED = 3; + constructor() { + // biome-ignore lint/correctness/noConstructorReturn: for test mocking purposes + return ws; + } + }, + CloseEvent, + ErrorEvent, + MessageEvent, + ping, + }, + }).SlackWebSocket; + const client = new EventEmitter(); + let closeEvents = 0; + client.on('close', () => { + closeEvents += 1; + }); + const logger = new ConsoleLogger(); + const warn = sandbox.stub(logger, 'warn'); + sandbox.stub(logger, 'debug'); + const sws = new SWS({ + url: 'ws://127.0.0.1/', + client, + clientPingTimeoutMS, + serverPingTimeoutMS: 60000, + logger, + }); + sws.connect(); + ws.dispatchEvent(new Event('open')); + return { ws, sws, ping, warn, closeEvents: () => closeEvents }; + } + + it('should stop the heartbeat once the close frame is sent, so the pong watchdog never warns', () => { + const clock = sandbox.useFakeTimers(); + const { ws, sws, ping, warn, closeEvents } = open(900); + channel('undici:websocket:pong').publish({ websocket: ws, payload: Buffer.from('x') }); + + sws.disconnect(); + const pingsBeforeClose = ping.callCount; + clock.tick(1000); + + assert.strictEqual(ping.callCount, pingsBeforeClose, 'no ping should be sent while closing'); + assert.strictEqual(closeEvents(), 1); + sinon.assert.notCalled(warn); + }); + + it('should terminate within one heartbeat tick when the peer starts the close handshake', () => { + const clock = sandbox.useFakeTimers(); + const { ws, warn, closeEvents } = open(3000); + + ws.readyState = HalfClosedPeerWS.CLOSING; + clock.tick(1000); + + assert.strictEqual(closeEvents(), 1); + sinon.assert.notCalled(warn); + }); + }); + describe('disconnect() when the connection drops while closing (issue #2771)', () => { class UndiciLikeWS extends EventTarget { static CONNECTING = 0; diff --git a/packages/socket-mode/src/SlackWebSocket.ts b/packages/socket-mode/src/SlackWebSocket.ts index c5540197a..17f0544bb 100644 --- a/packages/socket-mode/src/SlackWebSocket.ts +++ b/packages/socket-mode/src/SlackWebSocket.ts @@ -11,7 +11,7 @@ import type { SocketModeDispatcher } from './SocketModeOptions'; export const WS_READY_STATES = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED']; -const CLOSE_HANDSHAKE_TIMEOUT_MS = 30_000; +const CLOSE_HANDSHAKE_TIMEOUT_MS = 1_000; interface PingPongMessage { websocket: WebSocket; @@ -256,8 +256,9 @@ export class SlackWebSocket { // If we haven't received a close frame yet, then we send one to the peer, expecting to receive a close frame // in response. this.logger.debug('Sending close frame (status=1000).'); + this.stopMonitoringPings(); this.closeHandshakeTimeout = setTimeout(() => { - this.logger.warn('Peer did not complete the close handshake in time; forcing cleanup.'); + this.logger.debug('Peer did not complete the close handshake in time; forcing cleanup.'); this.cleanup(); }, CLOSE_HANDSHAKE_TIMEOUT_MS); this.websocket.close(1000); // 1000 = Normal Closure @@ -291,8 +292,7 @@ export class SlackWebSocket { } this.defaultSocket = null; this.websocket = null; - clearTimeout(this.serverPingTimeout); - clearInterval(this.clientPingTimeout); + this.stopMonitoringPings(); clearTimeout(this.closeHandshakeTimeout); // Emit event back to client letting it know connection has closed (in case it needs to reconnect if // reconnecting is enabled) @@ -346,6 +346,14 @@ export class SlackWebSocket { }, this.options.serverPingTimeoutMS); } + /** + * Stops the ping/pong heartbeat; a connection that is closing has nothing left to monitor. + */ + private stopMonitoringPings(): void { + clearTimeout(this.serverPingTimeout); + clearInterval(this.clientPingTimeout); + } + /** * Monitors WebSocket connection health; sends a ping to peer, and expects a pong within a certain timeframe. * If that expectation is not met, we disconnect the websocket. @@ -362,6 +370,11 @@ export class SlackWebSocket { this.logger.error('WebSocket not available, skipping ping.'); return; } + if (this.websocket.readyState === WebSocket.CLOSING) { + this.logger.debug('Peer started the close handshake; terminating WebSocket.'); + this.disconnect(); + return; + } ping(this.websocket, Buffer.from(pingMessage)); if (this.lastPongReceivedTimestamp === undefined) { pingAttemptCount += 1; From efe703bbd00c73b66138dfa3d5a4facfc3ffcd83 Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Thu, 5 Nov 2026 12:55:22 -0500 Subject: [PATCH 2/3] chore(socket-mode): drop redundant stopMonitoringPings comment Co-Authored-By: Claude --- packages/socket-mode/src/SlackWebSocket.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/socket-mode/src/SlackWebSocket.ts b/packages/socket-mode/src/SlackWebSocket.ts index 17f0544bb..a8b01cbf9 100644 --- a/packages/socket-mode/src/SlackWebSocket.ts +++ b/packages/socket-mode/src/SlackWebSocket.ts @@ -346,9 +346,6 @@ export class SlackWebSocket { }, this.options.serverPingTimeoutMS); } - /** - * Stops the ping/pong heartbeat; a connection that is closing has nothing left to monitor. - */ private stopMonitoringPings(): void { clearTimeout(this.serverPingTimeout); clearInterval(this.clientPingTimeout); From 2b03e7b50c2cd5c467c9fd8e196fd16b9388b2df Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Thu, 5 Nov 2026 13:26:45 -0500 Subject: [PATCH 3/3] test(socket-mode): assert the heartbeat does not cut the close handshake short The test still passed with stopMonitoringPings() removed from disconnect(), since the heartbeat's CLOSING check cleaned up early with no ping or warn. Asserting no close before the 1s timeout guards the ack window, and the test name now says what it checks. Co-Authored-By: Claude --- packages/socket-mode/src/SlackWebSocket.test.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/socket-mode/src/SlackWebSocket.test.ts b/packages/socket-mode/src/SlackWebSocket.test.ts index 3e1b68b84..92d18e77c 100644 --- a/packages/socket-mode/src/SlackWebSocket.test.ts +++ b/packages/socket-mode/src/SlackWebSocket.test.ts @@ -218,14 +218,17 @@ describe('SlackWebSocket', () => { return { ws, sws, ping, warn, closeEvents: () => closeEvents }; } - it('should stop the heartbeat once the close frame is sent, so the pong watchdog never warns', () => { + it('should stop the heartbeat once the close frame is sent, so the close-handshake timeout owns the wait', () => { const clock = sandbox.useFakeTimers(); const { ws, sws, ping, warn, closeEvents } = open(900); channel('undici:websocket:pong').publish({ websocket: ws, payload: Buffer.from('x') }); sws.disconnect(); const pingsBeforeClose = ping.callCount; - clock.tick(1000); + clock.tick(999); + assert.strictEqual(closeEvents(), 0, 'the heartbeat must not cut the close handshake short'); + + clock.tick(1); assert.strictEqual(ping.callCount, pingsBeforeClose, 'no ping should be sent while closing'); assert.strictEqual(closeEvents(), 1);