lib: do not crash using workers with disabled shared array buffers

This allows the repl to function normally while using the
`--no-harmony-sharedarraybuffer` V8 flag.
It also fixes using workers while using the
`--no-harmony-atomics` V8 flag.

Fixes: https://github.com/nodejs/node/issues/39717

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/41023
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Ruben BridgewaterandShelley Vohr authored and Myles Borins committed 2023-02-20 14:16:31 -05:00
1 parent 164bfe82cc
commit 4aebee63f0
5 files changed
+72 -16

No files matched your search

+8 -1
View File
@@ -1,5 +1,12 @@
'use strict';
/* global SharedArrayBuffer */
if (typeof SharedArrayBuffer === 'undefined') {
throw new Error('SharedArrayBuffers must be enabled to run this benchmark');
}
if (typeof Atomics === 'undefined') {
throw new Error('Atomics must be enabled to run this benchmark');
}
const common = require('../common.js');
const bench = common.createBenchmark(main, {
+19 -14
View File
@@ -10,7 +10,10 @@ const {
ObjectDefineProperty,
PromisePrototypeThen,
RegExpPrototypeExec,
globalThis: { Atomics },
globalThis: {
Atomics,
SharedArrayBuffer,
},
} = primordials;
const {
@@ -106,21 +109,23 @@ port.on('message', (message) => {
require('internal/worker').assignEnvironmentData(environmentData);
// The counter is only passed to the workers created by the main thread, not
// to workers created by other workers.
let cachedCwd = '';
let lastCounter = -1;
const originalCwd = process.cwd;
if (SharedArrayBuffer !== undefined && Atomics !== undefined) {
// The counter is only passed to the workers created by the main thread,
// not to workers created by other workers.
let cachedCwd = '';
let lastCounter = -1;
const originalCwd = process.cwd;
process.cwd = function() {
const currentCounter = Atomics.load(cwdCounter, 0);
if (currentCounter === lastCounter)
process.cwd = function() {
const currentCounter = Atomics.load(cwdCounter, 0);
if (currentCounter === lastCounter)
return cachedCwd;
lastCounter = currentCounter;
cachedCwd = originalCwd();
return cachedCwd;
lastCounter = currentCounter;
cachedCwd = originalCwd();
return cachedCwd;
};
workerIo.sharedCwdCounter = cwdCounter;
};
workerIo.sharedCwdCounter = cwdCounter;
}
if (manifestSrc) {
require('internal/process/policy').setup(manifestSrc, manifestURL);
+3 -1
View File
@@ -95,7 +95,9 @@ let cwdCounter;
const environmentData = new SafeMap();
if (isMainThread) {
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
// Atomics can be disabled with --no-harmony-atomics.
if (isMainThread && SharedArrayBuffer !== undefined && Atomics !== undefined) {
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
const originalChdir = process.chdir;
process.chdir = function(path) {
+21
View File
@@ -0,0 +1,21 @@
// Flags: --no-harmony-atomics
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
// Regression test for https://github.com/nodejs/node/issues/39717.
// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
const w = new Worker(__filename);
w.on('exit', common.mustCall((status) => {
assert.strictEqual(status, 2);
}));
} else {
process.exit(2);
}
+21
View File
@@ -0,0 +1,21 @@
// Flags: --no-harmony-sharedarraybuffer
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
// Regression test for https://github.com/nodejs/node/issues/39717.
// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
const w = new Worker(__filename);
w.on('exit', common.mustCall((status) => {
assert.strictEqual(status, 2);
}));
} else {
process.exit(2);
}