Skip to content

N-API: Need SharedArrayBuffer api #23276

Description

@orange4glace

needed feature :
napi_create_external_sharedarraybuffer
napi_get_sharedarraybuffer_info
napi_is_sharedarraybuffer

Since V8 api supports external sharedarraybuffer creation, it would be nice if n-api implements this.

Activity

  1. changed the title [-]N-API: Need SharedArrayBuffer feature[/-] [+]N-API: Need SharedArrayBuffer api[/+] on Oct 5, 2018
  2. addaleax commented on Oct 5, 2018

    @addaleax
    Member

    @orange4glace Is there a reason you closed this issue?

    In any case, one issue with externalized SharedArrayBuffers is that you won’t actually be able to share them between Workers – that doesn’t make them all too useful.

  3. orange4glace commented on Oct 5, 2018

    @orange4glace
    Author

    @addleax Is there any reason that it can't be shared with workers? I am planning to use it with Electron so the native module will be loaded in Browser process.

    PS. I closed it so I can make a PR instead.

  4. addaleax commented on Oct 5, 2018

    @addaleax
    Member

    @orange4glace I don’t know how Electron/Chromium deals with that, I was just referring to the worker_threads module in Node.js 10+.

    The reason it doesn’t work with our Workers is that SharedArrayBuffers can be garbage collected multiple times on different threads, and only when the last reference to it from any thread is gone, we can free it. For that, we need to know how to release the memory – that’s easiest if we don’t allow sharing externalized SharedArrayBuffers. You can still share internalized SharedArrayBuffers without any problems, whether they are created by addons or not.

  5. orange4glace commented on Oct 5, 2018

    @orange4glace
    Author

    Well, actually my module will fully manage the lifetime of the underlying buffer so I think I can manage that :?

  6. devsnek commented on Oct 5, 2018

    @devsnek
    Member

    it's also not a feature that all js engines might support as it's not a spec operation, so putting it in napi doesn't make much sense.

  7. addaleax commented on Oct 5, 2018

    @addaleax
    Member

    @orange4glace How does your module know when all references to it from all threads are gone?

  8. orange4glace commented on Oct 5, 2018

    @orange4glace
    Author

    @addaleax My plan is not accessing it after free it like manner of c pointer. Does it also matter?

  9. orange4glace commented on Oct 5, 2018

    @orange4glace
    Author

    @devsnek It's true but it is also true that SharedArrayBuffer is a part of ECMA 2017 standard.

  10. gabrielschulhof commented on Oct 5, 2018

    @gabrielschulhof
    Contributor

    The commit message for #23279 should say that it fixes this issue, and this issue should be open.

  11. huningxin commented on Sep 2, 2021

    @huningxin

    This is useful for an addon that needs to access Wasm shared memory.

    For instance, the Web Neural Network (WebNN) node.js addon exchange tensor data with ML frameworks (e.g. TensorFlow.js Wasm backend and ONNXRuntime Wasm) through Wasm memory. This issue prevents WebNN node.js addon to work with multi-threaded build of these frameworks which use SharedArrayBuffer in Electron.js (webmachinelearning/webnn-native#106).

  12. dygabo commented on Mar 22, 2023

    @dygabo
    Member

    This would be also useful for shared buffers that get updated with Atomic.exchange as Napi::ArrayBuffer seems to not be enough. At least wit node v14.18.0 Atomics.exchange returns: [object BigUint64Array] is not an integer shared typed array.
    Atomics.exchange on a value in an ArrayBuffer seems to works fine with node v18.

    Are there any plans of supporting SharedArrayBuffer in N-API?

  13. 5 remaining items

  14. mhdawson commented on Dec 23, 2024

    @mhdawson
    Member

    @nomagick what we need is somebody to work on a new PR since the original one was closed. Is that something you would volunteer to do?

  15. nomagick commented on Dec 24, 2024

    @nomagick

    what we need is somebody to work on a new PR since the original one was closed. Is that something you would volunteer to do?

    @mhdawson Yes exactly. Thanks.

  16. github-actions commented on Jun 23, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  17. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 23, 2025
  18. RobinWuu commented on Jun 25, 2025

    @RobinWuu

    I'm curious about the current status of this feature.
    I'm also interested in how to ensure that the finalize callback is triggered only after all shared arraybuffers in all threads have been garbage-collected.
    Additionally, is it possible to support this feature in QuickJS and JavaScriptCore?

  19. mhdawson commented on Jun 25, 2025

    @mhdawson
    Member

    @RobinWuu I don't think there has been any progress.

  20. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 26, 2025
  21. moved this from Need Triage to Todo in Node-API Team Projecton Jul 11, 2025
  22. mertcanaltin commented on Jul 14, 2025

    @mertcanaltin
    Member

    Hello, I did a study for this place and have a one question in this pr : #59071 (comment)

  23. moved this from Todo to Has PR in Node-API Team Projecton Jul 25, 2025
  24. mertcanaltin commented on Sep 16, 2025

    @mertcanaltin
    Member

    merged 🎉 , thanks all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.node-apiIssues and PRs related to Node-API.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions