GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ffi: keep DynamicLibrary alive while callbacks are registered by trivikr · Pull Request #66842 · nodejs/node

Conversation

@trivikr

@trivikr trivikr commented Oct 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Fixes: #66841

registerCallback() returns a plain bigint address, which doesn't keep the owning DynamicLibrary reachable. If user code dropped the library without calling close(), GC ran ~DynamicLibrary() and freed every ffi_closure. Native code still holding a callback pointer then segfaulted, even though no documented rule was broken.

This PR makes each FFICallback hold its owning library through a BaseObjectPtr, so the library stays strong until its last callback is unregistered or close() is called. Callbacks after unrefCallback() still count, since their trampolines remain live native memory. Because callbacks and library now reference each other, an Environment cleanup hook releases the callbacks so the library is still freed at teardown (worker or process exit).


Assisted-by: claude:opus-5.5

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot

nodejs-github-bot commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/ffi
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Oct 19, 2026
@trivikr trivikr added the ffi Issues and PRs related to experimental Foreign Function Interface support. label Oct 19, 2026
@codecov

codecov Bot commented Oct 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.67%. Comparing base (510d84c) to head (b0c03a2).
⚠️ Report is 24 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66842      +/-   ##
==========================================
+ Coverage   90.65%   90.67%   +0.02%     
==========================================
  Files         797      797              
  Lines      278783   278791       +8     
  Branches    53618    53630      +12     
==========================================
+ Hits       252718   252799      +81     
+ Misses      16420    16345      -75     
- Partials     9645     9647       +2     
Files with missing lines Coverage Δ
src/node_ffi.cc 73.05% <100.00%> (+0.24%) ⬆️
src/node_ffi.h 77.77% <ø> (ø)

... and 40 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ShogunPanda
ShogunPanda approved these changes Oct 19, 2026
View reviewed changes
@trivikr trivikr added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Oct 19, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Oct 19, 2026
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@trivikr trivikr added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Oct 19, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Oct 19, 2026
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@trivikr trivikr added the commit-queue PRs queued for automated landing through the Commit Queue. label Oct 19, 2026
addaleax
addaleax reviewed Oct 20, 2026
View reviewed changes

@addaleax addaleax left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just going to point out that there is an existing for this problem in the form of using BaseObjectPtr<DynamicLibrary> owner; instead of DynamicLibrary* owner; in struct FFICallback

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@addaleax addaleax removed commit-queue PRs queued for automated landing through the Commit Queue. author ready PRs with CI started, the required approvals, and no outstanding review comments. labels Oct 20, 2026
@trivikr
trivikr force-pushed the ffi-dynlib-gc branch from 7e8c874 to 6344ae1 Compare October 21, 2026 06:21
@trivikr
trivikr requested review from ShogunPanda and addaleax October 21, 2026 06:23
@trivikr

trivikr commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

Just going to point out that there is an existing for this problem in the form of using BaseObjectPtr<DynamicLibrary> owner; instead of DynamicLibrary* owner; in struct FFICallback

Thanks. Switched to BaseObjectPtr. The resulting cycle aborted in ~Realm on worker exit, so the library now releases its callbacks from a cleanup hook. Added a test for that case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr trivikr added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Oct 21, 2026
@trivikr

trivikr commented Oct 23, 2026

Copy link
Copy Markdown
Member Author

cc @addaleax Can you take a look? It now uses BaseObjectPtr

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr

trivikr commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

cc @ShogunPanda Can you review this again?

It now uses BaseObjectPtr as requested.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr
ffi: keep DynamicLibrary alive while callbacks are registered
b0c03a2
Callback pointers returned by registerCallback() are plain bigint
addresses, so they do not keep the owning DynamicLibrary reachable.
If user code dropped the library without calling close(), the next GC
ran ~DynamicLibrary(), which freed every ffi_closure. Native code still
holding a callback pointer then jumped into freed trampoline memory and
crashed the process, even though close() was never called.

Hold the owning library through a BaseObjectPtr in each FFICallback,
so it stays strong until its last callback is unregistered or the
library is closed. Callbacks released with unrefCallback() still count,
because their trampolines remain live native memory. Since this forms a
cycle, release the callbacks from an Environment cleanup hook so the
library is still freed at teardown.

Signed-off-by: Trivikram Kamat <[email protected]>
Assisted-by: claude:opus-5.5
@trivikr
trivikr force-pushed the ffi-dynlib-gc branch from 6344ae1 to b0c03a2 Compare October 29, 2026 05:34
addaleax
addaleax approved these changes Oct 30, 2026
View reviewed changes
@addaleax

addaleax commented Oct 30, 2026

Copy link
Copy Markdown
Member

sorry for the delay!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@addaleax addaleax added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Oct 30, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Oct 30, 2026
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@trivikr trivikr added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Oct 30, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Oct 30, 2026
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@trivikr trivikr added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Oct 31, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Oct 31, 2026
@nodejs-github-bot

nodejs-github-bot commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/78757/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr trivikr added the commit-queue PRs queued for automated landing through the Commit Queue. label Oct 31, 2026
@nodejs-github-bot
nodejs-github-bot merged commit d44625a into nodejs:main Oct 31, 2026
73 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

Landed in d44625a

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Oct 31, 2026
aduh95 pushed a commit that referenced this pull request Nov 11, 2026
@trivikr @aduh95
ffi: keep DynamicLibrary alive while callbacks are registered
7a24b2b
Callback pointers returned by registerCallback() are plain bigint
addresses, so they do not keep the owning DynamicLibrary reachable.
If user code dropped the library without calling close(), the next GC
ran ~DynamicLibrary(), which freed every ffi_closure. Native code still
holding a callback pointer then jumped into freed trampoline memory and
crashed the process, even though close() was never called.

Hold the owning library through a BaseObjectPtr in each FFICallback,
so it stays strong until its last callback is unregistered or the
library is closed. Callbacks released with unrefCallback() still count,
because their trampolines remain live native memory. Since this forms a
cycle, release the callbacks from an Environment cleanup hook so the
library is still freed at teardown.

Signed-off-by: Trivikram Kamat <[email protected]>
Assisted-by: claude:opus-5.5
PR-URL: #66842
Fixes: #66841
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
@github-actions github-actions Bot mentioned this pull request Nov 11, 2026
2026-11-12, Version 27.0.0-alpha.0.1.0 (Alpha) #67415
Merged
aduh95 pushed a commit that referenced this pull request Nov 12, 2026
@trivikr @aduh95
ffi: keep DynamicLibrary alive while callbacks are registered
a613c18
Callback pointers returned by registerCallback() are plain bigint
addresses, so they do not keep the owning DynamicLibrary reachable.
If user code dropped the library without calling close(), the next GC
ran ~DynamicLibrary(), which freed every ffi_closure. Native code still
holding a callback pointer then jumped into freed trampoline memory and
crashed the process, even though close() was never called.

Hold the owning library through a BaseObjectPtr in each FFICallback,
so it stays strong until its last callback is unregistered or the
library is closed. Callbacks released with unrefCallback() still count,
because their trampolines remain live native memory. Since this forms a
cycle, release the callbacks from an Environment cleanup hook so the
library is still freed at teardown.

Signed-off-by: Trivikram Kamat <[email protected]>
Assisted-by: claude:opus-5.5
PR-URL: #66842
Fixes: #66841
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
@github-actions github-actions Bot mentioned this pull request Nov 20, 2026
2026-11-30, Version 26.14.0 'Lithium' (LTS) #67664
Open
gabylb pushed a commit to gabylb/node that referenced this pull request Nov 27, 2026
@trivikr @gabylb
ffi: keep DynamicLibrary alive while callbacks are registered
4709607
Callback pointers returned by registerCallback() are plain bigint
addresses, so they do not keep the owning DynamicLibrary reachable.
If user code dropped the library without calling close(), the next GC
ran ~DynamicLibrary(), which freed every ffi_closure. Native code still
holding a callback pointer then jumped into freed trampoline memory and
crashed the process, even though close() was never called.

Hold the owning library through a BaseObjectPtr in each FFICallback,
so it stays strong until its last callback is unregistered or the
library is closed. Callbacks released with unrefCallback() still count,
because their trampolines remain live native memory. Since this forms a
cycle, release the callbacks from an Environment cleanup hook so the
library is still freed at teardown.

Signed-off-by: Trivikram Kamat <[email protected]>
Assisted-by: claude:opus-5.5
PR-URL: nodejs#66842
Fixes: nodejs#66841
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@addaleax addaleax addaleax approved these changes
@ShogunPanda ShogunPanda Awaiting requested review from ShogunPanda

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. c++ Issues and PRs that require attention from people who are familiar with C++. ffi Issues and PRs related to experimental Foreign Function Interface support. needs-ci PRs that need a full CI run.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

ffi: GC of DynamicLibrary frees registered callbacks still held by native code

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@trivikr @nodejs-github-bot @addaleax @ShogunPanda
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.