-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
Buffer is not transferable in Node 21/22 #55593
Comments
Note that
|
Does this also occur in v23? |
Yes. |
I did a quick manual bisect using the nightly builds. Diff: af9b48a...a90a145 Most likely caused by #47604 |
Some additional context, if you replace this line...
Also (executed right after
Does anybody know what's the plan, to make |
There is an internal Buffer pool that can not be transfered. Buffers allocated in this pool are not transferable, as documented at https://nodejs.org/api/worker_threads.html#workermarkasuntransferableobject. #47604 makes the "transfer" intent explicit that if the transfer can not be achieved, an error is thrown, according to the spec. The transfer operation was never completed without the change, instead, an implicit "copy" operation was performed. I think this difference can be documented at https://nodejs.org/api/buffer.html#buffers-and-typedarrays as well. |
Although the new behavior makes sense, it does make using buffers with worker threads less ergonomic. When you allocate a I've encountered the effects of this change in 101arrowz/fflate#227. The solution here seems to be to have my library retry the |
|
Thanks, that's helpful! Are you familiar with any runtime agnostic way to detect transferability beyond just trying it and failing if it doesn't work? I think there's |
|
OK, MDN is just out of date then; might be worth bringing up with them. I'll just use |
This is a pretty painful change. In v20 you can create a buffer and transfer it between workers, but in v22 it fails. Using |
As I understand the behavior before wasn't actually transferring the data anyway, it was just silently cloning it even if it were placed in the |
That's quite interesting! I didn't realise that was the behaviour. When I added it to the transferable I assumed I was being performant 😅 Well then is there any way to get binary data from NodeJS builtin APIs as a For context my usecase is that I have a server of sorts which is receiving binary data via stdin and I forward the data to one of the worker threads for handling - so being able to pass the bytes from stdin to whichever worker is free without copying would be ideal to improve throughput. |
Buffers from stdin are capable of being transfered. Only buffers created with |
Version
v21.7.3, v22.11.0
Platform
Subsystem
worker_threads
What steps will reproduce the bug?
This worked in v20 and fails in v21 and v22.
How often does it reproduce? Is there a required condition?
It always reproduces.
What is the expected behavior? Why is that the expected behavior?
The buffer should be transferred through the message channel.
What do you see instead?
Additional information
I didn't see anything in the v21 or v22 changelogs about buffers becoming untransferable, and transferring it worked in v20, so I'm assuming this is unintentional. If it's intentional, I would have expected at least a deprecation message indicating that this was going to start breaking.
The text was updated successfully, but these errors were encountered: