Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ci-scripts): clear the thread-validation timeout when fetch rejects

Etienne Lescot5a9d437b2e01
diff --git a/.github/scripts/discord-thread-validator.mjs b/.github/scripts/discord-thread-validator.mjs
index 88ae9c7ea..2ab906833 100644
--- a/.github/scripts/discord-thread-validator.mjs
+++ b/.github/scripts/discord-thread-validator.mjs
@@ -8,14 +8,13 @@ export async function validateThreadChannel(threadId, prNumber, { botToken, foru
return false;
}
const VALIDATION_TIMEOUT_MS = 5_000;
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), VALIDATION_TIMEOUT_MS);
try {
- const controller = new AbortController();
- const timeout = setTimeout(() => controller.abort(), VALIDATION_TIMEOUT_MS);
const res = await fetch(`https://discord.com/api/v10/channels/${threadId}`, {
headers: { Authorization: `Bot ${botToken}` },
signal: controller.signal,
});
- clearTimeout(timeout);
if (!res.ok) {
warning(`Thread validation failed: channel ${threadId} returned ${res.status}`);
return false;
@@ -38,5 +37,12 @@ export async function validateThreadChannel(threadId, prNumber, { botToken, foru
} catch (err) {
warning(`Thread validation threw: ${err && err.message ? err.message : err}`);
return false;
+ } finally {
+ // `finally`, not a line after the await: when `fetch` rejects — a real network
+ // error — the timer would otherwise stay armed and fire `controller.abort()`
+ // five seconds later, long after this returned. Under Vitest that lands after
+ // the worker has torn down, which is an intermittent post-run error rather
+ // than a failing test. Same shape as `callDiscord` in discord-bot-api.mjs.
+ clearTimeout(timeout);
}
}
diff --git a/.github/scripts/discord-thread-validator.test.mjs b/.github/scripts/discord-thread-validator.test.mjs
index f2c686b85..7e2718d1e 100644
--- a/.github/scripts/discord-thread-validator.test.mjs
+++ b/.github/scripts/discord-thread-validator.test.mjs
@@ -108,4 +108,22 @@ describe("validateThreadChannel", () => {
const result = await validateThreadChannel("500", number, { botToken });
expect(result).toBe(false);
});
+
+ it("leaves no timer armed once it has returned, on either path", async () => {
+ vi.useFakeTimers();
+ try {
+ vi.mocked(fetch).mockRejectedValue(new Error("network error"));
+ await validateThreadChannel("500", number, { botToken });
+ // The failing path is the one that used to leak: `clearTimeout` sat after
+ // the `await`, so a rejected fetch skipped it and left the 5s abort timer
+ // running past the end of the test.
+ expect(vi.getTimerCount()).toBe(0);
+
+ vi.mocked(fetch).mockResolvedValue({ ok: false, status: 404 });
+ await validateThreadChannel("404", number, { botToken });
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
});