Repository navigation
timers: share the timer state instead of borrowing it exclusively - #48854
Conversation
A timer runs JavaScript that sets and clears timers, so `timer::All` and the nodes in its heaps are reached again while a caller still uses them. They were handed out as `&'static mut` or as raw pointers to dereference per field. `&mut All` was live across callbacks in `drain_timers`, `get_timeout` and `set_timeout` (`valueOf`), and `wtf_arm` formed one on other threads. - `EventLoopTimer`, the heap links and the scalars of `All` and `FakeTimers` are `Cell`s. `All` takes `&self` and `timer_all()` returns `&All`. - Owners hold a plain `EventLoopTimer`, not a `JsCell` of one. - Collections are borrowed in `unsafe` blocks in which no JavaScript runs. - `All::remove` takes a reference. So does `VirtualMachine::timer_remove`, which is safe: the links cannot be written through a shared reference, and the test for an armed timer moved into it from its callers. - `RuntimeState` and `EventLoopTimer` are `repr(C)`, which keeps `timer` and `next` at offset 0. A cell has no niche, so the fields would be reordered. - `RUNTIME_STATE` is `#[thread_local]`, like the VM pointer. - `FakeTimers::clear` releases each timer as it pops it. Deleted: `timer_all_mut` and the per-module accessors (`timer_all`, `timer_heap`, `vm_timer`), the SQL timer hooks, which repeated the ones on `VirtualMachine`, `ClearedTimers`, `Deactivated`, `EventLoopDelayMonitor.rs`, `Timeout::cancel`, three resets of links the heap had already cleared, and `WTFTimer.repeat`, never read.
|
No actionable comments were generated in the recent review. WalkthroughThe timer system now uses cell-backed timer and heap fields with shared-reference operations. Runtime timer accessors and hooks use shared references. Timer owners across JavaScript, networking, DNS, test-runner, and SQL code use the updated APIs. ChangesTimer state and heap
Suggested reviewers: Merge Risk:
|
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline note, I also checked: the cross-thread wtf_arm/wtf_disarm path (node state/next/links are only touched under the wtf_timers guard, and ensure_uv_timer is skipped off the owning thread); the callers that lost their own state == ACTIVE check before timer_remove (GC controller arm/deinit, AbortSignal, cron, etc. — the check moved into VirtualMachine::timer_remove, so behavior is unchanged); and FakeTimers::clear releasing owners as it pops rather than collecting them. No build or test run was done in this review; the PR's test claims were not independently verified.
Extended reasoning...
The change is a ~3000-line refactor of the unsafe Rust timer subsystem across 40 files, converting exclusive borrows and raw pointer field access to shared &self with Cell/UnsafeCell interior mutability, changing RuntimeState to thread-local and repr(C), and deleting several accessors and files. It touches no injection, auth or data-exposure surface, but it is memory-safety-critical code reached from every timer, GC pacing tick and the fake-timer test runner. The size, the number of soundness arguments that now rest on SAFETY comments rather than the type system, and the Windows-only cfg-gated paths that cannot be checked by reading alone are why a human maintainer should still review it.
|
Generated by Claude Code |
What does this PR do?
A timer runs JavaScript that sets and clears timers, so
timer::Alland the nodes in its heaps are reached again while a caller still uses them. They were handed out as&'static mut, or as raw pointers to dereference field by field.&mut Allwas live across callbacks indrain_timers,get_timeoutandset_timeout(valueOf), andwtf_armformed one on other threads.What changes while shared is now a
Cell,Alltakes&self, andtimer_all()returns&All. Removes 114unsafeblocks and 8unsafe fn.EventLoopTimer, not aJsCellof one.All::removeandVirtualMachine::timer_removetake a reference. The test for an armed timer moved intotimer_removefrom its callers, so it is safe.All::removestill expects a linked timer.All::insertandAll::updatetake a raw pointer as before, because the heap keeps it.unsafe: linking a node into the heap, reading a node out of it, and the collections, which are borrowed in blocks in which no JavaScript runs.timer_all_mut, the per-module accessors (timer_all,timer_heap,vm_timer), the SQL timer hooks (they repeated the ones onVirtualMachine),ClearedTimers,Deactivated,EventLoopDelayMonitor.rs,Timeout::cancel, three resets of links the heap had already cleared, andWTFTimer.repeat(never read).Where the compiled code changes
RuntimeStateandEventLoopTimerarerepr(C)to keeptimerandnextat offset 0. No type grew.RUNTIME_STATEis declared with#[thread_local], like the VM pointer, instead ofthread_local!. A tick now names it three times instead of once, and on Android the macro's reads are not merged. A read compiles to the same code on Linux, macOS, Windows and FreeBSD.FakeTimers::clearreleases each timer as it pops it and no longer collects them.bun_runtime−274,bun_event_loop−7,bun_jsc+4,bun_sql_jsc+86 (reset_connection_timeoutis inlined into its 7 callers, 32 fewer calls). 216 crates are byte-identical.auto_tick−4,auto_tick_active−4,get_timeout−2,insert−1,set_timeout−13.bun_runtimeagainst 29 shorter, by 1 to 7 instructions (stop_advancing+7,Histogram::set_enabled+6,use_fake_timers_with_default+6), from fields read again after a call and register allocation. No new check, copy or allocation.How did you verify your code works?
Debug build, 28 test files for timers, fake timers,
AbortSignal.timeout,watchFile, cron, TLS and libuv timers: 24 in full (721 pass), the 4 slow ones by test name (cron's--hottest and the rest ofnode-tls-connectare left to CI). The 4 tests that fail also fail onmain.Generated by Claude Code