Skip to content

Commit cc0315a

Browse files
sjf-oasjf
andauthored
fix(slack): recover identity after transient startup auth failure (#117060)
* fix(slack): retry transient startup auth once * fix(slack): allow two startup auth retries * test(slack): cover stalled startup auth retries --------- Co-authored-by: Sarah Fortune <[email protected]>
1 parent a6e2cd3 commit cc0315a

4 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎extensions/slack/src/client.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ describe("slack web client config", () => {
234234
expect(WebClient).toHaveBeenCalledWith("xoxb-startup", {
235235
fetch: customFetch,
236236
rejectRateLimitedCalls: true,
237-
retryConfig: { retries: 0 },
237+
retryConfig: { retries: 2, minTimeout: 0 },
238238
slackApiUrl: "https://slack.test/api/",
239239
timeout: 10_000,
240240
});

‎extensions/slack/src/client.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ export function createSlackReadClient(token: string, options: WebClientOptions =
3737
}
3838

3939
export function createSlackStartupAuthClient(token: string, options: WebClientOptions = {}) {
40-
// Startup degrades after auth.test fails, so terminate this one-shot request without
41-
// imposing the same short deadline on Bolt's long-lived client.
40+
// Startup identity stays degraded until restart after auth.test fails. Retry two transient
41+
// transport failures before committing that state, without delaying on Slack rate limits.
4242
return createSlackWebClient(token, {
4343
...options,
4444
rejectRateLimitedCalls: true,
45-
retryConfig: { retries: 0 },
45+
retryConfig: { retries: 2, minTimeout: 0 },
4646
timeout: 10_000,
4747
});
4848
}

‎extensions/slack/src/client.web-api.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import { createServer, type Server } from "node:http";
33
import type { AddressInfo } from "node:net";
44
import { WebClient } from "@slack/web-api";
5-
import { afterEach, describe, expect, it } from "vitest";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
66
import {
77
createSlackLookupClient,
88
createSlackReadClient,
9+
createSlackStartupAuthClient,
910
createSlackWebClient,
1011
getSlackListenerUploadCompletionClient,
1112
} from "./client.js";
@@ -187,6 +188,42 @@ afterEach(() => {
187188
});
188189

189190
describe("Slack Web API routing", () => {
191+
it("retries two transient startup auth failures", async () => {
192+
const fetchMock = vi
193+
.fn()
194+
.mockRejectedValueOnce(new Error("request timed out"))
195+
.mockRejectedValueOnce(new Error("connection reset"))
196+
.mockResolvedValueOnce(
197+
new Response(JSON.stringify({ ok: true, team_id: "TMOCK", user_id: "UMOCK" }), {
198+
status: 200,
199+
}),
200+
);
201+
const client = createSlackStartupAuthClient("startup-fixture", {
202+
fetch: fetchMock as never,
203+
});
204+
205+
await expect(client.auth.test()).resolves.toMatchObject({
206+
ok: true,
207+
team_id: "TMOCK",
208+
user_id: "UMOCK",
209+
});
210+
expect(fetchMock).toHaveBeenCalledTimes(3);
211+
});
212+
213+
it("does not retry a permanent startup auth error", async () => {
214+
const fetchMock = vi.fn().mockResolvedValue(
215+
new Response(JSON.stringify({ ok: false, error: "invalid_auth" }), {
216+
status: 200,
217+
}),
218+
);
219+
const client = createSlackStartupAuthClient("invalid-fixture", {
220+
fetch: fetchMock as never,
221+
});
222+
223+
await expect(client.auth.test()).rejects.toThrow("invalid_auth");
224+
expect(fetchMock).toHaveBeenCalledTimes(1);
225+
});
226+
190227
it("aborts a stalled-header lookup after one request", async () => {
191228
for (const key of TEST_ENV_KEYS) {
192229
delete process.env[key];

‎extensions/slack/src/monitor/provider.auth-test-token.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ describe("auth.test boot call", () => {
271271
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() },
272272
});
273273
try {
274-
await vi.waitFor(() => expect(appStartMock).toHaveBeenCalledTimes(1), { timeout: 12_000 });
274+
await vi.waitFor(() => expect(appStartMock).toHaveBeenCalledTimes(1), { timeout: 35_000 });
275275
await vi.waitFor(() => expect(events).toContain("socket-closed"), { timeout: 1_000 });
276276

277-
expect(server.requestCount).toBe(1);
277+
expect(server.requestCount).toBe(3);
278278
expect(server.requestUrl).toBe("/api/auth.test");
279279
expect(events).toContain("auth-settled");
280280
expect(events.indexOf("auth-settled")).toBeLessThan(events.indexOf("app-start"));
@@ -286,7 +286,7 @@ describe("auth.test boot call", () => {
286286
await monitor.run;
287287
await server.close();
288288
}
289-
}, 20_000);
289+
}, 40_000);
290290

291291
it("preserves workspace startup when auth.test omits app_id", async () => {
292292
getSlackClient().auth.test.mockResolvedValueOnce({

0 commit comments

Comments
 (0)