Skip to content

Commit 46a22ab

Browse files
BridgeARcodebytere
authored andcommitted
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. It also fixes using workers while using the `--no-harmony-atomics` V8 flag. Fixes: #39717 Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #41023 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 847d740 commit 46a22ab

5 files changed

Lines changed: 73 additions & 17 deletions

File tree

‎benchmark/worker/atomics-wait.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
'use strict';
2-
/* global SharedArrayBuffer */
2+
3+
if(typeofSharedArrayBuffer==='undefined'){
4+
thrownewError('SharedArrayBuffers must be enabled to run this benchmark');
5+
}
6+
7+
if(typeofAtomics==='undefined'){
8+
thrownewError('Atomics must be enabled to run this benchmark');
9+
}
310

411
constcommon=require('../common.js');
512
constbench=common.createBenchmark(main,{

‎lib/internal/main/worker_thread.js‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const {
1010
ObjectDefineProperty,
1111
PromisePrototypeThen,
1212
RegExpPrototypeExec,
13-
globalThis: { Atomics },
13+
globalThis: {
14+
Atomics,
15+
SharedArrayBuffer,
16+
},
1417
}=primordials;
1518

1619
const{
@@ -105,21 +108,23 @@ port.on('message', (message) => {
105108

106109
require('internal/worker').assignEnvironmentData(environmentData);
107110

108-
// The counter is only passed to the workers created by the main thread, not
109-
// to workers created by other workers.
110-
letcachedCwd='';
111-
letlastCounter=-1;
112-
constoriginalCwd=process.cwd;
113-
114-
process.cwd=function(){
115-
constcurrentCounter=Atomics.load(cwdCounter,0);
116-
if(currentCounter===lastCounter)
111+
if(SharedArrayBuffer!==undefined&&Atomics!==undefined){
112+
// The counter is only passed to the workers created by the main thread,
113+
// not to workers created by other workers.
114+
letcachedCwd='';
115+
letlastCounter=-1;
116+
constoriginalCwd=process.cwd;
117+
118+
process.cwd=function(){
119+
constcurrentCounter=Atomics.load(cwdCounter,0);
120+
if(currentCounter===lastCounter)
121+
returncachedCwd;
122+
lastCounter=currentCounter;
123+
cachedCwd=originalCwd();
117124
returncachedCwd;
118-
lastCounter=currentCounter;
119-
cachedCwd=originalCwd();
120-
returncachedCwd;
121-
};
122-
workerIo.sharedCwdCounter=cwdCounter;
125+
};
126+
workerIo.sharedCwdCounter=cwdCounter;
127+
}
123128

124129
if(manifestSrc){
125130
require('internal/process/policy').setup(manifestSrc,manifestURL);

‎lib/internal/worker.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ let cwdCounter;
9191

9292
constenvironmentData=newSafeMap();
9393

94-
if(isMainThread){
94+
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
95+
// Atomics can be disabled with --no-harmony-atomics.
96+
if(isMainThread&&SharedArrayBuffer!==undefined&&Atomics!==undefined){
9597
cwdCounter=newUint32Array(newSharedArrayBuffer(4));
9698
constoriginalChdir=process.chdir;
9799
process.chdir=function(path){
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Flags: --no-harmony-atomics
2+
3+
'use strict';
4+
5+
constcommon=require('../common');
6+
constassert=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+
constw=newWorker(__filename);
15+
16+
w.on('exit',common.mustCall((status)=>{
17+
assert.strictEqual(status,2);
18+
}));
19+
}else{
20+
process.exit(2);
21+
}
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+
constcommon=require('../common');
6+
constassert=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+
constw=newWorker(__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)