Skip to content

Conversation

@codebytere
Copy link
Member

Closes#39717.

It's possible for SharedArrayBuffers to be disabled with --no-harmony-sharedarraybuffer so we first need to check that this isn't the case before attempting to use them in the repl or the following crash occurs:

electron_node on git:a3d0cc7244 ❯ node --no-harmony-sharedarraybuffer 6:25PM Welcome to Node.js v16.2.0. Type ".help" for more information. > snode:internal/readline/emitKeypressEvents:71 throw err; ^ TypeError: SharedArrayBuffer is not a constructor at node:internal/worker:96:32 at NativeModule.compileForInternalLoader (node:internal/bootstrap/loaders:312:7) at nativeModuleRequire (node:internal/bootstrap/loaders:341:14) at node:worker_threads:11:5 at NativeModule.compileForInternalLoader (node:internal/bootstrap/loaders:312:7) at nativeModuleRequire (node:internal/bootstrap/loaders:341:14) at node:inspector:32:26 at NativeModule.compileForInternalLoader (node:internal/bootstrap/loaders:312:7) at nativeModuleRequire (node:internal/bootstrap/loaders:341:14) at sendInspectorCommand (node:internal/util/inspector:14:21) 

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support. labels Aug 9, 2021
@codebyterecodebytere added the repl Issues and PRs related to the REPL subsystem. label Aug 9, 2021
Copy link
Member

@BridgeARBridgeAR left a comment

Choose a reason for hiding this comment

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

This will likely break process.cwd() after changing the directory as it will return a cached value.
cwd() should also be protected in that case:

// The counter is only passed to the workers created by the main thread, not
// to workers created by other workers.
letcachedCwd='';
letlastCounter=-1;
constoriginalCwd=process.cwd;
process.cwd=function(){
constcurrentCounter=Atomics.load(cwdCounter,0);
if(currentCounter===lastCounter)
returncachedCwd;
lastCounter=currentCounter;
cachedCwd=originalCwd();
returncachedCwd;
};
workerIo.sharedCwdCounter=cwdCounter;

I am fine doing that, I just wonder if this is a common situation at all. AFAIK we mostly do not check all possible configurations and do not officially support them?

If we want to support the flag, we should also add a test (while such test can't really protect from other potential SharedArrayBuffer usages).

@codebytere
Copy link
MemberAuthor

codebytere commented Aug 9, 2021

@BridgeAR happy to add a test - the other reason i'm interested in this in particular is because it's disabled in the web (see here) unless cross origin isolation is enabled, so this crashes the renderer process in some cases in Electron as well.

@targos
Copy link
Member

targos commented Aug 9, 2021

Is it repl or worker (the subsystem)?

@codebytere
Copy link
MemberAuthor

@targos i guess sort of both? I can make it whatever you think would be preferable, but it'd be hit anytime anyone invoked repl.start() or when dealing with cwd in workers.

Copy link
Member

@TrottTrott left a comment

Choose a reason for hiding this comment

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

LGTM with lint fix and either a test or a comment (or both).

@targos
Copy link
Member

@targos i guess sort of both? I can make it whatever you think would be preferable, but it'd be hit anytime anyone invoked repl.start() or when dealing with cwd in workers.

@codebytererepl,worker: fix crash ... would work :)

@codebyterecodebytere changed the title repl: fix crash when SharedArrayBuffer disabledrepl,worker: fix crash when SharedArrayBuffer disabledAug 10, 2021
@codebytere
Copy link
MemberAuthor

@BridgeAR i think that should do it - let me know if you had something else in mind tho!

Copy link
Member

@BridgeARBridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM. Just left a comment how the test could be improved.

@codebyterecodebytereforce-pushed the fix-sab-crash branch 2 times, most recently from 6e29e56 to 489fa03CompareAugust 10, 2021 09:12
@codebyterecodebytere removed the needs-ci PRs that need a full CI run. label Aug 10, 2021
@nodejsnodejs deleted a comment from nodejs-github-botAug 10, 2021
@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member

There's a relevant failure in CI. The test added here fails when invoked with test/tools.py --worker.

@codebytere
Copy link
MemberAuthor

@Trott are you potentially able to replicate locally?

i'm seeing

node on git:fix-sab-crash ❯ tools/test.py --worker test/parallel/test-worker-no-sab === release test-worker-no-sab === Path: parallel/test-worker-no-sab Command: out/Release/node --no-harmony-sharedarraybuffer /Users/codebytere/Developer/node/tools/run-worker.js /Users/codebytere/Developer/node/test/parallel/test-worker-no-sab.js [00:00|% 100|+ 0|- 1]: Done 

passing when i run locally 🤔

@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member

Trott commented Sep 2, 2021

@Trott are you potentially able to replicate locally?

i'm seeing

node on git:fix-sab-crash ❯ tools/test.py --worker test/parallel/test-worker-no-sab === release test-worker-no-sab === Path: parallel/test-worker-no-sab Command: out/Release/node --no-harmony-sharedarraybuffer /Users/codebytere/Developer/node/tools/run-worker.js /Users/codebytere/Developer/node/test/parallel/test-worker-no-sab.js [00:00|% 100|+ 0|- 1]: Done 

passing when i run locally 🤔

@codebytere That's not passing. The 1 would be in the + cell if it was passing, and the 0 would b e in the - cell. (You can see the difference if you run it again without --worker. Or you can try running it with --worker but run ttest/parallel/test-worker-no-s* to see one test pass and one test fail.)

The lack of output, though, is odd....

@Trott
Copy link
Member

Trott commented Sep 2, 2021

The lack of output, though, is odd....

The lack of output is legit. (Maybe we should update tools/run-worker.js to provide output in this situation?)

To replicate without the Python script, run this out/Release/node --no-harmony-sharedarraybuffer /Users/trott/io.js/tools/run-worker.js /Users/trott/io.js/test/parallel/test-worker-no-sab.js. It will return no output, but exit with an error code.

Copy link
Member

@BridgeARBridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM with my comments addressed.

Comment on lines +7 to +17
const{ isMainThread, Worker }=require('worker_threads');

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

constw=newWorker(__filename);

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

if(!isMainThread)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.

Suggested change
const{ isMainThread, Worker }=require('worker_threads');
// Regression test for https://github.com/nodejs/node/issues/39717.
constw=newWorker(__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;
constw=newWorker(__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?


process.cwd=function(){
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
if(typeofSharedArrayBuffer==='undefined')returnoriginalCwd();
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().

functionmain({ n }){
if(typeofSharedArrayBuffer==='undefined'){
thrownewError('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.

@codebytere
Copy link
MemberAuthor

Closing as superseded by #41023

@codebyterecodebytere deleted the fix-sab-crash branch September 13, 2022 22:31
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

replIssues and PRs related to the REPL subsystem.workerIssues and PRs related to Worker support.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repl crash when SharedArrayBuffers are disabled

8 participants

@codebytere@targos@nodejs-github-bot@Trott@jasnell@zcbenz@addaleax@BridgeAR