Title: ThreadsafeFunction in worker_threads cause segfault randomly · Issue #58484 · nodejs/node · GitHub
Open Graph Title: ThreadsafeFunction in worker_threads cause segfault randomly · Issue #58484 · nodejs/node
X Title: ThreadsafeFunction in worker_threads cause segfault randomly · Issue #58484 · nodejs/node
Description: Version v22.16.0 Platform Linux ubuntu-22.04 6.13.7-orbstack-00283-g9d1400e7e9c6 #104 SMP Mon Mar 17 06:15:48 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux Subsystem No response What steps will reproduce the bug? Steps clone https://github....
Open Graph Description: Version v22.16.0 Platform Linux ubuntu-22.04 6.13.7-orbstack-00283-g9d1400e7e9c6 #104 SMP Mon Mar 17 06:15:48 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux Subsystem No response What steps will reprod...
X Description: Version v22.16.0 Platform Linux ubuntu-22.04 6.13.7-orbstack-00283-g9d1400e7e9c6 #104 SMP Mon Mar 17 06:15:48 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux Subsystem No response What steps will reprod...
Opengraph URL: https://github.com/nodejs/node/issues/58484
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ThreadsafeFunction in worker_threads cause segfault randomly","articleBody":"### Version\n\nv22.16.0\n\n### Platform\n\n```text\nLinux ubuntu-22.04 6.13.7-orbstack-00283-g9d1400e7e9c6 #104 SMP Mon Mar 17 06:15:48 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux\n```\n\n### Subsystem\n\n_No response_\n\n### What steps will reproduce the bug?\n\n#### Steps\n\n- clone https://github.com/napi-rs/napi-rs\n- checkout `05-25-test_stress_test_on_aarch64_linux_gnu_platform` branch\n- Install latest Node.js and Rust\n- yarn install\n- yarn build:test\n- yarn workspace @examples/napi test __tests__/worker-thread.spec.ts --match '*worker_threads'\n\n#### Summary\n\nBecause NAPI-RS encapsulates too many things, I'll describe as briefly as possible the scenario where I encountered a segfault.\n\nHere is a simple `async` function in Rust:\n\n```rust\n#[napi]\npub fn buffer_pass_through(buffer: Buffer) -\u003e Buffer {\n buffer\n}\n```\n\nThe NAPI-RS would do these under the hood:\n\n- call `napi_create_promise` and get `defer` and `promise`, `promise` would return directly\n- call `napi_create_threadsafe_function` and pass the `defer` as the ThreadsafeFunction `context`\n- use `tokio::spawn` to run the async function, call the `ThreadsafeFunction` when async function has a value\n- call `napi_release_threadsafe_function` after the `ThreadsafeFunction` is called\n\nIf this function is not called in worker_threads, there's no problem. I tried writing a loop that calls it hundreds of thousands of times, and didn't find any issues.\n\nHowever, when this function is called in worker_threads, segfaults occasionally occur, which has been observed both in CI and in feedback from my users.\n\n\n\nBacktrace from lldb:\n\n```\n* thread #26, name = 'tokio-runtime-w', stop reason = signal SIGABRT\n * frame #0: 0x0000fffff7b07608 libc.so.6`__pthread_kill_implementation(threadid=281472292351904, signo=6, no_tid=\u003cunavailable\u003e) at pthread_kill.c:44:76\n frame #1: 0x0000fffff7abcb3c libc.so.6`__GI_raise(sig=6) at raise.c:26:13\n frame #2: 0x0000fffff7aa7e00 libc.so.6`__GI_abort at abort.c:79:7\n frame #3: 0x0000aaaaad7dfb38 node`uv_mutex_lock(mutex=0x0000fffdf816b038) at thread.c:345:5\n frame #4: 0x0000aaaaabeab7c4 node`node::LibuvMutexTraits::mutex_lock(mutex=0x0000fffdf816b038) at node_mutex.h:183:18\n frame #5: 0x0000aaaaabead48c node`node::MutexBase\u003cnode::LibuvMutexTraits\u003e::ScopedLock::ScopedLock(this=0x0000ffff5fffc370, mutex=0x0000fffdf816b038) at node_mutex.h:285:21\n frame #6: 0x0000aaaaac03f4d4 node`v8impl::(anonymous namespace)::ThreadSafeFunction::Release(this=0x0000fffdf816b010, mode=napi_tsfn_release) const at node_api.cc:276:45\n frame #7: 0x0000aaaaac043160 node`napi_release_threadsafe_function(func=0x0000fffdf816b010, mode=napi_tsfn_release) at node_api.cc:1411:70\n frame #8: 0x0000ffffddc45c78 example.linux-arm64-gnu.node`napi::js_values::deferred::JsDeferred$LT$Data$C$Resolver$GT$::call_tsfn::h488d0dbaa4a26cb5(self=JsDeferred\u003cnapi::js_values::unknown::Unknown, napi::tokio_runtime::execute_tokio_future::{async_block#0}::{closure_env#0}\u003cnapi::bindgen_runtime::js_values::arraybuffer::Uint8Array, napi_examples::typed_array::_napi_internal_register_array_buffer_pass_through::{closure#0}::{async_block_env#1}, napi_examples::typed_array::_napi_internal_register_array_buffer_pass_through::{closure#0}::{closure_env#2}, napi::error::Error\u003cnapi::status::Status\u003e\u003e\u003e @ 0x0000ffff5fffc490, result=\u003cunavailable\u003e) at deferred.rs:183:7\n frame #9: 0x0000ffffddc44d40 example.linux-arm64-gnu.node`napi::js_values::deferred::JsDeferred$LT$Data$C$Resolver$GT$::resolve::h3d2884560cd31212(self=JsDeferred\u003cnapi::js_values::unknown::Unknown, napi::tokio_runtime::execute_tokio_future::{async_block#0}::{closure_env#0}\u003cnapi::bindgen_runtime::js_values::arraybuffer::Uint8Array, napi_examples::typed_array::_napi_internal_register_array_buffer_pass_through::{closure#0}::{async_block_env#1}, napi_examples::typed_array::_napi_internal_register_array_buffer_pass_through::{closure#0}::{closure_env#2}, napi::error::Error\u003cnapi::status::Status\u003e\u003e\u003e @ 0x0000ffff5fffc520, resolver=\u003cunavailable\u003e) at deferred.rs:154:5\n frame #10: 0x0000ffffddb57d00 example.linux-arm64-gnu.node`napi::tokio_runtime::execute_tokio_future::_$u7b$$u7b$closure$u7d$$u7d$::ha7f1ee1bf2582723((null)=0x0000ffff5fffc9b0) at tokio_runtime.rs:233:16\n frame #11: 0x0000ffffdd9158fc example.linux-arm64-gnu.node`tokio::runtime::task::core::Core$LT$T$C$S$GT$::poll::_$u7b$$u7b$closure$u7d$$u7d$::hdd5c00f1ec17be65(ptr=0x0000fffdf815abb0) at core.rs:331:17\n frame #12: 0x0000ffffdd8fed1c example.linux-arm64-gnu.node`tokio::runtime::task::core::Core$LT$T$C$S$GT$::poll::h2a9ed49810ab1294 [inlined] tokio::loom::std::unsafe_cell::UnsafeCell$LT$T$GT$::with_mut::h21552376c10d6f31(self=0x0000fffdf815abb0, f={closure_env#0}\u003cnapi::tokio_runtime::execute_tokio_future::{async_block_env#0}\u003cnapi::bindgen_runtime::js_values::arraybuffer::Uint8Array, napi_examples::typed_array::_napi_internal_register_array_buffer_pass_through::{closure#0}::{async_block_env#1}, napi_examples::typed_array::_napi_internal_register_array_buffer_pass_through::{closure#0}::{closure_env#2}, napi::error::Error\u003cnapi::status::Status\u003e\u003e, alloc::sync::Arc\u003ctokio::runtime::scheduler::multi_thread::handle::Handle, alloc::alloc::Global\u003e\u003e @ 0x0000ffff5fffc968) at unsafe_cell.rs:16:9\n frame #13: 0x0000ffffdd8fed00 example.linux-arm64-gnu.node`tokio::runtime::task::core::Core$LT$T$C$S$GT$::poll::h2a9ed49810ab1294(self=0x0000fffdf815aba0, cx=\u003cunavailable\u003e) at core.rs:320:13\n```\n\n### How often does it reproduce? Is there a required condition?\n\nRepeat 3-5 times and it will appear randomly.\n\n### What is the expected behavior? Why is that the expected behavior?\n\nNo segfault\n\n### What do you see instead?\n\nSegfault\n\n### Additional information\n\nMaybe related: https://github.com/nodejs/node/issues/55706","author":{"url":"https://github.com/Brooooooklyn","@type":"Person","name":"Brooooooklyn"},"datePublished":"2025-05-27T15:53:41.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/58484/node/issues/58484"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e45e7634-0b2d-3f84-5ad8-f59f32aff992 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B6A:6D260:858D9:B5057:6A4D2E36 |
| html-safe-nonce | 963a8497755285b98a1027c9c6b7b698a86ab44fe1d603741082c297aea58669 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjZBOjZEMjYwOjg1OEQ5OkI1MDU3OjZBNEQyRTM2IiwidmlzaXRvcl9pZCI6IjY2NzUyODE4ODc0MDM3ODE2ODYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7939dc0b6b4057f3f58a882a9e4896a080f850566d4bbcb148ed8538dee01780 |
| hovercard-subject-tag | issue:3094425419 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/58484/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ad41613d3afe7b0d925e5842b942da7c45e8c64683a5f9466e169cfe52fbe790/nodejs/node/issues/58484 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ad41613d3afe7b0d925e5842b942da7c45e8c64683a5f9466e169cfe52fbe790/nodejs/node/issues/58484 |
| og:image:alt | Version v22.16.0 Platform Linux ubuntu-22.04 6.13.7-orbstack-00283-g9d1400e7e9c6 #104 SMP Mon Mar 17 06:15:48 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux Subsystem No response What steps will reprod... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Brooooooklyn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 92571a8944142227b7e19cd10918b1ddd06e5066c1ad5bc7e4769cf6140a87e6 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 93f17a978ee60bc4668e1d7b90e6bd2d622261fd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width