-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
n-api: Handle fatal exception in async callback #12838
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const test_async = require(`./build/${common.buildType}/test_async`); | ||
|
||
const testException = 'test_async_cb_exception'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we typically just say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
// Exception thrown from async completion callback. | ||
// (Tested in a spawned process because the exception is fatal.) | ||
if (process.argv[2] === 'child') { | ||
test_async.Test(1, common.mustCall(function(err, val) { | ||
throw new Error(testException); | ||
})); | ||
return; | ||
} | ||
const p = child_process.spawnSync( | ||
process.execPath, [ '--napi-modules', __filename, 'child' ]); | ||
assert.ifError(p.error); | ||
assert.ok(p.stderr.toString().includes(testException)); | ||
|
||
// Successful async execution and completion callback. | ||
test_async.Test(5, common.mustCall(function(err, val) { | ||
assert.strictEqual(err, null); | ||
assert.strictEqual(val, 10); | ||
process.nextTick(common.mustCall()); | ||
})); | ||
|
||
// Async work item cancellation with callback. | ||
test_async.TestCancel(common.mustCall()); |
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
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.
So … I’ve given this a bit of thought before. Semantically and technically it would make the most sense if
_complete
ran inside aMakeCallback
scope. Unfortunately, right now that would mean that_complete
would need to be specified as a JS function (which would probably even be fine except that nobody would remember to callnapi_delete_async_work
inside it).Once #11883 lands, I would like to look into splitting the
enter
andexit
parts ofMakeCallback
into their own parts (like what https://github.com/matthewloring/node/commit/dd178336a60c2698619b96a62267bd528c09ae0d does for Promises), and ideally using them here to give the users an easier API.Do you think that’s a good idea? I think it makes sense but it might require some weird hacks in N-API for the time being (like wrapping the
_complete
callback inside a JS function just to pass it toMakeCallback
…)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.
Conceptually that makes sense to me. But would the extra call to JS and back be a performance issue? And I'd prefer to avoid depending on any new APIs (refactoring
MakeCallback()
intoenter
andexit
parts) because that would make back-porting the N-API code more difficult.But anyway, that would not address the unhandled-exception issue. Based on my experimentation,
MakeCallback()
doesn't do anything to report unhandled exceptions from the JavaScript function that it invokes. Should that be considered a bug?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.
Sorry, maybe I didn’t express myself well – my thought was that we’d use a JS wrapper for now, and for backported APIs, and them move from that to the new APIs once they exist in Node core (and only for the Node core implementation).
The extra call to JS and back probably has a slight performance impact, but I’d guess that’s okay when compared to the general overhead of doing async work.
Yes, it’s orthogonal to this PR and won’t block it in any way. :)
I don’t think so – it should probably leave the caller the chance to clean up for themselves when an error occurred (also, implementing it in another way would probably be weird with nested MakeCallbacks).
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.
So the main benefit then would be so that user C++ callback code doesn't have to use
napi_make_callback()
to call JS code; it could use the regularnapi_call_function()
instead?We'd still need to keep the
napi_make_callback()
API to support advanced async scenarios though.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 I understand correctly it would be less about convenience and more about safety. If javascript is execute is should be in the state/context that MakeCallback provides. If we enforce that, it is safer than documenting and asking the implementer to make sure they do that.