Skip to content

Commit 5db7961

Browse files
committed
refactor(mothership): wait on a workflow tool's run with the shared interruptible sleep
1 parent 2b23efe commit 5db7961

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

‎apps/sim/lib/mothership/tools/handlers/workflow/mutations.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createLogger } from '@sim/logger'
2+
import { interruptibleSleep } from '@sim/utils/helpers'
23
import { generateId } from '@sim/utils/id'
34
import { filterUndefined, isPlainRecord, isRecordLike } from '@sim/utils/object'
45
import { createCopilotWorkspaceApiKey } from '@/lib/api-key/application/create-api-key'
@@ -277,18 +278,16 @@ async function answerWithinWait(
277278
})
278279
const settled = run({ ...lifecycle, executionId, onDispatch: () => markDispatched() })
279280
retainSandboxExecution(settled)
280-
let timer: ReturnType<typeof setTimeout> | undefined
281-
const waited = new Promise<null>((resolve) => {
282-
timer = setTimeout(() => resolve(null), waitMs)
283-
})
281+
const stopWaiting = new AbortController()
282+
const waited = interruptibleSleep(waitMs, stopWaiting.signal).then(() => null)
284283
try {
285284
const result = await Promise.race([settled, waited])
286285
if (result) return result
287286
// Only a run handed to the executor is answered running; one still in preflight settles first.
288287
const started = await Promise.race([settled, dispatched])
289288
if (started) return started
290289
} finally {
291-
clearTimeout(timer)
290+
stopWaiting.abort()
292291
}
293292
logger.info('Workflow tool run outlasted its wait; answering it still running', {
294293
toolCallId: context.toolCallId,

0 commit comments

Comments
 (0)