Repository navigation
bun test --isolate: make a finished file's jest timer controls inert #41790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5185,7 +5185,9 @@ impl VirtualMachine { | |
| // graph) for an hour. Every TimeoutObject / ImmediateObject / | ||
| // AbortSignal timeout in the heap belongs to the outgoing file (the | ||
| // new global doesn't exist yet), so drop them eagerly, same as | ||
| // `global_exit()`. Runs no user JS. | ||
| // `global_exit()`. Runs no user JS. So does a `jest.useFakeTimers()` | ||
| // the file left on: the hook puts the thread back on the real clock | ||
| // here, after the last drain above ran the outgoing realm's JS. | ||
|
Comment on lines
+5188
to
+5190
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| // | ||
| // The hook also runs `StatWatcherScheduler::shutdown_for_exit` first: | ||
| // it drains the (already-closed — the caller ran | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4380,6 +4380,17 @@ extern "C" void Zig__GlobalObject__stopActiveDOMObjectsForTestIsolation(Zig::Glo | |
| globalObject->scriptExecutionContext()->prepareForDestruction(); | ||
| } | ||
|
|
||
| // The realm's context went through prepareForDestruction(): a `bun test --isolate` global retired by | ||
| // the function above, or any global once VM teardown began. Script the realm leaked can still run. | ||
|
Comment on lines
+4383
to
+4384
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| extern "C" bool Zig__GlobalObject__isContextStopped(JSC::JSGlobalObject* lexicalGlobalObject) | ||
| { | ||
| auto* globalObject = dynamicDowncast<Zig::GlobalObject>(lexicalGlobalObject); | ||
| if (!globalObject) | ||
| return false; | ||
| auto* context = globalObject->scriptExecutionContext(); | ||
| return !context || context->activeDOMObjectsAreStopped(); | ||
| } | ||
|
|
||
| extern "C" void Zig__GlobalObject__destructOnExit(Zig::GlobalObject* globalObject) | ||
| { | ||
| auto& vm = JSC::getVM(globalObject); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1621,9 +1621,10 @@ fn cron_clear_all_reload(vm: &mut VirtualMachine) { | |
|
|
||
| /// `RuntimeHooks::cancel_all_timers` — cancel every `TimeoutObject` / | ||
| /// `ImmediateObject` still linked in the current thread's timer heap so the | ||
| /// in-heap `+1` ref and the JS pin drop before the GC sweep / `~VM`. | ||
| /// `timer::All` lives in `bun_runtime`; callers (`global_exit`, | ||
| /// `WebWorker::shutdown`) are in `bun_jsc`, hence the hook. | ||
| /// in-heap `+1` ref and the JS pin drop before the GC sweep / `~VM`, and put | ||
| /// the thread back on the real clock. | ||
| /// `timer::All` lives in `bun_runtime`; callers (VM teardown, the `--isolate` | ||
| /// swap) are in `bun_jsc`, hence the hook. | ||
|
Comment on lines
+1624
to
+1627
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| /// | ||
| /// # Safety | ||
| /// `vm` is the live per-thread VM; `runtime_state()` must still be installed. | ||
|
|
@@ -1633,6 +1634,23 @@ unsafe fn cancel_all_timers(vm: *mut VirtualMachine) { | |
| if state.is_null() { | ||
| return; | ||
| } | ||
| // Fake-timer activation lives in the per-thread `timer::All`, not the JS | ||
| // global, so under `--isolate` it belongs to the outgoing file exactly like | ||
| // the timers below: left on, it would route every later file's `setTimeout` | ||
| // into the never-driven fake heap. The swap calls this hook after the last | ||
| // point where the outgoing realm's JS runs as the live realm; anything that | ||
| // realm runs later is inert (`FakeTimers.rs` `called_from_retired_realm`). | ||
| // The fake heap itself stays populated for `cancel_all_timeout_objects`, | ||
| // which walks both heaps. | ||
| // SAFETY: `state` is the live boxed per-thread `RuntimeState`; `vm` per fn | ||
| // contract; `reset_for_isolation` touches only `fake_timers.active`, the | ||
| // `CURRENT_TIME` static and VM fields. | ||
| unsafe { | ||
| let fake_timers = &mut *ptr::addr_of_mut!((*state).timer.fake_timers); | ||
| if fake_timers.is_active() { | ||
| fake_timers.reset_for_isolation((*vm).global()); | ||
| } | ||
| } | ||
| // Drain the `fs.watchFile` scheduler queue while the timer heap and JSC | ||
| // are both still live. Each queued `StatWatcher` holds a `RefPtr` back to | ||
| // the scheduler and the scheduler holds a queue ref on the watcher, so any | ||
|
|
@@ -1717,11 +1735,12 @@ fn stop_dns_for_vm_teardown() -> SweepResult { | |
| /// prepareForDestruction discards the pre-exit queues.) | ||
| pub(crate) fn stop_active_handles_for_test_isolation(vm: &mut VirtualMachine) { | ||
| let _ = vm.event_loop_mut().drain_microtasks(); | ||
| let _ = stop_active_handles(vm, StopReason::TestIsolation); | ||
| let _ = stop_active_handles(StopReason::TestIsolation); | ||
| } | ||
|
|
||
| pub(crate) fn stop_active_handles_for_vm_teardown(vm: &mut VirtualMachine) -> SweepResult { | ||
| stop_active_handles(vm, StopReason::VmTeardown) | ||
| /// `_vm`: the caller holds this thread's VM exclusively, on the JS thread. | ||
| pub(crate) fn stop_active_handles_for_vm_teardown(_vm: &mut VirtualMachine) -> SweepResult { | ||
| stop_active_handles(StopReason::VmTeardown) | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, PartialEq, Eq)] | ||
|
|
@@ -1731,32 +1750,18 @@ enum StopReason { | |
| TestIsolation, | ||
| } | ||
|
|
||
| fn stop_active_handles(vm: &mut VirtualMachine, reason: StopReason) -> SweepResult { | ||
| fn stop_active_handles(reason: StopReason) -> SweepResult { | ||
| let state = runtime_state(); | ||
| if state.is_null() { | ||
| return SweepResult::Idle; | ||
| } | ||
| let mut result = SweepResult::Idle; | ||
| // Fake-timer state lives in the per-thread `timer::All`, not the JS | ||
| // global, so a file that leaves it active routes every later file's | ||
| // `setTimeout` into the never-driven fake heap. Leave the heap itself | ||
| // intact: `swap_global_for_test_isolation` runs `cancel_all_timeout_objects` | ||
| // next, which walks both heaps and releases `TimeoutObject` pins and | ||
| // discards `AbortSignalTimeout` timers at a point where no user JS can | ||
| // touch the outgoing signals. | ||
| { | ||
| let all = timer_all(); | ||
| // SAFETY: `state` is non-null so `timer_all()` is non-null; single | ||
| // JS thread, no re-entry while we hold the field borrow. | ||
| if !all.is_null() && unsafe { (*all).fake_timers.is_active() } { | ||
| let global = vm.global(); | ||
| // SAFETY: as above; only touches `fake_timers.active` and the | ||
| // `CURRENT_TIME` static. | ||
| unsafe { (*all).fake_timers.reset_for_isolation(global) }; | ||
| } | ||
| if !all.is_null() { | ||
| // SAFETY: as above; `disable` borrows only `event_loop_delay` and | ||
| // reaches the heap through `timer_all()` (disjoint-field access). | ||
| // SAFETY: `state` is non-null so `timer_all()` is live; single JS | ||
| // thread; `disable` borrows only `event_loop_delay` and reaches the | ||
| // heap through `timer_all()` (disjoint-field access). | ||
| unsafe { (*all).event_loop_delay.disable() }; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,9 @@ impl CurrentTime { | |
| /// and `performance.timeOrigin` go back to real as well. | ||
| #[unsafe(no_mangle)] | ||
| extern "C" fn Bun__FakeTimers__setSystemTime(global: &JSGlobalObject, ms: f64) { | ||
| if called_from_retired_realm(global) { | ||
| return; | ||
| } | ||
| let Some(current) = CURRENT_TIME.get_timespec_now() else { | ||
| return; | ||
| }; | ||
|
|
@@ -187,10 +190,10 @@ impl FakeTimers { | |
| } | ||
|
|
||
| /// Restore real timers without draining the fake heap. Used by the | ||
| /// `--isolate` file boundary so `swap_global_for_test_isolation`'s | ||
| /// `cancel_all_timeout_objects` (which runs after the outgoing global's | ||
| /// JS has stopped) can walk the still-populated fake heap and release | ||
| /// `TimeoutObject` pins and discard `AbortSignalTimeout` timers. | ||
| /// `cancel_all_timers` hook (the `--isolate` swap, VM teardown) right | ||
| /// before `cancel_all_timeout_objects`, which walks the still-populated | ||
| /// fake heap itself to release `TimeoutObject` pins and discard | ||
| /// `AbortSignalTimeout` timers once the outgoing global's JS has stopped. | ||
|
Comment on lines
+193
to
+196
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| pub(crate) fn reset_for_isolation(&mut self, global: &JSGlobalObject) { | ||
| CURRENT_TIME.clear(global); | ||
| self.active = false; | ||
|
|
@@ -316,6 +319,15 @@ impl FakeTimers { | |
| // JS Functions | ||
| // === | ||
|
|
||
| /// Under `--isolate` a file's `jest`/`vi` object outlives the file: JS it | ||
| /// leaked (a threadpool job's `.then`) still runs in the retired realm while a | ||
| /// later file owns the thread. The fake clock is per thread, not per realm, so | ||
| /// from a retired realm every control below is inert: it neither installs, | ||
| /// drives, reads, nor removes the clock of the file that is running now. | ||
|
Comment on lines
+322
to
+326
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| fn called_from_retired_realm(global: &JSGlobalObject) -> bool { | ||
| global.is_context_stopped() | ||
| } | ||
|
|
||
| fn error_unless_fake_timers(global: &JSGlobalObject) -> JsResult<()> { | ||
| // SAFETY: per-thread `timer::All`, live for the VM lifetime. | ||
| if unsafe { (*timer_all()).fake_timers.is_active() } { | ||
|
|
@@ -352,6 +364,9 @@ fn set_fake_timer_marker(global: &JSGlobalObject, enabled: bool) -> JsResult<()> | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn use_fake_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(frame.this()); | ||
| } | ||
| // SAFETY: FFI call into C++ JSMock | ||
| let mut js_now = JSMock__getCurrentUnixTimeMs(); | ||
|
|
||
|
|
@@ -396,6 +411,9 @@ fn use_fake_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSVal | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn use_real_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(frame.this()); | ||
| } | ||
| // SAFETY: per-thread `timer::All`; the borrow ends before `release`. | ||
| let cleared = unsafe { (*timer_all()).fake_timers.deactivate(global) }; | ||
| cleared.release(global.bun_vm_ptr()); | ||
|
|
@@ -408,6 +426,9 @@ fn use_real_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSVal | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn advance_timers_to_next_timer(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(frame.this()); | ||
| } | ||
| error_unless_fake_timers(global)?; | ||
|
|
||
| FakeTimers::execute_next(global)?; | ||
|
|
@@ -417,6 +438,9 @@ fn advance_timers_to_next_timer(global: &JSGlobalObject, frame: &CallFrame) -> J | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn advance_timers_by_time(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(frame.this()); | ||
| } | ||
| error_unless_fake_timers(global)?; | ||
|
|
||
| let arg = frame.arguments_as_array::<1>()[0]; | ||
|
|
@@ -453,6 +477,9 @@ fn advance_timers_by_time(global: &JSGlobalObject, frame: &CallFrame) -> JsResul | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn run_only_pending_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(frame.this()); | ||
| } | ||
| error_unless_fake_timers(global)?; | ||
|
|
||
| FakeTimers::execute_only_pending_timers(global)?; | ||
|
|
@@ -462,6 +489,9 @@ fn run_only_pending_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResu | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn run_all_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(frame.this()); | ||
| } | ||
| error_unless_fake_timers(global)?; | ||
|
|
||
| FakeTimers::execute_all_timers(global)?; | ||
|
|
@@ -471,6 +501,9 @@ fn run_all_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValu | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn get_timer_count(global: &JSGlobalObject, _frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(JSValue::js_number(0.0)); | ||
| } | ||
| error_unless_fake_timers(global)?; | ||
|
|
||
| // SAFETY: per-thread `timer::All`, live for the VM lifetime. | ||
|
|
@@ -481,6 +514,9 @@ fn get_timer_count(global: &JSGlobalObject, _frame: &CallFrame) -> JsResult<JSVa | |
|
|
||
| #[bun_jsc::host_fn] | ||
| fn clear_all_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(frame.this()); | ||
| } | ||
| error_unless_fake_timers(global)?; | ||
|
|
||
| // SAFETY: per-thread `timer::All`; the borrow ends before `release`. | ||
|
|
@@ -491,7 +527,10 @@ fn clear_all_timers(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSVa | |
| } | ||
|
|
||
| #[bun_jsc::host_fn] | ||
| fn is_fake_timers(_global: &JSGlobalObject, _frame: &CallFrame) -> JsResult<JSValue> { | ||
| fn is_fake_timers(global: &JSGlobalObject, _frame: &CallFrame) -> JsResult<JSValue> { | ||
| if called_from_retired_realm(global) { | ||
| return Ok(JSValue::FALSE); | ||
| } | ||
| // SAFETY: per-thread `timer::All`, live for the VM lifetime. | ||
| let is_active = unsafe { (*timer_all()).fake_timers.is_active() }; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code