Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion benchmark/worker/atomics-wait.js
Original file line number Diff line number Diff line change
@@ -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, {
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 @@ -10,7 +10,10 @@ const {
ObjectDefineProperty,
PromisePrototypeThen,
RegExpPrototypeExec,
globalThis: { Atomics },
globalThis: {
Atomics,
SharedArrayBuffer,
},
} = primordials;

const {
Expand Down Expand Up @@ -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;

process.cwd = function() {
const currentCounter = Atomics.load(cwdCounter, 0);
if (currentCounter === lastCounter)
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)
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);
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-worker-no-atomics.js
Original file line number Diff line number Diff line change
@@ -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 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);
}