-
Notifications
You must be signed in to change notification settings - Fork 5k
napi: keep async cleanup hook handles alive until the addon removes them #37204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jarred-Sumner
merged 5 commits into
main
from
farm/78bea1bc/napi-async-cleanup-hook-handle-uaf
Aug 9, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85a0dfd
napi: keep async cleanup hook handles alive until the addon removes them
robobun 46f360e
test: surface napi statuses in async cleanup hook fixture output
robobun 5365c7a
Trim comments, make removeAsyncCleanupHook void, pin expected test ou…
robobun c3a0369
test: split on either line ending for the Windows CRT
robobun 0090ca1
ci: retrigger
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // An async cleanup hook releases a threadsafe function, and the threadsafe | ||
| // function's finalizer (which runs later during env teardown) calls | ||
| // napi_remove_async_cleanup_hook. The handle must stay valid until the addon | ||
| // removes it; freeing it as soon as the hook returns is a use-after-free. | ||
| // See https://github.com/oven-sh/bun/issues/37201 | ||
| #define NAPI_EXPERIMENTAL | ||
| #include <node_api.h> | ||
| #include <stdio.h> | ||
|
|
||
| // Statuses are printed (and compared against Node's output by the test), so a | ||
| // call that fails without crashing still fails the test. | ||
| #define CHECK(expr) \ | ||
| do { \ | ||
| napi_status status_ = (expr); \ | ||
| if (status_ != napi_ok) { \ | ||
| printf(#expr " failed: status=%d\n", status_); \ | ||
| fflush(stdout); \ | ||
| return NULL; \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| static napi_async_cleanup_hook_handle hook_handle; | ||
| static napi_threadsafe_function tsfn; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| static void async_cleanup_hook(napi_async_cleanup_hook_handle handle, void* arg) { | ||
| printf("async cleanup hook fired\n"); | ||
| fflush(stdout); | ||
| napi_status status = napi_release_threadsafe_function(tsfn, napi_tsfn_release); | ||
| printf("released tsfn: status=%d\n", status); | ||
| fflush(stdout); | ||
| } | ||
|
|
||
| static void tsfn_finalize(napi_env env, void* data, void* hint) { | ||
| printf("tsfn finalize: removing async cleanup hook\n"); | ||
| fflush(stdout); | ||
| napi_status status = napi_remove_async_cleanup_hook(hook_handle); | ||
| printf("async cleanup hook removed: status=%d\n", status); | ||
| fflush(stdout); | ||
|
robobun marked this conversation as resolved.
|
||
| } | ||
|
|
||
| static void call_js_cb(napi_env env, napi_value js_cb, void* ctx, void* data) {} | ||
|
|
||
| static napi_value noop(napi_env env, napi_callback_info info) { | ||
| return NULL; | ||
| } | ||
|
|
||
| static napi_value start(napi_env env, napi_callback_info info) { | ||
| napi_value name; | ||
| CHECK(napi_create_string_utf8(env, "repro", NAPI_AUTO_LENGTH, &name)); | ||
| napi_value js_cb; | ||
| CHECK(napi_create_function(env, "noop", NAPI_AUTO_LENGTH, noop, NULL, &js_cb)); | ||
| CHECK(napi_create_threadsafe_function(env, js_cb, NULL, name, 0, 1, NULL, tsfn_finalize, NULL, call_js_cb, &tsfn)); | ||
| CHECK(napi_unref_threadsafe_function(env, tsfn)); | ||
| CHECK(napi_add_async_cleanup_hook(env, async_cleanup_hook, NULL, &hook_handle)); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return NULL; | ||
| } | ||
|
|
||
| napi_value Init(napi_env env, napi_value exports) { | ||
| napi_value fn; | ||
| napi_create_function(env, "start", NAPI_AUTO_LENGTH, start, NULL, &fn); | ||
| napi_set_named_property(env, exports, "start", fn); | ||
| return exports; | ||
| } | ||
|
|
||
| NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.