-
-
Notifications
You must be signed in to change notification settings - Fork 35k
process: refactor bootstrap of worker/main thread stdio, fatalException, and script evaluation #25199
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
Closed
process: refactor bootstrap of worker/main thread stdio, fatalException, and script evaluation #25199
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
process: move eval and exception bootstrap ito process/execution.js
This patch: - Moves `tryGetCwd`, `evalScript` and `fatalException` from `bootstrap/node.js` into `process/execution.js` so that they do have to be passed into the worker thread setup function, instead the worker code can require them when necessary. - Moves `setUncaughtExceptionCaptureCallback` and `hasUncaughtExceptionCaptureCallback` along with the two global state `exceptionHandlerState` and `shouldAbortOnUncaughtToggle` info `process.execution.js` as those are only used by the fatalException and these two accessors as one self-contained unit.
- Loading branch information
commit bcd78cd998f46033efb12c47a37a142025270d7f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
|
|
||
| const { | ||
| codes: { | ||
| ERR_INVALID_ARG_TYPE, | ||
| ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET | ||
| } | ||
| } = require('internal/errors'); | ||
|
|
||
| const { | ||
| executionAsyncId, | ||
| clearDefaultTriggerAsyncId, | ||
| clearAsyncIdStack, | ||
| hasAsyncIdStack, | ||
| afterHooksExist, | ||
| emitAfter | ||
| } = require('internal/async_hooks'); | ||
|
|
||
| // shouldAbortOnUncaughtToggle is a typed array for faster | ||
| // communication with JS. | ||
| const { shouldAbortOnUncaughtToggle } = internalBinding('util'); | ||
|
|
||
| function tryGetCwd() { | ||
| try { | ||
| return process.cwd(); | ||
| } catch { | ||
| // getcwd(3) can fail if the current working directory has been deleted. | ||
| // Fall back to the directory name of the (absolute) executable path. | ||
| // It's not really correct but what are the alternatives? | ||
| return path.dirname(process.execPath); | ||
| } | ||
| } | ||
|
|
||
| function evalScript(name, body, breakFristLine) { | ||
| const CJSModule = require('internal/modules/cjs/loader'); | ||
| if (breakFristLine) { | ||
| const fn = `function() {\n\n${body};\n\n}`; | ||
| body = `process.binding('inspector').callAndPauseOnStart(${fn}, {})`; | ||
| } | ||
|
|
||
| const cwd = tryGetCwd(); | ||
|
|
||
| const module = new CJSModule(name); | ||
| module.filename = path.join(cwd, name); | ||
| module.paths = CJSModule._nodeModulePaths(cwd); | ||
| const script = `global.__filename = ${JSON.stringify(name)};\n` + | ||
| 'global.exports = exports;\n' + | ||
| 'global.module = module;\n' + | ||
| 'global.__dirname = __dirname;\n' + | ||
| 'global.require = require;\n' + | ||
| 'return require("vm").runInThisContext(' + | ||
| `${JSON.stringify(body)}, { filename: ` + | ||
| `${JSON.stringify(name)}, displayErrors: true });\n`; | ||
| const result = module._compile(script, `${name}-wrapper`); | ||
| if (require('internal/options').getOptionValue('--print')) { | ||
| console.log(result); | ||
| } | ||
| // Handle any nextTicks added in the first tick of the program. | ||
| process._tickCallback(); | ||
| } | ||
|
|
||
| const exceptionHandlerState = { captureFn: null }; | ||
|
|
||
| function setUncaughtExceptionCaptureCallback(fn) { | ||
| if (fn === null) { | ||
| exceptionHandlerState.captureFn = fn; | ||
| shouldAbortOnUncaughtToggle[0] = 1; | ||
| return; | ||
| } | ||
| if (typeof fn !== 'function') { | ||
| throw new ERR_INVALID_ARG_TYPE('fn', ['Function', 'null'], fn); | ||
| } | ||
| if (exceptionHandlerState.captureFn !== null) { | ||
| throw new ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET(); | ||
| } | ||
| exceptionHandlerState.captureFn = fn; | ||
| shouldAbortOnUncaughtToggle[0] = 0; | ||
| } | ||
|
|
||
| function hasUncaughtExceptionCaptureCallback() { | ||
| return exceptionHandlerState.captureFn !== null; | ||
| } | ||
|
|
||
| function noop() {} | ||
|
|
||
| // XXX(joyeecheung): for some reason this cannot be defined at the top-level | ||
| // and exported to be written to process._fatalException, it has to be | ||
| // returned as an *anonymous function* wrapped inside a factory function, | ||
| // otherwise it breaks the test-timers.setInterval async hooks test - | ||
| // this may indicate that node::FatalException should fix up the callback scope | ||
| // before calling into process._fatalException, or this function should | ||
| // take extra care of the async hooks before it schedules a setImmediate. | ||
| function createFatalException() { | ||
| return (er) => { | ||
| // It's possible that defaultTriggerAsyncId was set for a constructor | ||
| // call that threw and was never cleared. So clear it now. | ||
| clearDefaultTriggerAsyncId(); | ||
|
|
||
| if (exceptionHandlerState.captureFn !== null) { | ||
| exceptionHandlerState.captureFn(er); | ||
| } else if (!process.emit('uncaughtException', er)) { | ||
| // If someone handled it, then great. otherwise, die in C++ land | ||
| // since that means that we'll exit the process, emit the 'exit' event. | ||
| try { | ||
| if (!process._exiting) { | ||
| process._exiting = true; | ||
| process.exitCode = 1; | ||
| process.emit('exit', 1); | ||
| } | ||
| } catch { | ||
| // Nothing to be done about it at this point. | ||
| } | ||
| try { | ||
| const { kExpandStackSymbol } = require('internal/util'); | ||
| if (typeof er[kExpandStackSymbol] === 'function') | ||
| er[kExpandStackSymbol](); | ||
| } catch { | ||
| // Nothing to be done about it at this point. | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // If we handled an error, then make sure any ticks get processed | ||
| // by ensuring that the next Immediate cycle isn't empty. | ||
| require('timers').setImmediate(noop); | ||
|
|
||
| // Emit the after() hooks now that the exception has been handled. | ||
| if (afterHooksExist()) { | ||
| do { | ||
| emitAfter(executionAsyncId()); | ||
| } while (hasAsyncIdStack()); | ||
| // Or completely empty the id stack. | ||
| } else { | ||
| clearAsyncIdStack(); | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| tryGetCwd, | ||
| evalScript, | ||
| fatalException: createFatalException(), | ||
| setUncaughtExceptionCaptureCallback, | ||
| hasUncaughtExceptionCaptureCallback | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Fun fact: you can fail the test on master with this diff