Skip to content
Open
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
29 changes: 26 additions & 3 deletions src/runtime/api/BunObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,9 +1140,32 @@ pub(crate) fn sleep_sync(
)));
}

std::thread::sleep(core::time::Duration::from_millis(
u64::try_from(milliseconds).expect("int cast"),
));
let duration = core::time::Duration::from_millis(milliseconds as u64);

// In a worker, std::thread::sleep cannot be interrupted by worker.terminate():
// the parent thread's notify_need_termination only fires a JSC VMTrap (checked
// at JS safepoints) and wakes the event loop poll, neither of which unblocks a
// parked nanosleep/Sleep. Slice the sleep and poll the termination flag between
// slices so terminate() takes effect within SLEEP_SYNC_TERMINATE_SLICE instead
// of the full requested duration. The VMTrap then throws TerminationException
// at the next safepoint after we return.
if let Some(worker) = global_object.bun_vm().worker_ref() {
const SLEEP_SYNC_TERMINATE_SLICE: core::time::Duration =
core::time::Duration::from_millis(100);
let deadline = std::time::Instant::now() + duration;
loop {
if worker.has_requested_terminate() {
break;
}
let now = std::time::Instant::now();
if now >= deadline {
break;
}
std::thread::sleep((deadline - now).min(SLEEP_SYNC_TERMINATE_SLICE));
}
} else {
std::thread::sleep(duration);
}
Ok(JSValue::UNDEFINED)
}

Expand Down
43 changes: 43 additions & 0 deletions test/js/web/workers/worker-terminate-lifetime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,46 @@ test.skipIf(!isASAN)(
},
timeout,
);

// Bun.sleepSync was a single uninterruptible std::thread::sleep, so a worker
// parked in a long sleepSync never observed the parent's terminate(): VMTraps
// only fire at JS safepoints and the event-loop wakeup cannot unblock nanosleep.
test(
"terminate() interrupts a worker blocked in Bun.sleepSync",
async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const w = new Worker(
"data:text/javascript," + encodeURIComponent(
'postMessage("sleeping"); Bun.sleepSync(600000);'
),
);
w.addEventListener("close", () => {
console.log("CLOSED");
process.exit(0);
});
// postMessage enqueue -> return -> sleepSync is synchronous on the worker
// thread; by the time this handler runs on the parent, the worker is
// already inside the sleep.
w.addEventListener("message", () => w.terminate());
setTimeout(() => {
console.log("HUNG");
process.exit(1);
}, 10000);
`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(stdout).toBe("CLOSED\n");
expect(exitCode).toBe(0);
},
timeout,
);
Loading