Skip to content

ffi: prevent premature GC of DynamicLibrary#63024

Merged
nodejs-github-bot merged 1 commit intonodejs:mainfrom
semimikoh:fix/ffi-premature-library-gc
May 1, 2026
Merged

ffi: prevent premature GC of DynamicLibrary#63024
nodejs-github-bot merged 1 commit intonodejs:mainfrom
semimikoh:fix/ffi-premature-library-gc

Conversation

@semimikoh
Copy link
Copy Markdown
Contributor

@semimikoh semimikoh commented Apr 29, 2026

Fixes: #63010

Functions created by getFunction() do not hold a strong reference
to the owning DynamicLibrary. V8 may garbage-collect the library
while derived functions are still in use, causing spurious
ERR_FFI_LIBRARY_CLOSED errors in tight loops.

Store a Global<Object> reference to the library in
FFIFunctionInfo so the library remains alive as long as any
derived function exists. The reference is released when the
function is garbage-collected via CleanupFunctionInfo.

Verification

$ # reproduce issue #63010 with the test script from the report
$ out/Release/node --experimental-ffi test_ffi_gc.js

No errors. Script runs to completion.

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/ffi

@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 Apr 29, 2026
@semimikoh semimikoh changed the title ffi: prevent premature GC of DynamicLibrary ffi: prevent premature GC of DynamicLibrary Apr 29, 2026
@semimikoh semimikoh force-pushed the fix/ffi-premature-library-gc branch from 875711b to 5d3ffc4 Compare April 29, 2026 05:58
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.64%. Comparing base (8173251) to head (0d47435).
⚠️ Report is 18 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #63024      +/-   ##
==========================================
- Coverage   91.49%   89.64%   -1.85%     
==========================================
  Files         358      708     +350     
  Lines      151574   220415   +68841     
  Branches    23921    42272   +18351     
==========================================
+ Hits       138679   197596   +58917     
- Misses      12619    14675    +2056     
- Partials      276     8144    +7868     
Files with missing lines Coverage Δ
src/node_ffi.cc 69.84% <100.00%> (ø)
src/node_ffi.h 72.72% <ø> (ø)

... and 471 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.

@ShogunPanda ShogunPanda added request-ci Add this label to start a Jenkins CI on a PR. ffi Issues and PRs related to experimental Foreign Function Interface support. labels Apr 29, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 29, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@addaleax addaleax added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. labels Apr 29, 2026
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@semimikoh
Copy link
Copy Markdown
Contributor Author

Ready from my side, thanks!

@semimikoh semimikoh force-pushed the fix/ffi-premature-library-gc branch from 5d3ffc4 to 0d47435 Compare April 30, 2026 18:02
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels May 1, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/63024
✔  Done loading data for nodejs/node/pull/63024
----------------------------------- PR info ------------------------------------
Title      ffi: prevent premature GC of DynamicLibrary (#63024)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     semimikoh:fix/ffi-premature-library-gc -> nodejs:main
Labels     c++, author ready, needs-ci, ffi
Commits    1
 - ffi: prevent premature GC of DynamicLibrary
Committers 1
 - semimikoh <[email protected]>
PR-URL: https://github.com/nodejs/node/pull/63024
Fixes: https://github.com/nodejs/node/issues/63010
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Bryan English <[email protected]>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/63024
Fixes: https://github.com/nodejs/node/issues/63010
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Bryan English <[email protected]>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Wed, 29 Apr 2026 05:51:17 GMT
   ✔  Approvals: 4
   ✔  - Paolo Insogna (@ShogunPanda) (TSC): https://github.com/nodejs/node/pull/63024#pullrequestreview-4194878077
   ✔  - Anna Henningsen (@addaleax): https://github.com/nodejs/node/pull/63024#pullrequestreview-4197628494
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/63024#pullrequestreview-4197975170
   ✔  - Bryan English (@bengl): https://github.com/nodejs/node/pull/63024#pullrequestreview-4199352943
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2026-04-29T07:30:26Z: https://ci.nodejs.org/job/node-test-pull-request/73010/
   ⚠  Commits were pushed after the last Full PR CI run:
   ⚠  - ffi: prevent premature GC of DynamicLibrary
- Querying data for job/node-test-pull-request/73010/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/25205417009

@ShogunPanda ShogunPanda added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci Add this label to start a Jenkins CI on a PR. commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels May 1, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@ShogunPanda ShogunPanda added the commit-queue Add this label to land a pull request using GitHub Actions. label May 1, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label May 1, 2026
@nodejs-github-bot nodejs-github-bot merged commit 9c01018 into nodejs:main May 1, 2026
77 checks passed
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Landed in 9c01018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. 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

Development

Successfully merging this pull request may close these issues.

ffi: Random "Library is closed" exceptions at call time

6 participants