-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Segfault with unref on a worker with ArrayBuffer in transferList
#33263
Comments
I can also reproduce this when cloning the reproduction repository with Node 14.1.0
It works with Node 12 for me. |
Thank you so much for the report and code recreation. |
I'm not completely sure, but may be related with The new V8 ArrayBuffer API landed on Node v14.0. |
cc @nodejs/buffer |
So this is related to this issue: #33240 and this PR #33252 Specifically, you're attempting to transfer an In your worker... something like: const fs = require('fs')
const crypto = require('crypto')
const { parentPort } = require('worker_threads')
parentPort.on('message', (message) => {
const hasher = crypto.createHash('sha256')
fs.createReadStream('example.txt')
.pipe(hasher)
.on('finish', () => {
const { buffer } = hasher.read()
const buf = new Uint8Array(buffer); // Create a copy
parentPort.postMessage({ value: buf }, [buf.buffer])
})
}) Alternatively, it's not clear from this example why you are using hasher.read() at all. The example is definitely not a typical case. What I would imagine would be a better approach in general is something like... const fs = require('fs')
const crypto = require('crypto')
const { parentPort } = require('worker_threads')
const { pipeline } = require('stream')
parentPort.on('message', (message) => {
const input = fs.createReadStream('example.txt')
const hasher = crypto.createHash('sha256')
pipeline(input, hasher, (err) => {
if (err) {
// handle the error appropriately
return;
}
// Pass a hex of the hash rather than the buffer
parentPort.postMessage({ value: hasher.digest().toString('hex')});
});
}) |
Now... all that said... just as a more general point that is independent of the segfault issue that we really need to make sure we look at... given that read stream and the hash operations here are already async, you're not likely to see any real benefit from using a worker thread in this way (see https://github.com/jasnell/piscina/tree/master/examples/server for an example perf analysis). Specifically, the performance of the worker thread in this specific example will never be faster than just doing the same operations on the main thread. |
Ah, yes... I'll open an issue there |
I'm now unable to reproduce this issue on 14.x and 15.x. I believe the issue has been resolved tho I'm not sure exactly which commit fixed it. Closing, can reopen if it's still an issue |
What steps will reproduce the bug?
When communicating a Uint8 Array Buffer from a worker to the parent process with
postMessage
, which is included in thetransferList
argument and then callingunref
on the worker, I get a Segfault:'node index.js' terminated by signal SIGSEGV (Address boundary error)
.index.js
worker.js
Reproduction here: https://github.com/timsuchanek/segfault-node-14
lldb backtrace
This works fine in Node 13 or lower and it seems, that this bug was introduced in Node 14.
The text was updated successfully, but these errors were encountered: