Skip to content

Commit 709e540

Browse files
committed
fix(mcp): bound a code-mode run by one deadline that includes sandbox queue time
1 parent 82e19a3 commit 709e540

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

‎apps/sim/lib/api/mcp/code-mode.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
3333
const MAX_CONCURRENT_CALLS = 8
3434
const 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
3537
const NOT_SENT = 'The program ended before this call was sent.'
3638

3739
export 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

Comments
 (0)