Skip to content

Commit eaed9a1

Browse files
authored
fix(sandbox): skip interval ticks while the previous one is still in flight (#10063)
1 parent bc438fc commit eaed9a1

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

‎apps/sim/lib/execution/isolated-vm-worker.cjs‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -848,14 +848,20 @@ async function executeTask(request, executionId) {
848848
admitTimer(fnRef, true)
849849
const id = nextTimerId++
850850
const delay = Math.max(1, Math.min(Number(unwrapPrimitive(msRef)) || 1, timeoutMs))
851+
// A tick is skipped while the previous one is still in flight, so a busy isolate
852+
// cannot pile undelivered callbacks onto the worker heap.
853+
let tickInFlight = false
854+
const settleTick = () => {
855+
tickInFlight = false
856+
}
851857
const nodeTimer = setInterval(() => {
852-
const entry = timers.get(id)
853-
if (!entry) return
858+
if (tickInFlight || !timers.has(id)) return
854859
try {
855-
fnRef.applyIgnored(undefined, [], { timeout: timeoutMs })
860+
fnRef.apply(undefined, [], { timeout: timeoutMs }).then(settleTick, settleTick)
861+
tickInFlight = true
856862
} catch {
857-
// isolate disposed — callback silently dropped; the sweep on dispose
858-
// clears the Node interval and releases the fn ref.
863+
// isolate disposed — the sweep on dispose clears the Node interval and
864+
// releases the fn ref.
859865
}
860866
}, delay)
861867
timers.set(id, { nodeTimer, fnRef, recurring: true })
@@ -969,7 +975,10 @@ async function executeTask(request, executionId) {
969975
if (typeof fn !== 'function') {
970976
throw new TypeError('setInterval requires a function callback');
971977
}
972-
return __setIntervalRef.applySync(undefined, [fn, ms], {
978+
// Each tick resolves back to the host, so the wrapper keeps the callback's
979+
// return value inside the isolate.
980+
const tick = () => { fn(); };
981+
return __setIntervalRef.applySync(undefined, [tick, ms], {
973982
arguments: { reference: true },
974983
});
975984
};

‎apps/sim/scripts/test-isolated-vm-remote-e2e.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,19 @@ async function run() {
640640
assert.equal(reused.error, undefined, reused.error?.message)
641641
})
642642

643+
await check('keeps a busy task from queuing interval ticks on the shared worker', async () => {
644+
const busy = await runTask(
645+
// utils-lint-allow: runs inside the isolate, where @sim/utils is unavailable
646+
'let ticks = 0; setInterval(() => { ticks++ }, 1); const end = Date.now() + 500; while (Date.now() < end) {} await new Promise((resolve) => setTimeout(resolve, 0)); if (ticks > 5) throw new Error("queued ticks: " + ticks)'
647+
)
648+
assert.equal(busy.error, undefined, busy.error?.message)
649+
const idle = await runTask(
650+
// utils-lint-allow: runs inside the isolate, where @sim/utils is unavailable
651+
'let ticks = 0; setInterval(() => { ticks++ }, 5); await new Promise((resolve) => setTimeout(resolve, 300)); if (ticks < 20) throw new Error("ticks: " + ticks)'
652+
)
653+
assert.equal(idle.error, undefined, idle.error?.message)
654+
})
655+
643656
await check('rejects a connection with the wrong token', async () => {
644657
const ws = await openSocket(probeHost, authFrame(generateRandomHex(32)))
645658
assert.equal(await closeCode(ws), 4401)

0 commit comments

Comments
 (0)