-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
n-api: Handle fatal exception in async callback
- Create a handle scope before invoking the async completion callback, because it is basically always needed, easy for user code to forget, and this makes it more consistent with ordinary N-API function callbacks. - Check for an unhandled JS exception after invoking an async completion callback, and report it via `node::FatalException()`. - Add a corresponding test case for an exception in async callback. Previously, any unhandled JS exception thrown from a `napi_async_complete_callback` would be silently ignored. Among other things this meant assertions in some test cases could be undetected. Backport-PR-URL: #19447 PR-URL: #12838 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
2e36365
commit a128219
Showing
4 changed files
with
38 additions
and
27 deletions.
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'; | ||
|
||
// 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