@@ -32,6 +32,8 @@ const MAX_CALLS_PER_RUN = 200
3232/** Calls one program may have in flight at once, so a `Promise.all` cannot fan out a whole page of requests. */
3333const MAX_CONCURRENT_CALLS = 8
3434const MAX_CODE_CHARS = 100_000
35+ /** Slack past the isolate's own timeout, so a run that never queued still ends with the isolate's TimeoutError. */
36+ const DEADLINE_GRACE_MS = 1000
3537const NOT_SENT = 'The program ended before this call was sent.'
3638
3739export const codeModeInput = z
@@ -258,6 +260,9 @@ export async function runCodeMode(
258260 }
259261 }
260262
263+ const timeoutMs = input . timeoutMs ?? DEFAULT_TIMEOUT_MS
264+ /** Bounds the whole run, time spent waiting for a sandbox worker included, so the caller hears back within its timeout. */
265+ const deadline = AbortSignal . timeout ( timeoutMs + DEADLINE_GRACE_MS )
261266 let bytes : Buffer | undefined
262267 let runError : unknown
263268 try {
@@ -267,8 +272,8 @@ export async function runCodeMode(
267272 {
268273 ownerKey,
269274 callSimApi,
270- signal : context . signal ,
271- timeoutMs : input . timeoutMs ?? DEFAULT_TIMEOUT_MS ,
275+ signal : AbortSignal . any ( [ context . signal , deadline ] ) ,
276+ timeoutMs,
272277 }
273278 )
274279 } catch ( error ) {
@@ -289,6 +294,16 @@ export async function runCodeMode(
289294 summary ( 'cancelled' )
290295 return failure ( { name : 'AbortError' , message : 'The program was cancelled.' } , calls )
291296 }
297+ if ( deadline . aborted ) {
298+ summary ( 'timeout' )
299+ return failure (
300+ {
301+ name : 'TimeoutError' ,
302+ message : `The program did not finish within ${ timeoutMs } ms, including time spent waiting for a sandbox.` ,
303+ } ,
304+ calls
305+ )
306+ }
292307 if ( runError instanceof SandboxUserCodeError ) {
293308 summary ( runError . name === 'TimeoutError' ? 'timeout' : 'error' )
294309 return failure ( { name : runError . name , message : runError . message } , calls )
0 commit comments