From 76639681315fd73b4fe839f9d93b1ca8dedf3dde Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 14 Nov 2026 15:00:31 -0800 Subject: [PATCH] fix(sandbox): skip interval ticks while the previous one is still in flight --- apps/sim/lib/execution/isolated-vm-worker.cjs | 21 +++++++++++++------ .../scripts/test-isolated-vm-remote-e2e.ts | 13 ++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/apps/sim/lib/execution/isolated-vm-worker.cjs b/apps/sim/lib/execution/isolated-vm-worker.cjs index 6a6c814bc4c..0a886c3cae3 100644 --- a/apps/sim/lib/execution/isolated-vm-worker.cjs +++ b/apps/sim/lib/execution/isolated-vm-worker.cjs @@ -848,14 +848,20 @@ async function executeTask(request, executionId) { admitTimer(fnRef, true) const id = nextTimerId++ const delay = Math.max(1, Math.min(Number(unwrapPrimitive(msRef)) || 1, timeoutMs)) + // A tick is skipped while the previous one is still in flight, so a busy isolate + // cannot pile undelivered callbacks onto the worker heap. + let tickInFlight = false + const settleTick = () => { + tickInFlight = false + } const nodeTimer = setInterval(() => { - const entry = timers.get(id) - if (!entry) return + if (tickInFlight || !timers.has(id)) return try { - fnRef.applyIgnored(undefined, [], { timeout: timeoutMs }) + fnRef.apply(undefined, [], { timeout: timeoutMs }).then(settleTick, settleTick) + tickInFlight = true } catch { - // isolate disposed — callback silently dropped; the sweep on dispose - // clears the Node interval and releases the fn ref. + // isolate disposed — the sweep on dispose clears the Node interval and + // releases the fn ref. } }, delay) timers.set(id, { nodeTimer, fnRef, recurring: true }) @@ -969,7 +975,10 @@ async function executeTask(request, executionId) { if (typeof fn !== 'function') { throw new TypeError('setInterval requires a function callback'); } - return __setIntervalRef.applySync(undefined, [fn, ms], { + // Each tick resolves back to the host, so the wrapper keeps the callback's + // return value inside the isolate. + const tick = () => { fn(); }; + return __setIntervalRef.applySync(undefined, [tick, ms], { arguments: { reference: true }, }); }; diff --git a/apps/sim/scripts/test-isolated-vm-remote-e2e.ts b/apps/sim/scripts/test-isolated-vm-remote-e2e.ts index e4ea4cc309f..ff4742546d8 100644 --- a/apps/sim/scripts/test-isolated-vm-remote-e2e.ts +++ b/apps/sim/scripts/test-isolated-vm-remote-e2e.ts @@ -640,6 +640,19 @@ async function run() { assert.equal(reused.error, undefined, reused.error?.message) }) + await check('keeps a busy task from queuing interval ticks on the shared worker', async () => { + const busy = await runTask( + // utils-lint-allow: runs inside the isolate, where @sim/utils is unavailable + '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)' + ) + assert.equal(busy.error, undefined, busy.error?.message) + const idle = await runTask( + // utils-lint-allow: runs inside the isolate, where @sim/utils is unavailable + 'let ticks = 0; setInterval(() => { ticks++ }, 5); await new Promise((resolve) => setTimeout(resolve, 300)); if (ticks < 20) throw new Error("ticks: " + ticks)' + ) + assert.equal(idle.error, undefined, idle.error?.message) + }) + await check('rejects a connection with the wrong token', async () => { const ws = await openSocket(probeHost, authFrame(generateRandomHex(32))) assert.equal(await closeCode(ws), 4401)