From dbddf4093f30cf76018a558c81a56c32ee4eac87 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 30 May 2026 17:38:58 -0400 Subject: [PATCH] fix(utils): clamp shared sleep timers --- src/utils.test.ts | 17 +++++++++++++++++ src/utils.ts | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/utils.test.ts b/src/utils.test.ts index da8648bb4e9..3e6aceeb06e 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -1,6 +1,7 @@ import fs from "node:fs"; import path from "node:path"; import { describe, expect, it, vi } from "vitest"; +import { MAX_TIMER_TIMEOUT_MS } from "./shared/number-coercion.js"; import { withTempDir } from "./test-helpers/temp-dir.js"; import { ensureDir, @@ -33,6 +34,22 @@ describe("sleep", () => { vi.useRealTimers(); } }); + + it("clamps oversized sleep delays before scheduling", async () => { + vi.useFakeTimers(); + const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout"); + try { + const promise = sleep(Number.MAX_SAFE_INTEGER); + + expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS); + + vi.advanceTimersByTime(MAX_TIMER_TIMEOUT_MS); + await expect(promise).resolves.toBeUndefined(); + } finally { + setTimeoutSpy.mockRestore(); + vi.useRealTimers(); + } + }); }); describe("resolveConfigDir", () => { diff --git a/src/utils.ts b/src/utils.ts index 997cf71306e..6833c6a623f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,6 +8,7 @@ import { resolveRequiredHomeDir, } from "./infra/home-dir.js"; import { isPlainObject } from "./infra/plain-object.js"; +import { resolveTimerTimeoutMs } from "./shared/number-coercion.js"; export { escapeRegExp } from "./shared/regexp.js"; export async function ensureDir(dir: string) { @@ -57,7 +58,7 @@ export function normalizeE164(number: string): string { } export function sleep(ms: number) { - return new Promise((resolve) => setTimeout(resolve, ms)); + return new Promise((resolve) => setTimeout(resolve, resolveTimerTimeoutMs(ms, 0, 0))); } function isHighSurrogate(codeUnit: number): boolean {