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: nodejs#39717

Co-authored-by: Shelley Vohr <[email protected]>

Signed-off-by: Ruben Bridgewater <[email protected]>
  • Loading branch information
BridgeAR committed Dec 4, 2021
1 parent 265a47d commit f4ae17d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 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
30 changes: 16 additions & 14 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,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 f4ae17d

Please sign in to comment.