Skip to content

Commit 03a6dc6

Browse files
committed
fix(sandbox): thread AbortSignal into runSandboxTask at every call site
Three remaining callers of runSandboxTask were not threading a cancellation signal, so a client disconnect mid-compile left the pool slot occupied for the full 60s task timeout. Matching the pattern the pptx-preview route already uses. - apps/sim/app/api/files/serve/[...path]/route.ts — GET forwards `request.signal` into handleLocalFile / handleCloudProxy, which forward into compileDocumentIfNeeded, which forwards into runSandboxTask. - apps/sim/lib/copilot/tools/server/files/workspace-file.ts — passes `context.abortSignal` (transport/user stop) into runSandboxTask. - apps/sim/lib/copilot/tools/server/files/edit-content.ts — same. Smoke: simulated client disconnect at t=1000ms during a task that would otherwise have waited 10s. The pool slot unwinds at t=1002ms with AbortError; previously would have sat 60s until the task-level timeout. Made-with: Cursor
1 parent 928f584 commit 03a6dc6

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

apps/sim/app/api/files/serve/[...path]/route.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ async function compileDocumentIfNeeded(
6464
filename: string,
6565
workspaceId: string | undefined,
6666
raw: boolean,
67-
ownerKey: string | undefined
67+
ownerKey: string | undefined,
68+
signal: AbortSignal | undefined
6869
): Promise<{ buffer: Buffer; contentType: string }> {
6970
if (raw) return { buffer, contentType: getContentType(filename) }
7071

@@ -91,7 +92,7 @@ async function compileDocumentIfNeeded(
9192
const compiled = await runSandboxTask(
9293
format.taskId,
9394
{ code, workspaceId: workspaceId || '' },
94-
{ ownerKey }
95+
{ ownerKey, signal }
9596
)
9697
compiledCacheSet(cacheKey, compiled)
9798
return { buffer: compiled, contentType: format.contentType }
@@ -155,10 +156,10 @@ export async function GET(
155156
const userId = authResult.userId
156157

157158
if (isUsingCloudStorage()) {
158-
return await handleCloudProxy(cloudKey, userId, raw)
159+
return await handleCloudProxy(cloudKey, userId, raw, request.signal)
159160
}
160161

161-
return await handleLocalFile(cloudKey, userId, raw)
162+
return await handleLocalFile(cloudKey, userId, raw, request.signal)
162163
} catch (error) {
163164
logger.error('Error serving file:', error)
164165

@@ -173,7 +174,8 @@ export async function GET(
173174
async function handleLocalFile(
174175
filename: string,
175176
userId: string,
176-
raw: boolean
177+
raw: boolean,
178+
signal: AbortSignal | undefined
177179
): Promise<NextResponse> {
178180
const ownerKey = `user:${userId}`
179181
try {
@@ -209,7 +211,8 @@ async function handleLocalFile(
209211
displayName,
210212
workspaceId,
211213
raw,
212-
ownerKey
214+
ownerKey,
215+
signal
213216
)
214217

215218
logger.info('Local file served', { userId, filename, size: fileBuffer.length })
@@ -229,7 +232,8 @@ async function handleLocalFile(
229232
async function handleCloudProxy(
230233
cloudKey: string,
231234
userId: string,
232-
raw = false
235+
raw = false,
236+
signal: AbortSignal | undefined = undefined
233237
): Promise<NextResponse> {
234238
const ownerKey = `user:${userId}`
235239
try {
@@ -268,7 +272,8 @@ async function handleCloudProxy(
268272
displayName,
269273
workspaceId,
270274
raw,
271-
ownerKey
275+
ownerKey,
276+
signal
272277
)
273278

274279
logger.info('Cloud file served', {

apps/sim/lib/copilot/tools/server/files/edit-content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export const editContentServerTool: BaseServerTool<EditContentArgs, EditContentR
240240
await runSandboxTask(
241241
docInfo.taskId!,
242242
{ code: finalContent, workspaceId },
243-
{ ownerKey: `user:${context.userId}` }
243+
{ ownerKey: `user:${context.userId}`, signal: context.abortSignal }
244244
)
245245
} catch (err) {
246246
const msg = err instanceof Error ? err.message : String(err)

apps/sim/lib/copilot/tools/server/files/workspace-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export const workspaceFileServerTool: BaseServerTool<WorkspaceFileArgs, Workspac
201201
await runSandboxTask(
202202
docInfo.taskId!,
203203
{ code: content, workspaceId },
204-
{ ownerKey: `user:${context.userId}` }
204+
{ ownerKey: `user:${context.userId}`, signal: context.abortSignal }
205205
)
206206
} catch (err) {
207207
const msg = err instanceof Error ? err.message : String(err)

0 commit comments

Comments
 (0)