-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
process,worker: fix process.exitCode handling for fatalException #21739
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
18e6b2c
process: fix process.exitCode handling for fatalException
lundibundi 4df2d6b
test: refactor process/worker exitCode tests
lundibundi 7ee6da0
fixup! worker: remove setting exitCode after uncaughtException/exit
lundibundi 6a993f1
worker: exit after uncaught exception
lundibundi 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
There are no files selected for viewing
This file contains 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 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 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 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 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,113 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
const cases = []; | ||
module.exports = cases; | ||
|
||
function exitsOnExitCodeSet() { | ||
process.exitCode = 42; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 42); | ||
assert.strictEqual(code, 42); | ||
}); | ||
} | ||
cases.push({ func: exitsOnExitCodeSet, result: 42 }); | ||
|
||
function changesCodeViaExit() { | ||
process.exitCode = 99; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 42); | ||
assert.strictEqual(code, 42); | ||
}); | ||
process.exit(42); | ||
} | ||
cases.push({ func: changesCodeViaExit, result: 42 }); | ||
|
||
function changesCodeZeroExit() { | ||
process.exitCode = 99; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 0); | ||
assert.strictEqual(code, 0); | ||
}); | ||
process.exit(0); | ||
} | ||
cases.push({ func: changesCodeZeroExit, result: 0 }); | ||
|
||
function exitWithOneOnUncaught() { | ||
process.exitCode = 99; | ||
process.on('exit', (code) => { | ||
// cannot use assert because it will be uncaughtException -> 1 exit code | ||
// that will render this test useless | ||
if (code !== 1 || process.exitCode !== 1) { | ||
console.log('wrong code! expected 1 for uncaughtException'); | ||
process.exit(99); | ||
} | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ | ||
func: exitWithOneOnUncaught, | ||
result: 1, | ||
error: /^Error: ok$/, | ||
}); | ||
|
||
function changeCodeInsideExit() { | ||
process.exitCode = 95; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 95); | ||
assert.strictEqual(code, 95); | ||
process.exitCode = 99; | ||
}); | ||
} | ||
cases.push({ func: changeCodeInsideExit, result: 99 }); | ||
|
||
function zeroExitWithUncaughtHandler() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 0); | ||
assert.strictEqual(code, 0); | ||
}); | ||
process.on('uncaughtException', () => {}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ func: zeroExitWithUncaughtHandler, result: 0 }); | ||
|
||
function changeCodeInUncaughtHandler() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 97); | ||
assert.strictEqual(code, 97); | ||
}); | ||
process.on('uncaughtException', () => { | ||
process.exitCode = 97; | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ func: changeCodeInUncaughtHandler, result: 97 }); | ||
|
||
function changeCodeInExitWithUncaught() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 1); | ||
assert.strictEqual(code, 1); | ||
process.exitCode = 98; | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ | ||
func: changeCodeInExitWithUncaught, | ||
result: 98, | ||
error: /^Error: ok$/, | ||
}); | ||
|
||
function exitWithZeroInExitWithUncaught() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 1); | ||
assert.strictEqual(code, 1); | ||
process.exitCode = 0; | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ | ||
func: exitWithZeroInExitWithUncaught, | ||
result: 0, | ||
error: /^Error: ok$/, | ||
}); |
This file contains 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
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.
If user code happens to set
process.exitCode
prior to this, this will override the user provided value. It should likely only set the value if it is not already setThere 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.
I do believe this is not the case, as uncaughtException has its own code 1, so it should be correct to override whatever was the code before. Though this is probably a semver-major because of this, but that shouldn't be a problem imo.
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.
hmm... I can definitely see the logic on it but I definitely don't like overriding user provided values unexpectedly. If we go with this, can I ask that a note be added to the documentation along with a code comment indicating why it's ok to silently override any user provided value here?
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.
That's fine with me. But where should I put a documentation?
Also, it is already noted that unhandledException has code 1, so this will basically enforce this, so that any previously set code will not be used, only the ones set in 'unhandledException' callback, 'exit' callback etc.
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.
In the description for the
uncaughtException
event indocs/api/process.md
. The documentation currently does not say anything about the process exit code. A note there that, should the event not be handled, the process will exit with exitCode = 1 even if the user had previously set aprocess.exitCode
value.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.
(btw, I see this as a limitation in the current documentation and not something that is introduced by this PR)
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.
Oh, I thought that's it's a given that if some new error happen exitCode will be replaced with the value of the error and just thought that it was undocumented. The fact that you wasn't able to change the code is indeed a strange thing. Anyway, I'll add the doc soon.