René's URL Explorer Experiment


Title: napi_threadsafe_function is very hard to use safely · Issue #55706 · nodejs/node · GitHub

Open Graph Title: napi_threadsafe_function is very hard to use safely · Issue #55706 · nodejs/node

X Title: napi_threadsafe_function is very hard to use safely · Issue #55706 · nodejs/node

Description: Version v22.10.0 Platform Linux s7 5.15.0-69-generic #76-Ubuntu SMP Fri Mar 17 17:19:29 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux Subsystem node-api What steps will reproduce the bug? The handle obtained with napi_create_threadsafe_functio...

Open Graph Description: Version v22.10.0 Platform Linux s7 5.15.0-69-generic #76-Ubuntu SMP Fri Mar 17 17:19:29 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux Subsystem node-api What steps will reproduce the bug? The handle obta...

X Description: Version v22.10.0 Platform Linux s7 5.15.0-69-generic #76-Ubuntu SMP Fri Mar 17 17:19:29 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux Subsystem node-api What steps will reproduce the bug? The handle obta...

Opengraph URL: https://github.com/nodejs/node/issues/55706

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"napi_threadsafe_function is very hard to use safely","articleBody":"### Version\n\nv22.10.0\n\n### Platform\n\n```text\nLinux s7 5.15.0-69-generic #76-Ubuntu SMP Fri Mar 17 17:19:29 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux\n```\n\n\n### Subsystem\n\nnode-api\n\n### What steps will reproduce the bug?\n\nThe handle obtained with `napi_create_threadsafe_function` is supposed to be usable by arbitrary threads.\r\nIn particular, the following operations should be safe to call from arbitrary threads (until that thread calls `napi_release_threadsafe_function` or receives `napi_closing` from `napi_call_threadsafe_function`):\r\n`napi_get_threadsafe_function_context`, `napi_call_threadsafe_function`, `napi_acquire_threadsafe_function`, and `napi_release_threadsafe_function`.\r\n\r\nHowever, this is currently not the case.\r\nIn particular, in the face of Node.js environment shutdown there are data races and use-after-frees that occur when threads call any of these functions during or after the cleanup that happens when the Node.js environment shuts down.\r\n\r\nThere are two main issues I already found:\r\n- During finalization, the queue is accessed without holding its mutex [here](https://github.com/nodejs/node/blob/b38e3124862f7623fd8e8f0a9b738065d9296a74/src/node_api.cc#L300)\r\n    - If another thread calls `napi_call_threadsafe_function` concurrently, this leads to a data race on the queue internals [here](https://github.com/nodejs/node/blob/b38e3124862f7623fd8e8f0a9b738065d9296a74/src/node_api.cc#L243)\r\n- At the end of finalization, the whole internal state is just deleted [here](https://github.com/nodejs/node/blob/4f5db8b26d906f1cbe9f6a9ac2028b0f7ad88c91/src/node_api.cc#L303) even if there are still threads holding handles to the tsnf.\r\n    - Afterwards each use of any of the above-mentioned functions is a use-after-free bug\r\n\r\nSee https://github.com/mika-fischer/node-bug-napi-tsfn for a detailed reproduction of the issue on Linux using valgrind\r\n\r\nWhile working around this is technically possible (see [src/run/fixed.hpp](https://github.com/mika-fischer/node-bug-napi-tsfn/blob/main/src/run_fixed.hpp)) it involves attaching a finalizer in order to track the finalization state in an external flag, whose lifetime must be managed via a shared_ptr or similar and all access to the TSFN must be protected by another mutex (or read-write-lock). This makes the whole thing very unergonomic to use and I'm pretty sure nobody will jump through these hoops.\r\n\r\nIt should also be relatively easy to fix and not break API or ABI\n\n### How often does it reproduce? Is there a required condition?\n\nAlways\n\n### What is the expected behavior? Why is that the expected behavior?\n\n- Finalization should lock the mutex to make concurrent calls safe.\r\n- Finalization should put the TSFN into a state where all its resources are released, but *only* delete the actual TSFN object if there are no more handles to it. Otherwise the actual deletion should be deferred until one of `napi_release_threadsafe_function` or `napi_call_threadsafe_function` decreases the thread_count to zero.\r\n- In this finalized-but-not-yet deleted state, the operations mentioned above should work as follows:\r\n  - `napi_get_threadsafe_function_context` should return the stored context pointer as usual\r\n  - `napi_call_threadsafe_function` should return `napi_closing`, decrease the thread_count and if it falls to zero delete the TSFN\r\n  - `napi_acquire_threadsafe_function` should return `napi_closing`\r\n  - `napi_release_threadsafe_function` should return `napi_ok`, decrease the thread_count and if it falls to zero delete the TSFN\n\n### What do you see instead?\n\ndata races \u0026 use-after-frees leading to crashes\n\n### Additional information\n\n_No response_","author":{"url":"https://github.com/mika-fischer","@type":"Person","name":"mika-fischer"},"datePublished":"2024-11-03T17:22:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/55706/node/issues/55706"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:a354f038-5469-f203-c8a0-596b4c4ae1e5
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAB0E:3555CE:9479A:C79C2:6A4D2E47
html-safe-nonce8075f39700a65edcaedc98d3583c15fb73a83912e734aeece92fc44a69f55dae
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjBFOjM1NTVDRTo5NDc5QTpDNzlDMjo2QTREMkU0NyIsInZpc2l0b3JfaWQiOiIyMTg0OTUyNzg4NzI2MDQyMzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacd5ee1a532f74c0ac0ea297eaea6dfce97026e485d1c0d987688d802029fae6fc
hovercard-subject-tagissue:2631341857
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/55706/issue_layout
twitter:imagehttps://opengraph.githubassets.com/87a8a55b2648c416d3cb37f5203e133ec79855241d7448a0db3e9bfce70cde51/nodejs/node/issues/55706
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/87a8a55b2648c416d3cb37f5203e133ec79855241d7448a0db3e9bfce70cde51/nodejs/node/issues/55706
og:image:altVersion v22.10.0 Platform Linux s7 5.15.0-69-generic #76-Ubuntu SMP Fri Mar 17 17:19:29 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux Subsystem node-api What steps will reproduce the bug? The handle obta...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemika-fischer
hostnamegithub.com
expected-hostnamegithub.com
None92571a8944142227b7e19cd10918b1ddd06e5066c1ad5bc7e4769cf6140a87e6
turbo-cache-controlno-preview
go-importgithub.com/nodejs/node git https://github.com/nodejs/node.git
octolytics-dimension-user_id9950313
octolytics-dimension-user_loginnodejs
octolytics-dimension-repository_id27193779
octolytics-dimension-repository_nwonodejs/node
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id27193779
octolytics-dimension-repository_network_root_nwonodejs/node
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release93f17a978ee60bc4668e1d7b90e6bd2d622261fd
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/nodejs/node/issues/55706#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fnodejs%2Fnode%2Fissues%2F55706
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fnodejs%2Fnode%2Fissues%2F55706
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=nodejs%2Fnode
Reloadhttps://github.com/nodejs/node/issues/55706
Reloadhttps://github.com/nodejs/node/issues/55706
Reloadhttps://github.com/nodejs/node/issues/55706
Please reload this pagehttps://github.com/nodejs/node/issues/55706
nodejs https://github.com/nodejs
nodehttps://github.com/nodejs/node
Please reload this pagehttps://github.com/nodejs/node/issues/55706
Notifications https://github.com/login?return_to=%2Fnodejs%2Fnode
Fork 36k https://github.com/login?return_to=%2Fnodejs%2Fnode
Star 118k https://github.com/login?return_to=%2Fnodejs%2Fnode
Code https://github.com/nodejs/node
Issues 1.4k https://github.com/nodejs/node/issues
Pull requests 956 https://github.com/nodejs/node/pulls
Actions https://github.com/nodejs/node/actions
Projects https://github.com/nodejs/node/projects
Security and quality 0 https://github.com/nodejs/node/security
Insights https://github.com/nodejs/node/pulse
Code https://github.com/nodejs/node
Issues https://github.com/nodejs/node/issues
Pull requests https://github.com/nodejs/node/pulls
Actions https://github.com/nodejs/node/actions
Projects https://github.com/nodejs/node/projects
Security and quality https://github.com/nodejs/node/security
Insights https://github.com/nodejs/node/pulse
napi_threadsafe_function is very hard to use safelyhttps://github.com/nodejs/node/issues/55706#top
https://github.com/gabrielschulhof
node-apiIssues and PRs related to the Node-API.https://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22node-api%22
https://github.com/mika-fischer
mika-fischerhttps://github.com/mika-fischer
on Nov 3, 2024https://github.com/nodejs/node/issues/55706#issue-2631341857
herehttps://github.com/nodejs/node/blob/b38e3124862f7623fd8e8f0a9b738065d9296a74/src/node_api.cc#L300
herehttps://github.com/nodejs/node/blob/b38e3124862f7623fd8e8f0a9b738065d9296a74/src/node_api.cc#L243
herehttps://github.com/nodejs/node/blob/4f5db8b26d906f1cbe9f6a9ac2028b0f7ad88c91/src/node_api.cc#L303
https://github.com/mika-fischer/node-bug-napi-tsfnhttps://github.com/mika-fischer/node-bug-napi-tsfn
src/run/fixed.hpphttps://github.com/mika-fischer/node-bug-napi-tsfn/blob/main/src/run_fixed.hpp
gabrielschulhofhttps://github.com/gabrielschulhof
node-apiIssues and PRs related to the Node-API.https://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22node-api%22
Node-API Team Projecthttps://github.com/orgs/nodejs/projects/11
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.