-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: fix crash when SharedArrayBuffer outlives creating thread
Keep a reference to the `ArrayBuffer::Allocator` alive for at least as long as a `SharedArrayBuffer` allocated by it lives. Refs: #28788 Fixes: #28777 Fixes: #28773 PR-URL: #29190 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
- Loading branch information
Showing
6 changed files
with
83 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
// Make sure that allocating uninitialized ArrayBuffers in one thread does not | ||
// affect the zero-initialization in other threads. | ||
|
||
const w = new Worker(` | ||
const { parentPort } = require('worker_threads'); | ||
function post() { | ||
const uint32array = new Uint32Array(64); | ||
parentPort.postMessage(uint32array.reduce((a, b) => a + b)); | ||
} | ||
setInterval(post, 0); | ||
`, { eval: true }); | ||
|
||
function allocBuffers() { | ||
Buffer.allocUnsafe(32 * 1024 * 1024); | ||
} | ||
|
||
const interval = setInterval(allocBuffers, 0); | ||
|
||
let messages = 0; | ||
w.on('message', (sum) => { | ||
assert.strictEqual(sum, 0); | ||
if (messages++ === 100) { | ||
clearInterval(interval); | ||
w.terminate(); | ||
} | ||
}); |
22 changes: 22 additions & 0 deletions
22
test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
// Regression test for https://github.com/nodejs/node/issues/28777 | ||
// Make sure that SharedArrayBuffers created in Worker threads are accessible | ||
// after the creating thread ended. | ||
|
||
const w = new Worker(` | ||
const { parentPort } = require('worker_threads'); | ||
const sharedArrayBuffer = new SharedArrayBuffer(4); | ||
parentPort.postMessage(sharedArrayBuffer); | ||
`, { eval: true }); | ||
|
||
let sharedArrayBuffer; | ||
w.once('message', common.mustCall((message) => sharedArrayBuffer = message)); | ||
w.once('exit', common.mustCall(() => { | ||
const uint8array = new Uint8Array(sharedArrayBuffer); | ||
uint8array[0] = 42; | ||
assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0])); | ||
})); |