node-api: execute tsfn finalizer after queue drains when aborted#61956
Open
KevinEady wants to merge 3 commits intonodejs:mainfrom
Open
node-api: execute tsfn finalizer after queue drains when aborted#61956KevinEady wants to merge 3 commits intonodejs:mainfrom
KevinEady wants to merge 3 commits intonodejs:mainfrom
Conversation
A threadsafe function may utilize its context in its `call_js` callback. The context should be valid during draining of the queue when the threadsafe function is aborted. Fixes: nodejs#60026
Collaborator
|
Review requested:
|
Contributor
Author
|
Without the fix commit, the new test fails: With the fix commit, the process completes as expected. |
Add a test where a threadsafe function's `call_js` callback uses its context which is freed in the threadsafe function's finalizer.
3ff5d15 to
d52e75c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #61956 +/- ##
==========================================
+ Coverage 88.84% 89.66% +0.82%
==========================================
Files 674 676 +2
Lines 204957 206465 +1508
Branches 39309 39538 +229
==========================================
+ Hits 182087 185135 +3048
+ Misses 15088 13453 -1635
- Partials 7782 7877 +95
🚀 New features to boost your workflow:
|
toyobayashi
added a commit
to toyobayashi/emnapi
that referenced
this pull request
Mar 6, 2026
legendecas
reviewed
Mar 6, 2026
|
|
||
| std::function<void(void*)> deleter = [](void* ptr) { | ||
| std::fprintf(stderr, "Context: deleter called\n"); | ||
| delete static_cast<int*>(ptr); |
Member
There was a problem hiding this comment.
Can we add a field to Context, for example, int stage, and in each callback increment the stage and assert the value, to ensure that the callback and the finalizer are invoked in order? This could avoid relying on logs or SEGV to verify the call order.
Contributor
Author
There was a problem hiding this comment.
As discussed in the 13 March 2026 Node-API meeting, I've updated the comment on #61956 (comment) based on the updated test 👍 .
toyobayashi
added a commit
to toyobayashi/emnapi
that referenced
this pull request
Mar 10, 2026
bc464e2 to
e9fec8b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Execute a threadsafe function's finalizer after the queue drains. It may be the case that the finalizer will free the context, which might be needed in order to properly clean up the data in the
call_jscallback during abort. If the finalizer runs before the queue drains, it may point to invalid memory.