-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Implement uv_thread_self on POSIX #29228
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
73a186b
Implement uv_thread_self on POSIX
robobun 7de5514
address review feedback
robobun c094ab1
fix remaining uv-posix-polyfills.cpp → .c typos in comment block
robobun 409b49e
drop unused async from sync regression test callback
robobun 3db8f82
trim regression test: drop prose comment block and redundant makeTree
robobun c5db337
regression test: drop unused ldflags, widen beforeAll timeout
robobun d1aa0fb
strengthen uv_thread_self unit check; drop beforeAll timeout
robobun 2b92865
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
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,91 @@ | ||
| // https://github.com/oven-sh/bun/issues/29223 | ||
| import { beforeAll, describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isWindows, tempDirWithFiles } from "harness"; | ||
| import path from "node:path"; | ||
|
|
||
| // Windows uses real libuv; the POSIX-stub code path does not apply there. | ||
| describe.if(!isWindows)("issue #29223", () => { | ||
| let tempdir: string = ""; | ||
|
|
||
| // Build the addon once in beforeAll (same pattern as test/napi/uv.test.ts). | ||
| beforeAll(async () => { | ||
| const addonSource = ` | ||
| #include <node_api.h> | ||
| #include <pthread.h> | ||
| #include <uv.h> | ||
|
|
||
| napi_value Init(napi_env env, napi_value exports) { | ||
| // This is what ffi-napi does: call uv_thread_self() while the NAPI | ||
| // module is being constructed. Before the fix this panicked Bun. | ||
| uv_thread_t self = uv_thread_self(); | ||
|
|
||
| // Also check that calling it twice from the same thread agrees with | ||
| // pthread_self() — proves we actually implemented it rather than | ||
| // returning a garbage value. | ||
| uv_thread_t again = uv_thread_self(); | ||
| int equal = pthread_equal(self, again) != 0 && pthread_equal(self, pthread_self()) != 0; | ||
|
|
||
| napi_value equal_js; | ||
| napi_get_boolean(env, equal, &equal_js); | ||
| napi_set_named_property(env, exports, "equal", equal_js); | ||
| return exports; | ||
| } | ||
|
|
||
| NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) | ||
| `; | ||
|
|
||
| const files = { | ||
| "addon.c": addonSource, | ||
| "package.json": JSON.stringify({ | ||
| name: "issue-29223-addon", | ||
| version: "0.0.0", | ||
| private: true, | ||
| scripts: { "build:napi": "node-gyp configure && node-gyp build" }, | ||
| dependencies: { "node-gyp": "10.2.0" }, | ||
| }), | ||
| "binding.gyp": `{ | ||
| "targets": [ | ||
| { | ||
| "target_name": "addon", | ||
| "sources": [ "addon.c" ], | ||
| "include_dirs": [ ".", "./libuv" ], | ||
| "cflags": ["-fPIC"] | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
| ] | ||
| }`, | ||
| "index.js": `const addon = require("./build/Release/addon.node"); | ||
| if (addon.equal !== true) { | ||
| console.error("FAIL: uv_thread_self returned inconsistent results"); | ||
| process.exit(2); | ||
| } | ||
| console.log("OK"); | ||
| `, | ||
| }; | ||
|
|
||
| tempdir = tempDirWithFiles("issue-29223", files); | ||
|
|
||
| // node-gyp uses the libuv headers we vendor for the stubs. | ||
| const libuvDir = path.join(import.meta.dir, "../../../src/bun.js/bindings/libuv"); | ||
| await Bun.$`cp -R ${libuvDir} ${path.join(tempdir, "libuv")}`.env(bunEnv); | ||
| await Bun.$`${bunExe()} install && ${bunExe()} run build:napi`.env(bunEnv).cwd(tempdir); | ||
| // cold `bun install` + node-gyp build can exceed the default 5s hook timeout on CI | ||
| }, 60_000); | ||
|
Check warning on line 72 in test/regression/issue/29223.test.ts
|
||
|
robobun marked this conversation as resolved.
Outdated
|
||
|
|
||
| test("NAPI addon calling uv_thread_self during Init does not crash", () => { | ||
| // spawnSync because the baseline (pre-fix) crashes via panic + abort; | ||
| // spawn + proc.exited can hang on such aborts under the test runner. | ||
| const { stdout, exitCode } = Bun.spawnSync({ | ||
| cmd: [bunExe(), "index.js"], | ||
| env: bunEnv, | ||
| cwd: tempdir, | ||
| stderr: "pipe", | ||
| stdout: "pipe", | ||
| }); | ||
|
|
||
| // The addon prints "OK" from index.js only if require() succeeded and | ||
| // uv_thread_self() returned a thread id consistent with pthread_self(). | ||
| // Pre-fix, require() panics the process and stdout stays empty. | ||
| expect(stdout.toString().trim()).toBe("OK"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); | ||
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.