-
Notifications
You must be signed in to change notification settings - Fork 4.3k
napi: polyfill uv_thread_* against pthread #29261
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
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
farm/c3c42f78/uv-thread-polyfills
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4447621
napi: polyfill uv_thread_* against pthread (fixes #29260)
robobun 7b7e6ad
[autofix.ci] apply automated fixes
autofix-ci[bot] f7d0afc
review: no-op detach, drop negative panic assertion, reorder asserts
robobun 5985ac9
review: match libuv stack-size logic verbatim; drop test timeout; pru…
robobun 55f4d12
review: trim 29260 test preamble; assert return value not type
robobun 8509520
no-validate-exceptions: skip 29260 under ASAN
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
Some comments aren't visible on the classic Files Changed page.
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
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,70 @@ | ||
| // Regression test for https://github.com/oven-sh/bun/issues/29260: | ||
| // NAPI modules (e.g. ffi-napi) call uv_thread_self at module init. This | ||
| // addon exercises uv_thread_self + uv_thread_equal + uv_thread_create + | ||
| // uv_thread_join + uv_thread_detach + uv_thread_create_ex. If any of them | ||
| // is not polyfilled, Bun panics with "unsupported uv function". | ||
|
|
||
| #include <node_api.h> | ||
| #include <stdio.h> | ||
| #include <uv.h> | ||
|
|
||
| static void thread_entry(void *arg) { | ||
| int *counter = (int *)arg; | ||
| *counter = 42; | ||
| } | ||
|
|
||
| static napi_value fail(napi_env env, const char *msg) { | ||
| napi_throw_error(env, NULL, msg); | ||
| return NULL; | ||
| } | ||
|
|
||
| NAPI_MODULE_INIT(/* napi_env env, napi_value exports */) { | ||
| // uv_thread_self + uv_thread_equal: self must equal self. | ||
| uv_thread_t self1 = uv_thread_self(); | ||
| uv_thread_t self2 = uv_thread_self(); | ||
| if (!uv_thread_equal(&self1, &self2)) { | ||
| return fail(env, "uv_thread_equal(self, self) returned false"); | ||
| } | ||
|
|
||
| // uv_thread_create + uv_thread_join: spawn, join, verify the thread ran. | ||
| int counter = 0; | ||
| uv_thread_t tid; | ||
| if (uv_thread_create(&tid, thread_entry, &counter) != 0) { | ||
| return fail(env, "uv_thread_create failed"); | ||
| } | ||
| if (uv_thread_join(&tid) != 0) { | ||
| return fail(env, "uv_thread_join failed"); | ||
| } | ||
| if (counter != 42) { | ||
| return fail(env, "uv_thread_create: thread did not run"); | ||
| } | ||
|
|
||
| // uv_thread_create_ex: no flags → default pthread stack size. | ||
| counter = 0; | ||
| uv_thread_options_t opts; | ||
| opts.flags = UV_THREAD_NO_FLAGS; | ||
| if (uv_thread_create_ex(&tid, &opts, thread_entry, &counter) != 0) { | ||
| return fail(env, "uv_thread_create_ex failed"); | ||
| } | ||
| if (uv_thread_join(&tid) != 0) { | ||
| return fail(env, "uv_thread_join (after _ex) failed"); | ||
| } | ||
| if (counter != 42) { | ||
| return fail(env, "uv_thread_create_ex: thread did not run"); | ||
| } | ||
|
|
||
| // uv_thread_detach: spawn, detach, the thread cleans up on its own. | ||
| counter = 0; | ||
| if (uv_thread_create(&tid, thread_entry, &counter) != 0) { | ||
| return fail(env, "uv_thread_create (detach) failed"); | ||
| } | ||
| if (uv_thread_detach(&tid) != 0) { | ||
| return fail(env, "uv_thread_detach failed"); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| napi_value result; | ||
robobun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (napi_get_boolean(env, true, &result) != napi_ok) { | ||
| return fail(env, "napi_get_boolean failed"); | ||
| } | ||
| return result; | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.