Skip to content

Commit 73059c2

Browse files
committed
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. Fixes: #39717 Co-authored-by: Shelley Vohr <[email protected]> Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent 265a47d commit 73059c2

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

benchmark/worker/atomics-wait.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22
/* global SharedArrayBuffer */
33

4+
if (typeof SharedArrayBuffer === 'undefined') {
5+
throw new Error('SharedArrayBuffers must be enabled to run this benchmark');
6+
}
7+
48
const common = require('../common.js');
59
const bench = common.createBenchmark(main, {
610
n: [1e7]

lib/internal/main/worker_thread.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
6262

6363
const assert = require('internal/assert');
6464

65+
const { SharedArrayBuffer } = globalThis;
66+
6567
patchProcessObject();
6668
setupInspectorHooks();
6769
setupDebugEnv();
@@ -135,21 +137,23 @@ port.on('message', (message) => {
135137

136138
require('internal/worker').assignEnvironmentData(environmentData);
137139

138-
// The counter is only passed to the workers created by the main thread, not
139-
// to workers created by other workers.
140-
let cachedCwd = '';
141-
let lastCounter = -1;
142-
const originalCwd = process.cwd;
143-
144-
process.cwd = function() {
145-
const currentCounter = Atomics.load(cwdCounter, 0);
146-
if (currentCounter === lastCounter)
140+
if (typeof SharedArrayBuffer !== 'undefined') {
141+
// The counter is only passed to the workers created by the main thread,
142+
// not to workers created by other workers.
143+
let cachedCwd = '';
144+
let lastCounter = -1;
145+
const originalCwd = process.cwd;
146+
147+
process.cwd = function() {
148+
const currentCounter = Atomics.load(cwdCounter, 0);
149+
if (currentCounter === lastCounter)
150+
return cachedCwd;
151+
lastCounter = currentCounter;
152+
cachedCwd = originalCwd();
147153
return cachedCwd;
148-
lastCounter = currentCounter;
149-
cachedCwd = originalCwd();
150-
return cachedCwd;
151-
};
152-
workerIo.sharedCwdCounter = cwdCounter;
154+
};
155+
workerIo.sharedCwdCounter = cwdCounter;
156+
}
153157

154158
const CJSLoader = require('internal/modules/cjs/loader');
155159
assert(!CJSLoader.hasLoadedAnyUserCJSModule);

lib/internal/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ let cwdCounter;
9090

9191
const environmentData = new SafeMap();
9292

93-
if (isMainThread) {
93+
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
94+
if (isMainThread && typeof SharedArrayBuffer !== 'undefined') {
9495
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
9596
const originalChdir = process.chdir;
9697
process.chdir = function(path) {

test/parallel/test-worker-no-sab.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Flags: --no-harmony-sharedarraybuffer
2+
3+
'use strict';
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const { Worker } = require('worker_threads');
8+
9+
// Regression test for https://github.com/nodejs/node/issues/39717.
10+
11+
// Do not use isMainThread so that this test itself can be run inside a Worker.
12+
if (!process.env.HAS_STARTED_WORKER) {
13+
process.env.HAS_STARTED_WORKER = 1;
14+
const w = new Worker(__filename);
15+
16+
w.on('exit', common.mustCall((status) => {
17+
assert.strictEqual(status, 2);
18+
}));
19+
} else {
20+
process.exit(2);
21+
}

0 commit comments

Comments
 (0)