-
- Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Conversation
BridgeAR left a comment
There was a problem hiding this 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:
node/lib/internal/main/worker_thread.js
Lines 138 to 152 in 82b1b55
| // 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; | |
| }; | |
| 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).
| Is it repl or worker (the subsystem)? |
| @targos i guess sort of both? I can make it whatever you think would be preferable, but it'd be hit anytime anyone invoked |
Trott left a comment
There was a problem hiding this 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).
@codebytere |
56f1396 to 51830bb Compare | @BridgeAR i think that should do it - let me know if you had something else in mind tho! |
BridgeAR left a comment
There was a problem hiding this 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.
6e29e56 to 489fa03 Compare 489fa03 to 7c5a3e0 Compare 7c5a3e0 to 324adc2 Compare | There's a relevant failure in CI. The test added here fails when invoked with |
| @Trott are you potentially able to replicate locally? i'm seeing passing when i run locally 🤔 |
324adc2 to 9ff558a Compare
@codebytere That's not passing. The The lack of output, though, is odd.... |
The lack of output is legit. (Maybe we should update To replicate without the Python script, run this |
BridgeAR left a comment
There was a problem hiding this 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.
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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); | |
| } |
There was a problem hiding this comment.
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 (typeof SharedArrayBuffer === 'undefined') return originalCwd(); |
There was a problem hiding this comment.
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().
| function main({ n }) { | ||
| if (typeof SharedArrayBuffer === 'undefined') { | ||
| throw new Error('SharedArrayBuffers must be enabled to run this benchmark'); | ||
| } |
There was a problem hiding this comment.
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.
| Closing as superseded by #41023 |
Closes #39717.
It's possible for SharedArrayBuffers to be disabled with
--no-harmony-sharedarraybufferso we first need to check that this isn't the case before attempting to use them in the repl or the following crash occurs: