Repository navigation
Expand file tree
/
Copy pathevent_loop.rs
More file actions
1512 lines (1377 loc) · 63.5 KB
/
Copy pathevent_loop.rs
File metadata and controls
1512 lines (1377 loc) · 63.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! `jsc.EventLoop` — the JS-thread event loop. Port of `src/jsc/event_loop.zig`.
//!
//! `tick`/`enter`/`exit`/`drain_microtasks`/`run_callback`/concurrent-queue
//! plumbing are real. The two hot dispatch loops (`tickQueueWithCount`'s
//! per-`Task` switch and `ImmediateObject::runImmediateTask`) name
//! `bun_runtime` types and are hoisted to that tier via link-time
//! `extern "Rust"` (`__bun_tick_queue_with_count` / `__bun_run_immediate_task`);
//! `auto_tick`/`auto_tick_active` likewise
//! dispatch through `virtual_machine::RuntimeHooks` (need `Timer::All` for the
//! poll deadline). See PORTING.md §Dispatch.
use core::ptr::NonNull;
use core::sync::atomic::{AtomicI32, AtomicPtr, Ordering};
use bun_io::{self as Async, Waker};
use bun_uws as uws;
use crate::js_promise::Status as PromiseStatus;
use crate::virtual_machine::VirtualMachine;
use crate::{self as jsc, CallFrame, JSGlobalObject, JSValue, JsResult};
// ──────────────────────────────────────────────────────────────────────────
// Re-exports (thin re-exports of sibling/neighbor modules — do NOT inline
// bodies). Kept so downstream `bun_jsc::event_loop::Foo` paths match the
// Zig namespace shape (`jsc.EventLoop.Foo` re-exports at file tail).
// ──────────────────────────────────────────────────────────────────────────
pub use bun_event_loop::AnyTask;
pub use bun_event_loop::AnyTaskWithExtraContext;
pub use bun_event_loop::ConcurrentTask::{
self, ConcurrentTask as ConcurrentTaskItem, Queue as ConcurrentQueue,
};
pub use bun_event_loop::DeferredTaskQueue::{self, DeferredRepeatingTask};
pub use bun_event_loop::ManagedTask;
pub use bun_event_loop::MiniEventLoop::{self, AbstractVM, EventLoopKind, MiniVM};
pub use bun_event_loop::Task;
pub use bun_event_loop::any_event_loop::{
AnyEventLoop, EventLoopHandle, EventLoopTask, EventLoopTaskPtr,
};
pub use bun_threading::work_pool::{Task as WorkPoolTask, WorkPool};
pub use crate::concurrent_promise_task::ConcurrentPromiseTask;
pub use crate::cpp_task::{ConcurrentCppTask, CppTask};
pub use crate::garbage_collection_controller::GarbageCollectionController;
pub use crate::jsc_scheduler as JSCScheduler;
pub use crate::posix_signal_handle::{PosixSignalHandle, PosixSignalTask};
pub use crate::work_task::{WorkTask, WorkTaskContext};
bun_core::declare_scope!(EventLoop, hidden);
// TODO(port): bun.LinearFifo(Task, .Dynamic) — std.fifo.LinearFifo
pub type Queue =
bun_collections::LinearFifo<Task, bun_collections::linear_fifo::DynamicBuffer<Task>>;
pub struct EventLoop {
pub tasks: Queue,
/// setImmediate() gets it's own two task queues
/// When you call `setImmediate` in JS, it queues to the start of the next tick
/// This is confusing, but that is how it works in Node.js.
///
/// So we have two queues:
/// - next_immediate_tasks: tasks that will run on the next tick
/// - immediate_tasks: tasks that will run on the current tick
///
/// Having two queues avoids infinite loops creating by calling `setImmediate` in a `setImmediate` callback.
///
/// PORT NOTE (§Dispatch): payload is `*mut ()` — the real
/// `bun_runtime::timer::ImmediateObject` lives in the higher-tier crate
/// (cycle). Low tier stores the erased pointer; the high-tier hook
/// (link-time `__bun_run_immediate_task`) casts it back.
pub immediate_tasks: Vec<*mut ()>,
pub next_immediate_tasks: Vec<*mut ()>,
pub concurrent_tasks: ConcurrentQueue,
// TODO(port): lifetime — *JSGlobalObject backref owned by VM
pub global: Option<NonNull<JSGlobalObject>>,
// TODO(port): lifetime — *VirtualMachine backref (EventLoop is a field of VirtualMachine)
pub virtual_machine: Option<NonNull<VirtualMachine>>,
pub waker: Option<Waker>,
// TODO(port): lifetime — ?*uws.Timer FFI handle
pub forever_timer: Option<NonNull<uws::Timer>>,
pub deferred_tasks: DeferredTaskQueue::DeferredTaskQueue,
#[cfg(windows)]
// TODO(port): lifetime — ?*uws.Loop FFI handle
pub uws_loop: Option<NonNull<uws::Loop>>,
#[cfg(not(windows))]
pub uws_loop: (),
pub debug: Debug,
pub entered_event_loop_count: isize,
pub concurrent_ref: AtomicI32,
/// `std.atomic.Value(?*Timer.WTFTimer)` — atomic nullable pointer.
///
/// PORT NOTE (§Dispatch): payload is `*mut ()` — the real
/// `bun_runtime::timer::WTFTimer` lives in the higher-tier crate (cycle).
/// Low tier stores the erased pointer; the high-tier hook installed via
/// (link-time `__bun_run_wtf_timer`) casts it back.
pub imminent_gc_timer: AtomicPtr<()>,
#[cfg(unix)]
/// Boxed `PosixSignalHandle` ring buffer, leaked once by
/// `Bun__ensureSignalHandler` and live for the process lifetime. Stored as
/// a [`bun_ptr::BackRef`] so the per-tick `drain()` / signal-context
/// `enqueue()` reads go through the single audited `BackRef::deref`
/// instead of an open-coded `NonNull::as_ref` `unsafe` at each site.
pub signal_handler: Option<bun_ptr::BackRef<PosixSignalHandle>>,
#[cfg(not(unix))]
pub signal_handler: (),
}
impl Default for EventLoop {
fn default() -> Self {
Self {
tasks: Queue::init(),
immediate_tasks: Vec::new(),
next_immediate_tasks: Vec::new(),
concurrent_tasks: ConcurrentQueue::default(),
global: None,
virtual_machine: None,
waker: None,
forever_timer: None,
deferred_tasks: DeferredTaskQueue::DeferredTaskQueue::default(),
#[cfg(windows)]
uws_loop: None,
#[cfg(not(windows))]
uws_loop: (),
debug: Debug::default(),
entered_event_loop_count: 0,
concurrent_ref: AtomicI32::new(0),
imminent_gc_timer: AtomicPtr::new(core::ptr::null_mut()),
#[cfg(unix)]
signal_handler: None,
#[cfg(not(unix))]
signal_handler: (),
}
}
}
#[cfg(debug_assertions)]
#[derive(Default)]
pub struct Debug {
pub is_inside_tick_queue: bool,
pub js_call_count_outside_tick_queue: usize,
pub drain_microtasks_count_outside_tick_queue: usize,
pub _prev_is_inside_tick_queue: bool,
/// RAII: deref-on-drop. `exit()` just `take()`s; if `Debug` is dropped
/// without `exit()` running, the +1 from the last `run_callback` no
/// longer leaks.
pub last_fn_name: bun_core::OwnedString,
pub track_last_fn_name: bool,
}
#[cfg(debug_assertions)]
impl Debug {
pub fn enter(&mut self) {
self._prev_is_inside_tick_queue = self.is_inside_tick_queue;
self.is_inside_tick_queue = true;
self.js_call_count_outside_tick_queue = 0;
self.drain_microtasks_count_outside_tick_queue = 0;
}
pub fn exit(&mut self) {
self.is_inside_tick_queue = self._prev_is_inside_tick_queue;
self._prev_is_inside_tick_queue = false;
self.js_call_count_outside_tick_queue = 0;
self.drain_microtasks_count_outside_tick_queue = 0;
drop(core::mem::take(&mut self.last_fn_name));
}
}
#[cfg(not(debug_assertions))]
#[derive(Default)]
pub struct Debug;
#[cfg(not(debug_assertions))]
impl Debug {
#[inline]
pub fn enter(&mut self) {}
#[inline]
pub fn exit(&mut self) {}
}
/// RAII pairing for [`Debug::enter`] / [`Debug::exit`] — the Rust spelling of
/// Zig's `loop.debug.enter(); defer loop.debug.exit();`. Holds the raw pointer
/// (not `&mut`) so re-entrant JS callbacks that touch the same loop while the
/// guard is live don't alias a long-lived mutable borrow.
#[must_use = "dropping immediately exits the debug scope"]
pub struct DebugEnterGuard {
debug: *mut Debug,
}
impl Drop for DebugEnterGuard {
#[inline]
fn drop(&mut self) {
// SAFETY: `debug` was live at `enter_scope` and is owned by the
// process-lifetime `EventLoop`.
unsafe { (*self.debug).exit() };
}
}
impl Debug {
/// `enter()` now, `exit()` on drop.
///
/// # Safety
/// `debug` must point to a live `Debug` (the `event_loop.debug` field) and
/// remain valid for the guard's lifetime.
#[inline]
pub unsafe fn enter_scope(debug: *mut Debug) -> DebugEnterGuard {
// SAFETY: caller contract — `debug` is live; short-lived `&mut` only.
unsafe { (*debug).enter() };
DebugEnterGuard { debug }
}
}
mod drain_result {
pub const SUCCESS: u8 = 0;
pub const JS_TERMINATED: u8 = 1;
}
// TODO(port): move to jsc_sys
//
// `JSGlobalObject` is an opaque `UnsafeCell`-backed ZST handle; C++ mutating
// the microtask queue through it is interior mutation invisible to Rust.
unsafe extern "C" {
safe fn JSC__JSGlobalObject__drainMicrotasks(global: &JSGlobalObject) -> u8;
}
#[derive(thiserror::Error, strum::IntoStaticStr, Debug)]
pub enum JsTerminated {
#[error("JSTerminated")]
JSTerminated,
}
/// Zig: `bun.JSTerminated!T` — short alias for `Result<T, JsTerminated>`.
pub type JsTerminatedResult<T> = Result<T, JsTerminated>;
impl From<JsTerminated> for bun_core::Error {
fn from(_: JsTerminated) -> Self {
bun_core::err!("JSTerminated")
}
}
// ──────────────────────────────────────────────────────────────────────────
// §Dispatch hot-path — `tick_queue_with_count` is the per-tick dispatch over
// `Task { tag, ptr }`. Per PORTING.md, the *high tier owns the match loop*:
// `bun_runtime` registers the real dispatcher at init; this crate only stores
// `(tag, ptr)` and the hook. The dispatch match lives in
// `bun_runtime::dispatch::run_tasks` (every arm names a `bun_runtime` type).
// ──────────────────────────────────────────────────────────────────────────
// The hook receives the specific `EventLoop` to drain (which may be the
// isolated `SpawnSyncEventLoop`, not `vm.event_loop()`) plus the VM.
unsafe extern "Rust" {
/// `bun_runtime::dispatch::tick_queue_with_count` — the real per-task
/// match loop (Zig `tickQueueWithCount`). Link-time resolved.
fn __bun_tick_queue_with_count(
el: *mut EventLoop,
vm: *mut VirtualMachine,
counter: &mut u32,
) -> Result<(), JsTerminated>;
/// `ImmediateObject::runImmediateTask` — `task` is an erased
/// `*mut bun_runtime::timer::ImmediateObject`; returns whether the callback
/// threw. Defined in `bun_runtime::dispatch`. Link-time resolved.
fn __bun_run_immediate_task(task: *mut (), vm: *mut VirtualMachine) -> bool;
/// Release the event loop's `+1` ref on a still-queued `ImmediateObject`
/// without running it. Defined in `bun_runtime::dispatch`.
fn __bun_cancel_pending_immediate(task: *mut (), vm: *mut VirtualMachine);
/// `WTFTimer::run` — `timer` is an erased `*mut bun_runtime::timer::WTFTimer`.
/// Defined in `bun_runtime::dispatch`. Link-time resolved.
fn __bun_run_wtf_timer(timer: *mut (), vm: *mut VirtualMachine);
/// Tag-specific shutdown release for a queued-but-never-run task. Called
/// from `release_queued_tasks_for_shutdown` (after `shutdown_for_exit`,
/// before `destructOnExit`) for every entry left in `self.tasks`.
/// Returns `true` iff the tag was consumed; `false` means the entry
/// must be left in the queue (it stays reachable from the static-rooted
/// VM box, which is the pre-`532a5411961b` behaviour for tags that don't
/// own JSC handles or whose callback isn't safe to no-op-dispatch).
/// Defined in `bun_runtime::dispatch`. Link-time resolved.
fn __bun_release_task_at_shutdown(task: bun_event_loop::Task) -> bool;
}
#[inline]
fn tick_queue_with_count(
el: &mut EventLoop,
vm: *mut VirtualMachine,
counter: &mut u32,
) -> Result<(), JsTerminated> {
// SAFETY: `el` is the queue to drain (may be the isolated spawnSync loop);
// `vm` is the live per-thread VM (caller contract).
unsafe { __bun_tick_queue_with_count(el, vm, counter) }
}
/// RAII pairing for [`EventLoop::enter`] / [`EventLoop::exit`].
///
/// Holds the raw `*mut EventLoop` (not `&mut`) so re-entrant JS callbacks that
/// touch the same loop while the guard is live don't alias a long-lived mutable
/// borrow — the `&mut` is formed only at the enter/exit call sites. Construct
/// via [`EventLoop::enter_scope`].
#[must_use = "dropping immediately exits the event loop scope"]
pub struct EventLoopEnterGuard {
loop_: *mut EventLoop,
}
impl Drop for EventLoopEnterGuard {
#[inline]
fn drop(&mut self) {
// SAFETY: `loop_` was live at `enter_scope` and the VM owns it for the
// process lifetime; forming a short-lived `&mut` here mirrors the
// manual `(*loop_).exit()` callers previously wrote.
unsafe { (*self.loop_).exit() };
}
}
impl EventLoop {
/// Before your code enters JavaScript at the top of the event loop, call
/// `loop.enter()`. If running a single callback, prefer `runCallback` instead.
///
/// When we call into JavaScript, we must drain process.nextTick & microtasks
/// afterwards (so that promises run). We must only do that once per task in the
/// event loop. To make that work, we count enter/exit calls and once that
/// counter reaches 0, we drain the microtasks.
#[inline]
pub fn enter(&mut self) {
bun_core::scoped_log!(EventLoop, "enter() = {}", self.entered_event_loop_count);
self.entered_event_loop_count += 1;
self.debug.enter();
}
/// "exit" a microtask context in the event loop. See `enter`.
pub fn exit(&mut self) {
let count = self.entered_event_loop_count;
bun_core::scoped_log!(EventLoop, "exit() = {}", count - 1);
if count == 1 && !self.vm_ref().is_inside_deferred_task_queue.get() {
let _ = self.drain_microtasks();
}
self.entered_event_loop_count -= 1;
self.debug.exit();
// PORT NOTE: reshaped for borrowck — Zig `defer this.debug.exit()` moved to tail; no early returns
}
/// `enter()` now, `exit()` on drop. Takes the raw VM-owned pointer so the
/// guard doesn't hold a long-lived `&mut EventLoop` across re-entrant JS.
///
/// # Safety
/// `loop_` must be the live `vm.event_loop()` pointer and remain valid for
/// the guard's lifetime (the VM owns it for the process lifetime).
#[inline]
pub unsafe fn enter_scope(loop_: *mut EventLoop) -> EventLoopEnterGuard {
// SAFETY: caller contract — `loop_` is live; short-lived `&mut` only.
unsafe { (*loop_).enter() };
EventLoopEnterGuard { loop_ }
}
pub fn exit_maybe_drain_microtasks(
&mut self,
allow_drain_microtask: bool,
) -> Result<(), JsTerminated> {
let count = self.entered_event_loop_count;
bun_core::scoped_log!(EventLoop, "exit() = {}", count - 1);
let inside_deferred = self.vm_ref().is_inside_deferred_task_queue.get();
let result = if allow_drain_microtask && count == 1 && !inside_deferred {
self.drain_microtasks()
} else {
Ok(())
};
// PORT NOTE: spec event_loop.zig:92-103 uses `try drainMicrotasksWithGlobal(...)`
// which returns BEFORE reaching `entered_event_loop_count -= 1`; only
// `defer this.debug.exit()` runs on the error path. Mirror that here.
if result.is_ok() {
self.entered_event_loop_count -= 1;
}
self.debug.exit();
result
}
#[inline]
pub fn get_vm_impl(&self) -> *mut VirtualMachine {
self.vm()
}
/// SAFETY: returns `&mut` into VM-owned scratch; two calls alias the same
/// buffer. Caller must not hold another live `&mut` to it.
pub unsafe fn pipe_read_buffer(&mut self) -> &mut [u8] {
// SAFETY: vm() is the live owning VM; rare_data() lazily inits the
// per-VM scratch buffer. Caller contract (see doc): no concurrent &mut.
unsafe { &mut (*self.vm()).rare_data().pipe_read_buffer()[..] }
}
pub fn drain_microtasks_with_global(
&mut self,
global_object: &JSGlobalObject,
jsc_vm: &jsc::VM,
) -> Result<(), JsTerminated> {
// Hoist the VM backref once. LLVM can't CSE the `Option<NonNull>` field
// load across the FFI calls below (`release_weak_refs`, `drainMicrotasks`,
// `deferred_tasks.run`), so each `self.vm_ref()` re-loaded
// `self.virtual_machine` from memory (5× per call, ~2×/request).
// SAFETY: `virtual_machine` is set in `VirtualMachine::init()` to the
// owning per-thread singleton; non-null and outlives `self`.
let vm = unsafe { self.virtual_machine.unwrap_unchecked().as_ref() };
// During spawnSync, the isolated event loop shares the same VM/GlobalObject.
// Draining microtasks would execute user JavaScript, which must not happen.
if vm.suppress_microtask_drain.get() {
return Ok(());
}
jsc::mark_binding();
jsc_vm.release_weak_refs();
match JSC__JSGlobalObject__drainMicrotasks(global_object) {
drain_result::SUCCESS => {}
drain_result::JS_TERMINATED => return Err(JsTerminated::JSTerminated),
_ => unreachable!(),
}
// `Cell` write through `&VirtualMachine` — no `&mut VM` formed (would
// overlap `&mut self: EventLoop`, which is a value field of the VM).
vm.is_inside_deferred_task_queue.set(true);
self.deferred_tasks.run();
vm.is_inside_deferred_task_queue.set(false);
// PORT NOTE: spec event_loop.zig:144-146 guards on `event_loop_handle != null`
// but then calls `this.virtual_machine.uwsLoop().drainQuicIfNecessary()`.
// On Windows `uwsLoop()` returns `uws.Loop.get()` (NOT `event_loop_handle`,
// which is the libuv loop). Mirror that here.
if vm.event_loop_handle.is_some() {
vm.uws_loop_mut().drain_quic_if_necessary();
}
#[cfg(debug_assertions)]
{
self.debug.drain_microtasks_count_outside_tick_queue +=
(!self.debug.is_inside_tick_queue) as usize;
}
Ok(())
}
#[inline(always)]
pub fn drain_microtasks(&mut self) -> Result<(), JsTerminated> {
// Zig spec (event_loop.zig:153) reads `this.global` directly — match
// it via `global_ref()` instead of round-tripping through
// `virtual_machine` (saves a dependent load on the hot path).
let global = self.global_ref();
let jsc_vm = self.vm_ref().jsc_vm();
self.drain_microtasks_with_global(global, jsc_vm)
}
// should be called after exit()
pub fn maybe_drain_microtasks(&mut self) {
if self.entered_event_loop_count == 0 && !self.vm_ref().is_inside_deferred_task_queue.get()
{
let _ = self.drain_microtasks();
}
}
/// When you call a JavaScript function from outside the event loop task
/// queue, it has to be wrapped in `runCallback` to ensure that microtasks
/// are drained and errors are handled.
pub fn run_callback(
&mut self,
callback: JSValue,
global_object: &JSGlobalObject,
this_value: JSValue,
arguments: &[JSValue],
) {
// R-2 noalias mitigation (see PORT_NOTES_PLAN R-2; precedent
// `b818e70e1c57` NodeHTTPResponse::cork): `&mut self` carries LLVM
// `noalias`, and `callback.call()` receives nothing derived from
// `self`, so LLVM is licensed to forward `self.entered_event_loop_count`
// (written by `enter()`) across the JS call into `exit()`. JS re-enters
// via host-fns that reach this same `EventLoop` through
// `vm.event_loop()` and may run nested `enter()/exit()` pairs (or call
// `drain_microtasks` directly), making the cached count stale. ASM-
// verified PROVEN_CACHED. Launder `self` so the post-call access goes
// through an opaque pointer LLVM can't prove is in the noalias scope.
let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self));
// SAFETY: `this` is the unique live `EventLoop` (a value field of the
// process-lifetime `VirtualMachine`); short-lived `&mut` only.
unsafe { (*this).enter() };
if let Err(err) = callback.call(global_object, this_value, arguments) {
global_object.report_active_exception_as_unhandled(err);
}
// Force a re-escape between the JS call and the post-call `exit()` so
// LLVM cannot forward any `*this` field across `call()`.
let this: *mut Self = core::hint::black_box(this);
// SAFETY: as above.
unsafe { (*this).exit() };
// PORT NOTE: reshaped for borrowck — `defer this.exit()` moved to tail; no early returns
}
pub fn run_callback_with_result(
&mut self,
callback: JSValue,
global_object: &JSGlobalObject,
this_value: JSValue,
arguments: &[JSValue],
) -> JSValue {
// R-2 noalias mitigation — see `run_callback` above.
let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self));
// SAFETY: `this` is the unique live `EventLoop`; short-lived `&mut`.
unsafe { (*this).enter() };
let result = match callback.call(global_object, this_value, arguments) {
Ok(v) => v,
Err(err) => {
global_object.report_active_exception_as_unhandled(err);
JSValue::ZERO
}
};
let this: *mut Self = core::hint::black_box(this);
// SAFETY: as above.
unsafe { (*this).exit() };
// PORT NOTE: reshaped for borrowck — `defer this.exit()` moved to tail
result
}
fn tick_with_count(&mut self, virtual_machine: *mut VirtualMachine) -> u32 {
let mut counter: u32 = 0;
let _ = tick_queue_with_count(self, virtual_machine, &mut counter);
counter
}
fn tick_concurrent(&mut self) {
let _ = self.tick_concurrent_with_count();
}
/// Check whether refConcurrently has been called but the change has not yet been applied to the
/// underlying event loop's `active` counter
pub fn has_pending_refs(&self) -> bool {
self.concurrent_ref.load(Ordering::SeqCst) > 0
}
pub fn run_imminent_gc_timer(&mut self) {
// Spec event_loop.zig:302-306: `if (swap()) |timer| timer.run(vm)`.
// The real `WTFTimer` lives in `bun_runtime` (cycle), so the body
// dispatches through `__bun_run_wtf_timer` (link-time extern).
let ptr = self
.imminent_gc_timer
.swap(core::ptr::null_mut(), Ordering::SeqCst);
if !ptr.is_null() {
// SAFETY: `ptr` was published by `WTFTimer::update` and remains
// valid until `run` removes it; `vm()` is the live owning VM.
unsafe { __bun_run_wtf_timer(ptr, self.vm()) };
}
}
pub fn tick_concurrent_with_count(&mut self) -> usize {
self.update_counts();
#[cfg(unix)]
{
if let Some(signal_handler) = self.signal_handler {
// `signal_handler` is a `BackRef` to the leaked process-lifetime
// `PosixSignalHandle` (see field doc); the ring-buffer backing is
// disjoint from `*self`, so the `&PosixSignalHandle` materialised
// by `BackRef::deref` does not alias the `&mut self` passed here.
signal_handler.drain(self);
}
}
self.run_imminent_gc_timer();
let concurrent = self.concurrent_tasks.pop_batch();
let count = concurrent.count;
if count == 0 {
return 0;
}
let mut iter = concurrent.iterator();
let start_count = self.tasks.readable_length();
// PORT NOTE: Zig resets `head = 0` when empty as a micro-opt; LinearFifo
// realigns internally on grow, so this is folded into `ensure_unused_capacity`.
let _ = self.tasks.ensure_unused_capacity(count);
// Defer destruction of the ConcurrentTask to avoid issues with pointer aliasing
let mut to_destroy: Option<*mut ConcurrentTaskItem> = None;
loop {
let task = iter.next();
if task.is_null() {
break;
}
if let Some(dest) = to_destroy.take() {
// SAFETY: dest was returned by iterator and marked auto_delete; uniquely owned here
let _ = unsafe { bun_core::heap::take(dest) };
}
// SAFETY: `task` is non-null (checked above) and owned by this batch.
let task_ref = unsafe { &mut *task };
if task_ref.auto_delete() {
to_destroy = Some(task);
}
// PERF(port): Zig wrote into `writable_slice(0)` and bumped `count`
// directly; LinearFifo's fields are private — `write_item` is the
// public path (single-slot copy, same complexity).
let _ = self.tasks.write_item(task_ref.task);
}
if let Some(dest) = to_destroy {
// SAFETY: see above
let _ = unsafe { bun_core::heap::take(dest) };
}
self.tasks.readable_length() - start_count
}
fn update_counts(&mut self) {
// PORT NOTE: spec event_loop.zig:283-284 unwraps `event_loop_handle.?`
// (panic). Do NOT silently drop the swapped delta when the handle is
// missing — refs queued via `ref_concurrently()` would be lost forever.
let delta = self.concurrent_ref.swap(0, Ordering::SeqCst);
let loop_ = self
.vm_ref()
.platform_loop_opt()
.expect("event_loop_handle");
#[cfg(windows)]
{
if delta > 0 {
loop_.add_active(u32::try_from(delta).expect("int cast"));
} else {
loop_.sub_active(u32::try_from(-delta).expect("int cast"));
}
}
#[cfg(not(windows))]
{
if delta > 0 {
loop_.num_polls += delta;
loop_.active = loop_
.active
.saturating_add(u32::try_from(delta).expect("int cast"));
} else {
loop_.num_polls -= -delta;
loop_.active = loop_
.active
.saturating_sub(u32::try_from(-delta).expect("int cast"));
}
}
}
/// Walk `self.virtual_machine.event_loop_handle` via raw-pointer
/// projection without materializing a `&VirtualMachine` (the VM may be
/// mutably borrowed elsewhere on the JS thread when libuv completion
/// callbacks reach for the loop). Mirrors Zig
/// `this.event_loop.virtual_machine.event_loop_handle.?`.
#[inline]
pub fn uv_loop(&self) -> *mut crate::PlatformEventLoop {
let vm = self.virtual_machine.expect("virtual_machine").as_ptr();
// SAFETY: `virtual_machine` is set in `VirtualMachine::init()` to the
// owning per-thread singleton; non-null and live for the VM lifetime.
// `addr_of!` projects to the field place without forming an
// intermediate `&VirtualMachine` that would assert no-alias.
unsafe { core::ptr::addr_of!((*vm).event_loop_handle).read() }.expect("event_loop_handle")
}
pub fn usockets_loop(&self) -> *mut uws::Loop {
// Spec event_loop.zig:359-365 unwraps `.?` (panic on null). Preserve
// that fail-fast contract — callers immediately materialize `&mut *`,
// so a null return would be instant UB instead of a clean panic.
#[cfg(windows)]
{
return self
.uws_loop
.expect("usockets_loop: uws_loop not initialized (call ensure_waker first)")
.as_ptr();
}
#[cfg(not(windows))]
{
self.vm_ref().event_loop_handle.expect(
"usockets_loop: event_loop_handle not initialized (call ensure_waker first)",
)
}
}
#[inline]
pub fn process_gc_timer(&mut self) {
self.vm_ref().as_mut().gc_controller.process_gc_timer();
}
pub fn tick(&mut self) {
jsc::mark_binding();
crate::top_scope!(scope, self.global_ref());
self.entered_event_loop_count += 1;
self.debug.enter();
// PORT NOTE: reshaped for borrowck — Zig
// `defer scope.deinit(); defer { entered_event_loop_count -= 1; debug.exit() }`
// is inlined at each return site below (a scopeguard closure would
// alias `&mut self`).
let ctx = self.vm();
self.tick_concurrent();
self.process_gc_timer();
// PORT NOTE: reshaped for borrowck — `vm_ref()` is `&'static`, so the
// global borrow detaches from `&self` and survives the `&mut self` call.
let global = self.vm_ref().global();
let global_vm = self.vm_ref().jsc_vm();
loop {
// Zig: while (tickWithCount > 0) : (handleRejectedPromises) { tickConcurrent } else { ... }
while self.tick_with_count(ctx) > 0 {
self.tick_concurrent();
self.global_ref().handle_rejected_promises();
}
// Zig while-else: else branch runs whenever the condition becomes false.
if self
.drain_microtasks_with_global(global, global_vm)
.is_err()
|| scope.has_exception()
{
self.entered_event_loop_count -= 1;
self.debug.exit();
return;
}
self.tick_concurrent();
if self.tasks.readable_length() > 0 {
continue;
}
break;
}
while self.tick_with_count(ctx) > 0 {
self.tick_concurrent();
}
self.global_ref().handle_rejected_promises();
self.entered_event_loop_count -= 1;
self.debug.exit();
}
/// Tick the task queue without draining microtasks afterward.
pub fn tick_tasks_only(&mut self) {
self.tick_concurrent();
let vm = self.vm();
// `Cell` swap through `&VirtualMachine` — no `&mut VM` formed (would
// overlap `&mut self: EventLoop`, which is a value field of the VM).
let prev = self.vm_ref().suppress_microtask_drain.replace(true);
while self.tick_with_count(vm) > 0 {
self.tick_concurrent();
}
self.vm_ref().suppress_microtask_drain.set(prev);
// PORT NOTE: reshaped for borrowck — `defer vm.suppress_microtask_drain = prev` moved to tail
}
pub fn enqueue_task(&mut self, task: Task) {
let _ = self.tasks.write_item(task);
}
/// Drain `concurrent_tasks` without running them and `delete` any
/// `EventLoopTask*` payloads so their captured `Ref<>`s drop. Called from
/// `global_exit` after `terminate_all_workers_and_wait` (every worker has
/// posted its close task by then) and before `destructOnExit` (so
/// `~Worker` runs during the final GC sweep with the JSC VM still alive).
/// Without this, the last worker's close-task lambda — and the
/// `WebWorker` box reachable through its `protectedThis` — leak.
pub fn drop_concurrent_cpp_tasks(&mut self) {
unsafe extern "C" {
fn Bun__deleteEventLoopTask(task: *mut CppTask);
}
let mut iter = self.concurrent_tasks.pop_batch().iterator();
loop {
let node = iter.next();
if node.is_null() {
break;
}
// SAFETY: `node` is non-null and owned by the popped batch; the
// iterator advanced past it before returning, so reading then
// freeing here is sound.
let (task, auto_delete) = unsafe { ((*node).task, (*node).auto_delete()) };
if task.tag == bun_event_loop::task_tag::CppTask {
// SAFETY: every `CppTask` payload is a heap
// `WebCore::EventLoopTask*` (`ScriptExecutionContext::postTask*`
// → `new EventLoopTask`); we own it once popped.
unsafe { Bun__deleteEventLoopTask(task.ptr.cast::<CppTask>()) };
} else {
// Hand non-Cpp payloads to `self.tasks` so `deinit()`'s
// existing per-tag reclaim handles them.
let _ = self.tasks.write_item(task);
}
if auto_delete {
// SAFETY: heap-owned (see `ConcurrentTask::create`); not yet
// freed, and the iterator no longer references it.
drop(unsafe { bun_core::heap::take(node) });
}
}
}
/// Release queued-but-never-run tasks that own a ref the dispatch path
/// would have dropped. Called from `global_exit` after `shutdown_for_exit`
/// (HTTP daemon parked, no further cross-thread posts) and before
/// `destructOnExit` (JSC still live, so `FetchTasklet::deinit` can drop
/// its `Strong`/`Weak` handles). Re-runs `drop_concurrent_cpp_tasks` first
/// so any task the HTTP thread posted after the earlier drain — its
/// `is_shutting_down()` read is non-atomic and can lag — is forwarded into
/// `self.tasks` for the per-tag release below.
///
/// `ManagedTask` entries are deliberately re-queued rather than freed:
/// owners (e.g. `SendQueue.close_next_tick` / `after_close_task`) keep raw
/// back-pointers that they `cancel()` from `Drop`, and those `Drop`s fire
/// during `destructOnExit` (`Subprocess::finalize` → `SendQueue::drop`).
/// Freeing the box here would leave those pointers dangling and make
/// `cancel()` a heap-use-after-free. `deinit()` runs after `destructOnExit`
/// — every owner has cancelled and cleared its pointer by then — so it is
/// the correct teardown point for `ManagedTask`s.
///
/// Tags `__bun_release_task_at_shutdown` doesn't claim are likewise
/// re-queued so they remain reachable from the static-rooted VM box (the
/// pre-`532a5411961b` state). Consuming them silently here unhooked that
/// root and surfaced the boxes as direct leaks (e.g. `AnyTaskJob<_>`); the
/// definer can't safely dispatch every `AnyTask` callback at shutdown.
pub fn release_queued_tasks_for_shutdown(&mut self) {
self.drop_concurrent_cpp_tasks();
let mut requeue: Vec<bun_event_loop::Task> = Vec::new();
while let Some(task) = self.tasks.read_item() {
// SAFETY: tag-specific release (drops JSC handles while the VM is
// still live); definer in `bun_runtime::dispatch` matches the same
// tag set `tick_queue_with_count` does. `false` ⇒ not handled.
let consumed = task.tag != bun_event_loop::task_tag::ManagedTask
&& unsafe { __bun_release_task_at_shutdown(task) };
if !consumed {
requeue.push(task);
}
}
for task in requeue {
let _ = self.tasks.write_item(task);
}
}
pub fn deinit(&mut self) {
// Free (don't run — running could re-enter the dying VM) queued
// ManagedTask boxes. Other tags are left in place: they were re-queued
// by `release_queued_tasks_for_shutdown` because their callback can't
// be no-op-dispatched safely (`AnyTask` callbacks call into JS) and
// their box may be aliased by the originator. Keeping them in
// `self.tasks` (a field of the static-rooted `VirtualMachine` box that
// is never `dealloc`'d) leaves the chain reachable to LSan — the same
// visibility they had via `concurrent_tasks` before
// `drop_concurrent_cpp_tasks` drained it.
let mut requeue: Vec<bun_event_loop::Task> = Vec::new();
while let Some(task) = self.tasks.read_item() {
if task.tag == bun_event_loop::task_tag::ManagedTask {
// SAFETY: every ManagedTask is heap_owned (ManagedTask::new -> heap::into_raw).
let managed =
unsafe { bun_core::heap::take(task.ptr.cast::<ManagedTask::ManagedTask>()) };
if let (Some(cleanup), Some(ctx)) = (managed.cleanup, managed.ctx) {
cleanup(ctx.as_ptr());
}
drop(managed);
} else {
requeue.push(task);
}
}
// PORT NOTE: Zig's `tasks.deinit()` / `clearAndFree()` map to dropping
// the owned buffers; reassigning a fresh value drops the old in place.
self.tasks = Queue::init();
for task in requeue {
let _ = self.tasks.write_item(task);
}
let pending = core::mem::take(&mut self.immediate_tasks);
let next = core::mem::take(&mut self.next_immediate_tasks);
if !pending.is_empty() || !next.is_empty() {
let vm = self.vm();
for task in pending.into_iter().chain(next) {
// SAFETY: `task` came from `enqueue_immediate_task`; `vm` is the live per-thread VM.
unsafe { __bun_cancel_pending_immediate(task, vm) };
}
}
}
/// PORT NOTE (§Dispatch): `task` is an erased
/// `*mut bun_runtime::timer::ImmediateObject` — see [`RunImmediateFn`].
pub fn enqueue_immediate_task(&mut self, task: *mut ()) {
self.immediate_tasks.push(task);
}
/// `tickImmediateTasks` — spec event_loop.zig:239-270. Swaps the two
/// immediate queues, drains the now-current batch, then recycles the
/// drained Vec as the next-tick buffer.
///
/// PORT NOTE: the real `ImmediateObject` lives in `bun_runtime` (cycle), so
/// the per-task body dispatches through `__bun_run_immediate_task` (link-
/// time, definer in `bun_runtime`). The swap always happens — this is
/// load-bearing for `auto_tick`'s `has_pending_immediate` read, which must
/// observe the post-swap `immediate_tasks` (next-tick immediates), not the
/// un-drained current batch (busy-spin hazard, spec Timer.zig:251-256).
///
/// # Safety
/// `virtual_machine` must be the live per-thread VM that owns this `EventLoop`.
pub unsafe fn tick_immediate_tasks(&mut self, virtual_machine: *mut VirtualMachine) {
// R-2 noalias mitigation (PORT_NOTES_PLAN R-2; precedent
// `b818e70e1c57` NodeHTTPResponse::cork): `&mut self` is `noalias`, and
// the only thing reaching the `__bun_run_immediate_task` extern call is
// `virtual_machine` — a *separate* pointer parameter that LLVM is told
// does NOT alias `*self` (even though `EventLoop` is a value field of
// `*virtual_machine`). JS re-enters via `setImmediate` →
// `enqueue_immediate_task` and pushes onto `self.next_immediate_tasks`.
// Without the launder, LLVM may forward the post-`take` empty
// `next_immediate_tasks` past the loop into the `.capacity() > 0`
// recursion check and the trailing `= to_run_now` store, dropping any
// immediates JS queued during this tick. ASM-verified PROVEN_CACHED.
let this: *mut Self = core::hint::black_box(core::ptr::from_mut(self));
// SAFETY: `this` is the unique live `EventLoop`; each access below is a
// short-lived `&mut` that does not overlap re-entry.
let mut to_run_now = core::mem::take(unsafe { &mut (*this).immediate_tasks });
// SAFETY: as above.
unsafe { (*this).immediate_tasks = core::mem::take(&mut (*this).next_immediate_tasks) };
let mut exception_thrown = false;
for task in to_run_now.iter() {
// SAFETY: ImmediateObject pointers are kept alive by the JS heap
// until `runImmediateTask` consumes them; `virtual_machine` is the
// live owning VM per caller contract.
exception_thrown = unsafe { __bun_run_immediate_task(*task, virtual_machine) };
}
// Re-escape `this` after the re-entrant loop so nothing about `*this`
// is carried across it.
let this: *mut Self = core::hint::black_box(this);
// make sure microtasks are drained if the last task had an exception
if exception_thrown {
// SAFETY: as above.
unsafe { (*this).maybe_drain_microtasks() };
}
// SAFETY: as above; this read MUST observe pushes JS made during the
// loop (the recursion check).
if unsafe { (*this).next_immediate_tasks.capacity() } > 0 {
// this would only occur if we were recursively running tickImmediateTasks.
bun_core::hint::cold();
// SAFETY: as above.
let r = unsafe { &mut *this };
let next = core::mem::take(&mut r.next_immediate_tasks);
r.immediate_tasks.extend_from_slice(&next);
}
if to_run_now.capacity() > 1024 * 128 {
// once in a while, deinit the array to free up memory
to_run_now = Vec::new();
} else {
to_run_now.clear();
}
// SAFETY: as above.
unsafe { (*this).next_immediate_tasks = to_run_now };
}
pub fn ensure_waker(&mut self) {
jsc::mark_binding();
if self.vm_ref().event_loop_handle.is_none() {
#[cfg(windows)]
{
self.uws_loop = NonNull::new(uws::Loop::get());
}
let vm = self.vm();
// SAFETY: `vm` is the live owning VM.
unsafe { (*vm).event_loop_handle = Some(Async::Loop::get()) };
// PORT NOTE: reshaped for borrowck — Zig passes `vm.gc_controller` and
// `vm` simultaneously; route through raw addr_of to avoid stacked-borrow
// aliasing of the embedded field with its parent.
// SAFETY: `vm` is the live owning VM; gc_controller is embedded.
unsafe {
let gc: *mut GarbageCollectionController =
core::ptr::addr_of_mut!((*vm).gc_controller);
(*gc).init(&mut *vm);
}
}
#[cfg(windows)]
{
if self.uws_loop.is_none() {
self.uws_loop = NonNull::new(uws::Loop::get());
}
}
// PORT NOTE: `EventLoopHandle` lives in `bun_event_loop` (lower tier),
// which cannot name `jsc::EventLoop`, so it stores `*mut ()`. The
// typed `set_parent_event_loop` extension trait in `bun_uws` expects
// a `ParentEventLoopHandle` impl, but `EventLoopHandle` already
// exposes `into_tag_ptr()` — go straight to the sys-level setter.
// `self` is the live per-thread `jsc::EventLoop` (mut ref) — non-null.
let self_ptr = core::ptr::from_mut(self).cast::<()>();
let (tag, ptr) = EventLoopHandle::init(self_ptr).into_tag_ptr();
// SAFETY: `uws::Loop::get()` returns the live process-global uws loop.
unsafe {
(*uws::Loop::get())
.internal_loop_data
.set_parent_raw(tag, ptr)
};
}
/// Asynchronously run the garbage collector and track how much memory is now allocated
pub fn perform_gc(&mut self) {
self.vm_ref().as_mut().gc_controller.perform_gc();