GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||
GeezerVison is showing github.com — ffi: keep DynamicLibrary alive while callbacks are registered by trivikr · Pull Request #66842 · nodejs/node Conversation
|
|
Review requested:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
7e8c874 to
6344ae1
Compare
Thanks. Switched to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
cc @addaleax Can you take a look? It now uses |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
cc @ShogunPanda Can you review this again? It now uses |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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
6344ae1 to
b0c03a2
Compare
|
sorry for the delay! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Landed in d44625a |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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]>
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]>
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]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
ffi: GC of DynamicLibrary frees registered callbacks still held by native code
There was an error while loading. Please reload this page.