Skip to content

ffi: prevent premature GC of DynamicLibrary#63024

Open
semimikoh wants to merge 1 commit intonodejs:mainfrom
semimikoh:fix/ffi-premature-library-gc
Open

ffi: prevent premature GC of DynamicLibrary#63024
semimikoh wants to merge 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
Signed-off-by: semimikoh <ejffjeosms@gmail.com>
@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.65%. Comparing base (8173251) to head (5d3ffc4).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #63024      +/-   ##
==========================================
- Coverage   91.49%   89.65%   -1.85%     
==========================================
  Files         358      708     +350     
  Lines      151574   220376   +68802     
  Branches    23921    42263   +18342     
==========================================
+ Hits       138679   197570   +58891     
- Misses      12619    14655    +2036     
- Partials      276     8151    +7875     
Files with missing lines Coverage Δ
src/node_ffi.cc 69.84% <100.00%> (ø)
src/node_ffi.h 72.72% <ø> (ø)

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

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

Labels

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

3 participants