Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions apps/sim/lib/execution/isolated-vm-worker.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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(); };
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
return __setIntervalRef.applySync(undefined, [tick, ms], {
arguments: { reference: true },
});
};
Expand Down
13 changes: 13 additions & 0 deletions apps/sim/scripts/test-isolated-vm-remote-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading