Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/socket-mode-shutdown-heartbeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slack/socket-mode': patch
---

Suppress heartbeat timeout warnings while a Socket Mode client intentionally closes its WebSocket connection.
35 changes: 35 additions & 0 deletions packages/socket-mode/src/SlackWebSocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,40 @@ describe('SlackWebSocket', () => {
ws.dispatchEvent(new ErrorEvent('error', { error: new Error('boom'), message: 'boom' }));
sinon.assert.calledOnce(discStub);
});

it('should stop heartbeat monitoring during an intentional disconnect', () => {
const ws = new WSMock();
SlackWebSocket = proxyquire.load('./SlackWebSocket', {
undici: {
WebSocket: class Fake {
constructor() {
// biome-ignore lint/correctness/noConstructorReturn: for test mocking purposes
return ws;
}
},
CloseEvent,
ErrorEvent,
MessageEvent,
ping: () => {},
},
}).SlackWebSocket;
const logger = new ConsoleLogger();
const warn = sinon.stub(logger, 'warn');
const clock = sandbox.useFakeTimers();
const sws = new SlackWebSocket({
url: 'whatevs',
client: new EventEmitter(),
clientPingTimeoutMS: 5000,
serverPingTimeoutMS: 30000,
logger,
});

sws.connect();
ws.dispatchEvent(new Event('open'));
sws.disconnect({ suppressHeartbeat: true });
clock.tick(20000);

sinon.assert.neverCalledWithMatch(warn, "A pong wasn't received from the server");
});
});
});
7 changes: 6 additions & 1 deletion packages/socket-mode/src/SlackWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ export class SlackWebSocket {
/**
* Disconnects the WebSocket connection with Slack, if connected.
*/
public disconnect(): void {
public disconnect({ suppressHeartbeat = false }: { suppressHeartbeat?: boolean } = {}): void {
if (suppressHeartbeat) {
clearTimeout(this.serverPingTimeout);
clearInterval(this.clientPingTimeout);
}

if (this.websocket) {
// Disconnecting a WebSocket involves a close frame handshake so we check if we've already received a close frame.
// If so, we can terminate the underlying socket connection and let the client know.
Expand Down
4 changes: 3 additions & 1 deletion packages/socket-mode/src/SocketModeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ export class SocketModeClient extends EventEmitter {
} else {
// Resolve (or reject) on disconnect
this.once(State.Disconnected, resolve);
this.websocket?.disconnect();
// A manual shutdown should not report heartbeat failures while the
// WebSocket close handshake is in progress.
this.websocket?.disconnect({ suppressHeartbeat: true });
}
});
}
Expand Down