node/test/parallel/test-worker-arraybuffer-zerofill.js
Anna Henningsen 119c4ccf0e
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: https://github.com/nodejs/node/pull/28788
Fixes: https://github.com/nodejs/node/issues/28777
Fixes: https://github.com/nodejs/node/issues/28773

PR-URL: https://github.com/nodejs/node/pull/29190
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2019-08-20 00:14:10 +02:00

34 lines
769 B
JavaScript

'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();
}
});