Skip to content

Commit ff0bd97

Browse files
authored
Use napi's module retain helper to keep Oxide loaded [ci-all] (#20726)
This PR replaces the Windows-only `GetModuleHandleExW` FFI from #20471 with the helper that napi-rs now ships for the same purpose: `napi::bindgen_prelude::retain_current_module_for_unload_safety()` (added in napi-rs/napi-rs#3423, released in 3.12.1). The `#[napi(module_exports)]` hook now calls the helper once. That keeps the Oxide addon mapped after a Node worker that loaded it exits, so Rayon threads that outlive the worker don't crash with `0xC0000005` (#20470). Changes: - Bump `napi` from 3.11.0 to 3.16.2 and `napi-derive` from 3.6.0 to 3.7.1. - Remove our own `unsafe` FFI block. Behavior changes compared to #20471: - The helper is best effort. If pinning fails, `require` no longer throws. The addon then behaves as it did before the pin existed. - The helper also pins the module on Linux and macOS (`dladdr` + `dlopen(RTLD_NOLOAD)`), not only on Windows. - The helper does not exist on wasm, so the hook is `#[cfg(not(target_family = "wasm"))]`. The wasm build had no pin before either. This is an internal change with no new effect for users, so it has no changelog fragment. ## Test plan - `pnpm run build` (includes the wasm build) - `cargo test -p tailwind-oxide` - `env -u NODE_ENV pnpm vitest run --root=integrations oxide/workers` - `[ci-all]` in the title so CI runs `integrations/oxide/workers.test.ts` on Windows and macOS too.
1 parent 0591058 commit ff0bd97

3 files changed

Lines changed: 37 additions & 50 deletions

File tree

‎Cargo.lock‎

Lines changed: 31 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/node/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ crate-type = ["cdylib"]
88

99
[dependencies]
1010
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
11-
napi = { version = "3.11.0", default-features = false, features = ["napi4"] }
12-
napi-derive = "3.6.0"
11+
napi = { version = "3.16.2", default-features = false, features = ["napi4"] }
12+
napi-derive = "3.7.1"
1313
tailwindcss-oxide = { path = "../oxide" }
1414
rayon = "1.12.0"
1515

‎crates/node/src/lib.rs‎

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,11 @@ extern crate napi_derive;
55

66
mod utf16;
77

8-
#[cfg(windows)]
8+
// Rayon threads may outlive a Node worker. Keep their code mapped until process exit.
9+
#[cfg(not(target_family = "wasm"))]
910
#[napi(module_exports)]
10-
fn retain_native_library() -> napi::Result<()> {
11-
use std::ffi::c_void;
12-
13-
#[link(name = "kernel32")]
14-
extern "system" {
15-
fn GetModuleHandleExW(flags: u32, address: *const u16, module: *mut *mut c_void) -> i32;
16-
}
17-
18-
const FROM_ADDRESS: u32 = 0x00000004;
19-
const PIN: u32 = 0x00000001;
20-
let mut module = std::ptr::null_mut();
21-
// Rayon threads may outlive a Node worker. Keep their code mapped until process exit.
22-
// SAFETY: FROM_ADDRESS treats this function's address as a module location, not a string.
23-
// The module is executing this registration hook and `module` is a valid output pointer.
24-
let retained = unsafe {
25-
GetModuleHandleExW(
26-
FROM_ADDRESS | PIN,
27-
retain_native_library as *const () as *const u16,
28-
&mut module,
29-
)
30-
};
31-
if retained == 0 {
32-
return Err(napi::Error::from_reason(format!(
33-
"Failed to retain the Oxide native library: {}",
34-
std::io::Error::last_os_error()
35-
)));
36-
}
37-
Ok(())
11+
fn retain_native_library() {
12+
napi::bindgen_prelude::retain_current_module_for_unload_safety();
3813
}
3914

4015
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)