File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,10 +5,11 @@ export namespace SessionRetry {
55 export const RETRY_INITIAL_DELAY = 2000
66 export const RETRY_BACKOFF_FACTOR = 2
77 export const RETRY_MAX_DELAY_NO_HEADERS = 30_000 // 30 seconds
8+ export const RETRY_MAX_DELAY = 2_147_483_647 // max 32-bit signed integer for setTimeout
89
910 export async function sleep ( ms : number , signal : AbortSignal ) : Promise < void > {
1011 return new Promise ( ( resolve , reject ) => {
11- const timeout = setTimeout ( resolve , ms )
12+ const timeout = setTimeout ( resolve , Math . min ( ms , RETRY_MAX_DELAY ) )
1213 signal . addEventListener (
1314 "abort" ,
1415 ( ) => {
Original file line number Diff line number Diff line change @@ -58,6 +58,26 @@ describe("session.retry.delay", () => {
5858 const longError = apiError ( { "retry-after-ms" : "700000" } )
5959 expect ( SessionRetry . delay ( 1 , longError ) ) . toBe ( 700000 )
6060 } )
61+
62+ test ( "sleep caps delay to max 32-bit signed integer to avoid TimeoutOverflowWarning" , async ( ) => {
63+ const controller = new AbortController ( )
64+
65+ const warnings : string [ ] = [ ]
66+ const originalWarn = process . emitWarning
67+ process . emitWarning = ( warning : string | Error ) => {
68+ warnings . push ( typeof warning === "string" ? warning : warning . message )
69+ }
70+
71+ const promise = SessionRetry . sleep ( 2_560_914_000 , controller . signal )
72+ controller . abort ( )
73+
74+ try {
75+ await promise
76+ } catch { }
77+
78+ process . emitWarning = originalWarn
79+ expect ( warnings . some ( ( w ) => w . includes ( "TimeoutOverflowWarning" ) ) ) . toBe ( false )
80+ } )
6181} )
6282
6383describe ( "session.message-v2.fromError" , ( ) => {
You can’t perform that action at this time.
0 commit comments