SIGN IN SIGN UP
denoland / deno UNCLAIMED

A modern runtime for JavaScript and TypeScript.

0 0 2 Rust

refactor(core): use uv_check_t handle for setImmediate (#32879)

## Summary

Replaces the manual Rust-side gating logic for `setImmediate` with a
proper libuv `uv_check_t` + `uv_idle_t` handle pair, matching how
Node.js implements immediates.

### What changed

- **`uv_compat.rs`**: New `ImmediateCheckHandle` struct manages two
libuv handles:
- **Check handle** (`uv_check_t`): always started, always unref'd — runs
every iteration but never keeps the event loop alive
- **Idle handle** (`uv_idle_t`): started/stopped by JS to control event
loop liveness — only active when refed immediates exist
- **1 new op**: `op_immediate_check(bool)` — JS toggles the idle handle
when the refed immediate count transitions to/from zero
- **`01_core.js`**: `immediateRefCount` and `clearImmediate` call
`op_immediate_check` to start/stop the idle handle
- **`jsruntime.rs`**: Immediates now fire in Phase 5 (check phase, after
I/O), matching Node.js semantics. The manual `has_refed_immediates`
field in `EventLoopPendingState` is removed — a refed active idle handle
is already counted by `has_uv_alive_handles()`
- **`jsrealm.rs`**: `ImmediateCheckHandle` cleanup runs in
`JsRealmInner::destroy` before the uv loop is dropped
- **uv loop auto-creation**: Every `JsRuntime` now automatically creates
and registers a uv loop in `new_inner`, removing manual setup from
`ext/node/lib.rs`, `worker.rs`, and `web_worker.rs`
- **`AsyncId`** moved from `ext/node/ops/handle_wrap.rs` to
`libs/core/uv_compat.rs` (re-exported for backwards compat)

### Why this is better

- Leverages libuv's native handle ref/unref semantics for event loop
liveness instead of tracking `has_refed_immediates` manually in
`EventLoopPendingState`
- Immediates fire in the check phase (after I/O), matching Node.js's
`uv_check_t` architecture
- Removes the fragile `has_refed_immediates` field that duplicated what
libuv already tracks
- Proper memory management: owning type with explicit cleanup, no leaks
- Re-wake condition uses `IMM_IDX_REF_COUNT` (not `IMM_IDX_COUNT`) to
avoid busy-loops from unrefed immediates

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
B
Bartek Iwańczuk committed
92d7ec6caec4fc0d8b63bc820411c048594eade4
Parent: c09b7f4
Committed by GitHub <noreply@github.com> on 3/24/2026, 11:13:58 AM