Skip to content

fix: backport Node.js cleanup hook fixes for node::ObjectWrap addons - #53390

Merged
MarshallOfSound merged 7 commits into
mainfrom
fix/node-addon-53387-main
Sep 3, 2026
Merged

MarshallOfSound merged 7 commits into
mainfrom
fix/node-addon-53387-main

Conversation

@claude

@claude claude Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Requested by Deepak Mohan · Slack thread

Before: Since the Node.js 24.19.0 upgrade (Electron 42.11.0 / 43.5.0 / 44.1.0, and main / 45-x-y on Node.js 24.20.0), any native addon deriving from node::ObjectWrap that is rebuilt against the new headers aborts the process with Assertion failed: (env) != nullptr in node::RemoveEnvironmentCleanupHook as soon as one of its wrapped objects is garbage-collected while no context is entered (e.g. idle-time GC). Node.js 24.19.0 backported the change that makes ObjectWrap register a cleanup hook without the follow-up fix that makes removing that hook safe during GC.

After: RemoveEnvironmentCleanupHook() no longer requires a current context (hooks are looked up in a global registry), and CleanupHookThunkRun() no longer reads the thunk after the hook may have freed it, so node::ObjectWrap addons survive garbage collection and environment teardown.

Description of Change

Cherry-picks two upstream Node.js changes into patches/node:

Also adds spec/fixtures/native-addon/object-wrap (a minimal node::ObjectWrap subclass plus a helper that runs a full GC with the context exited, mirroring the upstream worker-addon test) and a node feature / native addons spec that loads it in an ELECTRON_RUN_AS_NODE child process and asserts the process exits cleanly.

Validation

A full Electron build was not possible in the environment this was prepared in, so the patched runtime itself has not been exercised. What was verified:

  • Patches apply cleanly: script/git-import-patches of the resulting patches/node onto nodejs/node v24.20.0 (72 patches), then re-exported with script/git-export-patches with no churn in any other patch.
  • The new spec was run through script/spec-runner.js --electronVersion=<v> --skipYarnInstall --files spec/node-spec.ts -g 'native addons' on Linux x64 against prebuilt releases, with the fixture addon built by node-gyp against that release's headers:
  • The upstream cctest change is dead code for Electron (cctest is not built) and is carried only to keep the cherry-pick of src: fix use-after-free in CleanupHookThunkRun nodejs/node#65630 complete.

Fixes #53387.

Checklist

Release Notes

Notes: Fixed native addons deriving from node::ObjectWrap aborting during garbage collection on Node.js 24.19.0 and later.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U


Generated by Claude Code

Node.js 24.19.0 made node::ObjectWrap register an environment cleanup
hook but did not carry the fix that makes RemoveEnvironmentCleanupHook()
safe to call from ~ObjectWrap() during garbage collection, so any addon
built against the 24.19.0+ headers aborts with
"Assertion failed: (env) != nullptr" once one of its wrapped objects is
collected while no context is entered.

Cherry-picks:
* nodejs/node#63985 "src: keep global list of addon-provided cleanup
  hooks" (68321eff80918e324e9fdaf1aa8cee6db14b84f7 on nodejs/node main).
* nodejs/node#65630 "src: fix use-after-free in CleanupHookThunkRun" at
  dd2fa31b1d99e5a6710e08ce4139da7c2e344598 and its regression cctest at
  fd325a7fc2b33839c024220aa0c44b9104749686 (not yet landed upstream).

Fixes: #53387

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U
Adds a minimal node::ObjectWrap addon fixture with a helper that runs a
full garbage collection while no context is entered, and a spec that
loads it in an ELECTRON_RUN_AS_NODE child process and asserts the
process exits cleanly. Reproduces
#53387 on unfixed builds.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U
@claude claude Bot added no-backport semver/patch backwards-compatible bug fixes labels Sep 2, 2026
@trop

trop Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

claude[bot] has manually backported this PR to "45-x-y", please check out #53391

@trop

trop Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

claude[bot] has manually backported this PR to "44-x-y", please check out #53392

@trop trop Bot added the in-flight/44-x-y label Sep 2, 2026
@claude claude Bot added target/42-x-y PR should also be added to the "42-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. and removed no-backport labels Sep 2, 2026
@trop

trop Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

claude[bot] has manually backported this PR to "43-x-y", please check out #53393

@trop trop Bot added in-flight/43-x-y and removed target/43-x-y PR should also be added to the "43-x-y" branch. labels Sep 2, 2026
@trop

trop Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

claude[bot] has manually backported this PR to "42-x-y", please check out #53394

@trop trop Bot added in-flight/42-x-y and removed target/42-x-y PR should also be added to the "42-x-y" branch. labels Sep 2, 2026
The native addon fixtures under spec/fixtures/native-addon are yarn
workspaces, so yarn only runs their build scripts once per install state.
In CI the root `yarn install` builds them first, against the system
Node.js headers, and the later spec install with npm_config_nodedir set
never rebuilds them. N-API fixtures load anyway, but the object-wrap
fixture uses Node's C++ API, embeds the system NODE_MODULE_VERSION, and
is rejected by Electron with ERR_DLOPEN_FAILED.

Rebuild every native addon fixture with node-gyp after the spec install
so they are compiled against the configured Electron headers, which is
what spec-runner already intended.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U
The rebuild of the native addon fixtures against Electron's node headers
broke every Linux test shard: the CI test image ships GCC 11, which
cannot parse the V8 headers (`class V8_DEPRECATE_SOON(...) V8_EXPORT
Value` mixes a C++11 attribute with a GNU attribute, rejected by GCC
<= 12, see #53284):

  v8config.h:865:20: error: expected identifier before '__attribute__'
  v8-primitive.h:658:4: error: expected '}' before 'public'

Only addons registering through Node's C++ module API (NODE_MODULE /
NODE_MODULE_INIT) embed NODE_MODULE_VERSION and need Electron's headers,
so limit the rebuild to those; the N-API fixtures keep loading fine as
built by the root install. On Linux, when the Chromium toolchain from
the source checkout is present (CI restores third_party/llvm-build and
libc++ into the test job via src_artifacts, and the nan tests already
build with it), compile the fixture with clang and Electron's libc++
exactly as script/nan-spec-runner.js does; otherwise, and for the
--electronVersion prebuilt path, keep using the system compiler.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U
…indows

The Windows test shards failed the fixture rebuild before configure:
node-gyp inherits NPM_CONFIG_MSVS_VERSION=2022 from the test step's env,
but the runners only ship a newer Visual Studio ("msvs_version does not
match this version"). The root install that built the same fixtures ran
without that pin and picked the installed toolset, so strip it from the
rebuild environment and let node-gyp detect Visual Studio. The Electron
import library it links against is already staged by
script/actions/move-artifacts.sh as gen/node_headers/Release/node.lib.

Also replace the source scan for NODE_MODULE usage with an explicit
"electron:requiresElectronHeaders" flag in the fixture's package.json, so
only fixtures that declare they need Electron's headers (currently
object-wrap) are rebuilt and the N-API fixtures are left untouched.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U
…ture

The rebuild of spec/fixtures/native-addon/object-wrap against Electron's
headers broke two more sets of Linux test shards:

* linux-arm64: the test job runs on an arm64 host but restores the x64
  Chromium toolchain of the cross-compiling build job, so node-gyp died
  with "Exec format error: .../llvm-build/Release+Asserts/bin/clang".
  The image's GCC is the one that rejects the V8 headers
  (#53284), so nothing on that
  host can compile the fixture. Check that the Chromium clang actually
  runs before selecting it; when it does not, warn loudly, skip the
  rebuild and export ELECTRON_SKIP_ELECTRON_HEADER_ADDON_SPECS so the
  ObjectWrap spec is reported as skipped there (with a comment pointing
  at this gap) instead of failing with an ABI error. Every other platform
  keeps running the spec for real.

* linux-x64-asan / -ubsan: the link failed with "ld.lld: error: unable to
  find library -lc++abi". Sanitizer builds compile libc++abi into the
  electron executable and export it from there
  (export_libcxxabi_from_executables) rather than producing libc++abi.a,
  so only pass -lc++abi when that archive exists; the symbols resolve
  from the executable when the addon is loaded.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U
…kipped

The linux-arm test job runs the specs with --skipYarnInstall, so the spec
install that rebuilds spec/fixtures/native-addon/object-wrap against
Electron's headers never happens there and the ObjectWrap spec failed to
load the fixture the root install built against the system Node.js
headers ("was compiled against a different Node.js version using
NODE_MODULE_VERSION 127. This version of Node.js requires
NODE_MODULE_VERSION 146"). When the install is needed but skipped, warn
and export ELECTRON_SKIP_ELECTRON_HEADER_ADDON_SPECS so those specs are
reported as skipped, as on linux-arm64 where no toolchain can build the
fixture.

Also format the previous change with oxfmt, which `yarn lint:fmt`
flagged in CI.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NawBg2exXYXTXt12SBGe9U
@MarshallOfSound
MarshallOfSound marked this pull request as ready for review September 2, 2026 21:35
@MarshallOfSound
MarshallOfSound requested a review from a team as a code owner September 2, 2026 21:35
@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) September 2, 2026 21:44
@MarshallOfSound
MarshallOfSound merged commit 4b4fabe into main Sep 3, 2026
84 checks passed
@MarshallOfSound
MarshallOfSound deleted the fix/node-addon-53387-main branch September 3, 2026 03:09
@release-clerk

release-clerk Bot commented Sep 3, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed native addons deriving from node::ObjectWrap aborting during garbage collection on Node.js 24.19.0 and later.

@github-actions github-actions Bot added the 46-x-y label Sep 3, 2026
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. and removed in-flight/44-x-y in-flight/43-x-y in-flight/42-x-y in-flight/45-x-y labels Sep 3, 2026
@ckerr ckerr mentioned this pull request Sep 16, 2026
2 of 4 tasks
MarshallOfSound pushed a commit that referenced this pull request Sep 17, 2026
Linux ARM64 addon rebuilds use host clang with target libc++ libraries.
Pass the target triple and sysroot to both compiler and linker.
Related: #53390

Assisted-By: GitHub Copilot
MarshallOfSound pushed a commit that referenced this pull request Sep 17, 2026
Linux ARM64 addon rebuilds use host clang with target libc++ libraries.
Pass the target triple and sysroot to both compiler and linker.
Related: #53390

Assisted-By: GitHub Copilot

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <[email protected]>
MarshallOfSound pushed a commit that referenced this pull request Sep 17, 2026
Linux ARM64 addon rebuilds use host clang with target libc++ libraries.
Pass the target triple and sysroot to both compiler and linker.
Related: #53390

Assisted-By: GitHub Copilot

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <[email protected]>
MarshallOfSound pushed a commit that referenced this pull request Sep 17, 2026
Linux ARM64 addon rebuilds use host clang with target libc++ libraries.
Pass the target triple and sysroot to both compiler and linker.
Related: #53390

Assisted-By: GitHub Copilot

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <[email protected]>
MarshallOfSound pushed a commit that referenced this pull request Sep 17, 2026
Linux ARM64 addon rebuilds use host clang with target libc++ libraries.
Pass the target triple and sysroot to both compiler and linker.
Related: #53390

Assisted-By: GitHub Copilot

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Bug] Native addons deriving from node::ObjectWrap abort during GC on Node 24.19.0 / 24.20.0

3 participants