Skip to content

Commit

Permalink
lib: do not crash using workers with disabled shared array buffers
Browse files Browse the repository at this point in the history
This allows the repl to function normally while using the
`--no-harmony-sharedarraybuffer` V8 flag.

Fixes: #39717

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
BridgeAR committed Dec 4, 2021
1 parent 265a47d commit a39f548
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
4 changes: 4 additions & 0 deletions benchmark/worker/atomics-wait.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';
/* global SharedArrayBuffer */

if (typeof SharedArrayBuffer === 'undefined') {
throw new Error('SharedArrayBuffers must be enabled to run this benchmark');
}

const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [1e7]
Expand Down
35 changes: 20 additions & 15 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const {
ArrayPrototypeSplice,
ObjectDefineProperty,
PromisePrototypeCatch,
globalThis: { Atomics },
globalThis: {
Atomics,
SharedArrayBuffer
},
} = primordials;

const {
Expand Down Expand Up @@ -135,21 +138,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;

process.cwd = function() {
const currentCounter = Atomics.load(cwdCounter, 0);
if (currentCounter === lastCounter)
if (typeof SharedArrayBuffer !== '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)
return cachedCwd;
lastCounter = currentCounter;
cachedCwd = originalCwd();
return cachedCwd;
lastCounter = currentCounter;
cachedCwd = originalCwd();
return cachedCwd;
};
workerIo.sharedCwdCounter = cwdCounter;
};
workerIo.sharedCwdCounter = cwdCounter;
}

const CJSLoader = require('internal/modules/cjs/loader');
assert(!CJSLoader.hasLoadedAnyUserCJSModule);
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ let cwdCounter;

const environmentData = new SafeMap();

if (isMainThread) {
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
if (isMainThread && typeof SharedArrayBuffer !== 'undefined') {
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
const originalChdir = process.chdir;
process.chdir = function(path) {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-worker-no-sab.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit a39f548

Please sign in to comment.