Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
541f74f
fix(socket-mode): observe reconnect failures and stop connecting afte…
WilliamBergamin Nov 5, 2026
362c891
refactor(socket-mode): use try/catch for the reconnect failure handler
WilliamBergamin Nov 5, 2026
7f3dde6
refactor(socket-mode): await the reconnect delay instead of chaining …
WilliamBergamin Nov 5, 2026
0c5efa5
Merge branch 'main' into fix-issue-2656
WilliamBergamin Nov 5, 2026
026874a
fix(socket-mode): reject start() with the typed error in every connec…
WilliamBergamin Nov 5, 2026
a3fbb4e
refactor(socket-mode): rename SMDisconnectedWhileConnectingError to S…
WilliamBergamin Nov 5, 2026
73a877e
Merge branch 'main' into fix-issue-2656
WilliamBergamin Nov 6, 2026
4482a23
fix(socket-mode): emit disconnected when a reconnect gives up
WilliamBergamin Nov 6, 2026
0a7371d
fix(socket-mode): skip the reconnect delay once disconnect() is called
WilliamBergamin Nov 6, 2026
6cd76f8
test(socket-mode): hold the handshake on a real socket
WilliamBergamin Nov 6, 2026
429a514
docs(socket-mode): explain how a failed reconnect now ends the process
WilliamBergamin Nov 6, 2026
c7e0441
refactor(socket-mode): cancel the reconnect delay with an AbortSignal
WilliamBergamin Nov 6, 2026
501fea2
refactor(socket-mode): import setTimeout from timers/promises without…
WilliamBergamin Nov 6, 2026
6815c93
Update .changeset/socket-mode-reconnect-rejections.md
WilliamBergamin Nov 6, 2026
d26bb3d
fix(socket-mode): swallow only an aborted reconnect, keep real failur…
WilliamBergamin Nov 6, 2026
f17fd06
refactor(socket-mode): emit close with the closed SlackWebSocket
WilliamBergamin Nov 9, 2026
05e173b
fix(socket-mode): skip a reconnect while one is waiting or fetching i…
WilliamBergamin Nov 9, 2026
3b693b4
fix(socket-mode): check the session signal captured before each await
WilliamBergamin Nov 9, 2026
931a3cc
test(socket-mode): drive the fails-for-good reconnect through the rea…
WilliamBergamin Nov 9, 2026
380f138
test(socket-mode): cover a restart during the reconnect wait end to end
WilliamBergamin Nov 9, 2026
0b57999
docs(socket-mode): note the restart fix in the changeset
WilliamBergamin Nov 9, 2026
011dc33
refactor(socket-mode): track pending connects in a set
WilliamBergamin Nov 9, 2026
5697065
refactor(socket-mode): catch only the aborted reconnect wait
WilliamBergamin Nov 9, 2026
29d6250
refactor(socket-mode): name the pending connect set connectOperations
WilliamBergamin Nov 9, 2026
41e1339
test(socket-mode): map coverage to TypeScript source lines
WilliamBergamin Nov 9, 2026
0299c76
fix(socket-mode): forget an aborted session's pending connects on res…
WilliamBergamin Nov 9, 2026
7aa5170
test(socket-mode): hold reconnect URLs with the shared fake client
WilliamBergamin Nov 9, 2026
1606941
test(socket-mode): drive the handshake tests with the fake WebSocket
WilliamBergamin Nov 9, 2026
74c7594
test(socket-mode): make the reconnect tests fail when the guard they …
WilliamBergamin Nov 9, 2026
dea9b45
fix(socket-mode): open the replacement connection before closing the …
WilliamBergamin Nov 9, 2026
4eadbb4
Merge branch 'main' into fix-issue-2656
WilliamBergamin Nov 9, 2026
e1ce02b
fix(socket-mode): back off on disconnect reasons other than a refresh
WilliamBergamin Nov 10, 2026
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
9 changes: 9 additions & 0 deletions .changeset/socket-mode-reconnect-rejections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@slack/socket-mode": minor
---

Fix an unhandled promise rejection of `undefined` that could crash the process when `disconnect()` raced a pending automatic reconnect ([#2656](https://github.com/slackapi/node-slack-sdk/issues/2656)). A reconnect that fails for good still rejects as before, with its cause.

Also, `disconnect()` called before `start()` finishes connecting (while fetching the WebSocket URL, waiting to retry it, or during the WebSocket handshake) no longer leaves a connection opening afterwards: `start()` rejects with the new `SMConnectAbortedError` instead of hanging or rejecting with `undefined`.

Calling `start()` right after `disconnect()`, while a reconnect is waiting or fetching its URL, now opens one connection instead of two.
7 changes: 7 additions & 0 deletions .changeset/socket-mode-refresh-overlap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@slack/socket-mode": patch
---

Fix a 5 to 12 second gap without a connection each time Slack refreshes a Socket Mode connection ([#2767](https://github.com/slackapi/node-slack-sdk/issues/2767)). On a `disconnect` message with the `warning` or `refresh_requested` reason, the client now opens the replacement connection right away, without a reconnect backoff, and leaves the old connection open until Slack sends it a `disconnect` message too, so payloads Slack still sends on it during the switch are received instead of dropped. Each payload is acknowledged on the connection it arrived on. While the replacement is being opened, the current connection keeps serving, and overlapping `disconnect` messages open one replacement. A replacement that fails for good rejects with its cause, as a failed reconnect does. Any other reason, such as `link_disabled`, still closes the connection first and reconnects after the usual delay.

**Note:** during each refresh, a client now holds two connections for about 60 seconds, until Slack sends the old one its own `disconnect` (`refresh_requested`, about a minute after the `warning`). Both count toward Slack's limit of 10 open Socket Mode connections per app, so apps that run many instances at once should leave room for one extra connection per instance while they refresh.
14 changes: 7 additions & 7 deletions packages/socket-mode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ The client's connection to Slack has a lifecycle. This means the client can be s

In the table below, the client's states are listed, which are also the names of the events you can use to observe the transition to that state. The table also includes descriptions for the states and arguments that a listener would receive.

| Event Name | Arguments | Description |
| --------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `connecting` | | The client is in the process of connecting to the platform. |
| `connected` | | The client is connected to the platform and incoming events will start being emitted. |
| `disconnecting` | | The client is no longer connected to the platform and cleaning up its resources. It will soon transition to `disconnected`. |
| `reconnecting` | | The client is no longer connected to the platform and cleaning up its resources. It will soon transition to `connecting`. |
| `disconnected` | `(error)` | The client is not connected to the platform. This is a steady state - no attempt to connect is occurring. The `error` argument will be `undefined` when the client initiated the disconnect (normal). |
| Event Name | Arguments | Description |
| --------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `connecting` | | The client is in the process of connecting to the platform. |
| `connected` | | The client is connected to the platform and incoming events will start being emitted. |
| `disconnecting` | | The client is no longer connected to the platform and cleaning up its resources. It will soon transition to `disconnected`. |
| `reconnecting` | | The client is replacing its connection. On a Slack refresh, the current connection keeps serving after the new one is `connected`, until Slack sends it `disconnect`. It will soon transition to `connecting`. |
| `disconnected` | `(error)` | The client is not connected to the platform. This is a steady state - no attempt to connect is occurring. The `error` argument will be `undefined` when the client initiated the disconnect (normal). |

The client also emits events that are part of its lifecycle, but aren't states. Instead, they represent specific moments that might be helpful to your app. The following table lists these events, their description, and includes the arguments that a listener would receive.

Expand Down
2 changes: 1 addition & 1 deletion packages/socket-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"docs": "npx typedoc --plugin typedoc-plugin-markdown",
"prepack": "npm run build",
"test": "npm run test:unit && npm run test:integration",
"test:coverage": "npm run build && bash -c 'node --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=lcov.info --test-reporter=junit --test-reporter-destination=test-results.xml --import tsx --test src/*.test.ts test/integrations/*.test.js'",
"test:coverage": "npm run build && bash -c 'node --enable-source-maps --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=lcov.info --test-reporter=junit --test-reporter-destination=test-results.xml --import tsx --test src/*.test.ts test/integrations/*.test.js'",
"test:integration": "npm run build && bash -c 'node --import tsx --test test/integrations/*.test.js'",
"test:types": "tsd",
"test:unit": "npm run build && bash -c 'node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=junit --test-reporter-destination=test-results.xml --import tsx --test src/*.test.ts'",
Expand Down
48 changes: 48 additions & 0 deletions packages/socket-mode/src/SlackWebSocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ describe('SlackWebSocket', () => {
ws.dispatchEvent(new ErrorEvent('error', { error: new Error('boom'), message: 'boom' }));
sinon.assert.calledOnce(discStub);
});

it('should emit "ws_message" with the receiving SlackWebSocket, so the client can tell connections apart', () => {
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 client = new EventEmitter();
const received = sandbox.spy();
client.on('ws_message', received);
const sws = new SlackWebSocket({
url: 'whatevs',
client,
clientPingTimeoutMS: 1,
serverPingTimeoutMS: 1,
});
sws.connect();
ws.dispatchEvent(new MessageEvent('message', { data: '{"type":"hello"}' }));
sinon.assert.calledOnceWithExactly(received, '{"type":"hello"}', false, sws);
});
});

describe('disconnect() with an unresponsive peer', () => {
Expand Down Expand Up @@ -358,6 +388,24 @@ describe('SlackWebSocket', () => {
});
});

describe('close notification', () => {
it('should emit "close" with the closed SlackWebSocket, so the client can tell connections apart', () => {
const client = new EventEmitter();
const closed = sandbox.spy();
client.on('close', closed);
const sws = new SlackWebSocket({
url: 'ws://127.0.0.1/',
client,
clientPingTimeoutMS: 1,
serverPingTimeoutMS: 1,
});

sws.disconnect();

sinon.assert.calledOnceWithExactly(closed, sws);
});
});

describe('buildDefaultDispatcher() connect hook', () => {
type ConnectCb = (err: Error | null, socket: unknown) => void;
type ConnectFn = (opts: unknown, cb: ConnectCb) => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/socket-mode/src/SlackWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class SlackWebSocket {
return;
}
const isBinary = typeof event.data !== 'string';
this.options.client.emit('ws_message', event.data, isBinary);
this.options.client.emit('ws_message', event.data, isBinary, this);
};
this.websocket.addEventListener('message', this.messageHandler);

Expand Down Expand Up @@ -296,7 +296,7 @@ export class SlackWebSocket {
clearTimeout(this.closeHandshakeTimeout);
// Emit event back to client letting it know connection has closed (in case it needs to reconnect if
// reconnecting is enabled)
this.options.client.emit('close');
this.options.client.emit('close', this);
}

/**
Expand Down
Loading
Loading