Title: ffi: GC of DynamicLibrary frees registered callbacks still held by native code · Issue #66841 · nodejs/node · GitHub
Open Graph Title: ffi: GC of DynamicLibrary frees registered callbacks still held by native code · Issue #66841 · nodejs/node
X Title: ffi: GC of DynamicLibrary frees registered callbacks still held by native code · Issue #66841 · nodejs/node
Description: Version main Platform macOS Subsystem ffi What steps will reproduce the bug? import { DynamicLibrary } from 'node:ffi'; const libraryPath = "../node/test/ffi/fixture_library/build/Release/ffi_test_library.dylib"; // A separate handle pro...
Open Graph Description: Version main Platform macOS Subsystem ffi What steps will reproduce the bug? import { DynamicLibrary } from 'node:ffi'; const libraryPath = "../node/test/ffi/fixture_library/build/Release/ffi_test_...
X Description: Version main Platform macOS Subsystem ffi What steps will reproduce the bug? import { DynamicLibrary } from 'node:ffi'; const libraryPath = "../node/test/ffi/fixture_library/build/Rele...
Opengraph URL: https://github.com/nodejs/node/issues/66841
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ffi: GC of DynamicLibrary frees registered callbacks still held by native code","articleBody":"### Version\n\nmain\n\n### Platform\n\n```text\nmacOS\n```\n\n### Subsystem\n\nffi\n\n### What steps will reproduce the bug?\n\n```js\nimport { DynamicLibrary } from 'node:ffi';\nconst libraryPath = \"../node/test/ffi/fixture_library/build/Release/ffi_test_library.dylib\";\n\n// A separate handle provides the native function that invokes the callback.\nconst keeper = new DynamicLibrary(libraryPath);\nconst call_int_callback = keeper.getFunction('call_int_callback',\n { arguments: ['pointer', 'i32'], return: 'i32' });\n\nlet lib = new DynamicLibrary(libraryPath);\nconst cbPtr = lib.registerCallback(\n { arguments: ['i32'], return: 'i32' }, (x) => x * 2);\n\nconsole.log(call_int_callback(cbPtr, 21)); // 42\n\nlib = null; // drop the handle without calling close()\nglobalThis.gc(); // ~DynamicLibrary -> Close() -> ffi_closure_free\n\nconsole.log(call_int_callback(cbPtr, 21)); // segmentation fault\n```\n\nRun with\n```console\nnode --expose-gc repro.mjs\n```\n\n### How often does it reproduce? Is there a required condition?\n\nAlways\n\n### What is the expected behavior? Why is that the expected behavior?\n\nA callback pointer from `library.registerCallback()` should stay valid until the user calls `library.close()` or `library.unregisterCallback(pointer)`. The docs only call it undefined behavior to invoke callback pointers after one of those explicit calls. Here neither was called.\n\n### What do you see instead?\n\nThe process crashes with a segmentation fault. Native code jumps into a freed `ffi_closure` trampoline, which is a use-after-free.\n\n### Additional information\n\n_No response_","author":{"url":"https://github.com/trivikr","@type":"Person","name":"trivikr"},"datePublished":"2026-10-19T03:34:44Z","url":"https://github.com/nodejs/node/issues/66841"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:230ed664-673c-bc41-52fa-7dc29689891e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB32:22EC8D:428117:59CFB6:6AFBED01 |
| html-safe-nonce | 013d546b36164da2b83706fdc32bf88ca331c4abe701781b324a821fa9afe3cf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjMyOjIyRUM4RDo0MjgxMTc6NTlDRkI2OjZBRkJFRDAxIiwidmlzaXRvcl9pZCI6IjU0MjE2MzQxMzU0NzI3MjkzNDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fbeb3b5eb4162116bc873828b9bd74de74c7dfdde5baf451d624af78bb7b490a |
| hovercard-subject-tag | issue:5917314371 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/66841/issue_layout |
| twitter:image | https://opengraph.githubassets.com/18937a6fffaad6b6ef8289909a57d107efc9602607dc991be8f11fc87ee52628/nodejs/node/issues/66841 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/18937a6fffaad6b6ef8289909a57d107efc9602607dc991be8f11fc87ee52628/nodejs/node/issues/66841 |
| og:image:alt | Version main Platform macOS Subsystem ffi What steps will reproduce the bug? import { DynamicLibrary } from 'node:ffi'; const libraryPath = "../node/test/ffi/fixture_library/build/Release/ffi_test_... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | trivikr |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width