Skip to content

repl,worker: fix crash when SharedArrayBuffer disabled #39718

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions benchmark/worker/atomics-wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const bench = common.createBenchmark(main, {
});

function main({ n }) {
if (typeof SharedArrayBuffer === 'undefined') {
throw new Error('SharedArrayBuffers must be enabled to run this benchmark');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main is going to be executed more than once. As such, it's probably best to move the check to the top of the file.


const i32arr = new Int32Array(new SharedArrayBuffer(4));
bench.start();
for (let i = 0; i < n; i++)
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
ArrayPrototypeSplice,
ObjectDefineProperty,
PromisePrototypeCatch,
globalThis: { Atomics },
globalThis: { Atomics, SharedArrayBuffer },
} = primordials;

const {
Expand Down Expand Up @@ -142,6 +142,9 @@ port.on('message', (message) => {
const originalCwd = process.cwd;

process.cwd = function() {
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
if (typeof SharedArrayBuffer === 'undefined') return originalCwd();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine but we could prevent replacing process.cwd in the first place, so that the extra check is not needed when calling process.cwd().


const currentCounter = Atomics.load(cwdCounter, 0);
if (currentCounter === lastCounter)
return cachedCwd;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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
17 changes: 17 additions & 0 deletions test/parallel/test-worker-no-sab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Flags: --no-harmony-sharedarraybuffer

'use strict';

const common = require('../common');
const assert = require('assert');
const { isMainThread, Worker } = require('worker_threads');

// Regression test for https://github.com/nodejs/node/issues/39717.

const w = new Worker(__filename);

w.on('exit', common.mustCall((status) => {
assert.strictEqual(status, 2);
}));

if (!isMainThread) process.exit(2);
Comment on lines +7 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { isMainThread, Worker } = require('worker_threads');
// Regression test for https://github.com/nodejs/node/issues/39717.
const w = new Worker(__filename);
w.on('exit', common.mustCall((status) => {
assert.strictEqual(status, 2);
}));
if (!isMainThread) process.exit(2);
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);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codebytere Are you ok if we commit this suggestion, run tests, check with @BridgeAR if they can then approve the PR,, and hopefully land? Or is there something about this suggestion that would make you reluctant to do that?