-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: fix Error::ThrowAsJavaScriptException crash
When terminating an environment (e.g., by calling worker.terminate), napi_throw, which is called by Error::ThrowAsJavaScriptException, returns napi_pending_exception, which is then incorrectly treated as a fatal error resulting in a crash. PR-URL: #975 Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@gmail.com> Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
- Loading branch information
Showing
7 changed files
with
228 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
Object InitError(Env env); | ||
|
||
Object Init(Env env, Object exports) { | ||
exports.Set("error", InitError(env)); | ||
return exports; | ||
} | ||
|
||
NODE_API_MODULE(addon, Init) |
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,94 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
// These tests ensure that Error types can be used in a terminating | ||
// environment without triggering any fatal errors. | ||
|
||
if (process.argv[2] === 'runInChildProcess') { | ||
const binding_path = process.argv[3]; | ||
const index_for_test_case = Number(process.argv[4]); | ||
|
||
const binding = require(binding_path); | ||
|
||
// Use C++ promises to ensure the worker thread is terminated right | ||
// before running the testable code in the binding. | ||
|
||
binding.error.resetPromises() | ||
|
||
const { Worker } = require('worker_threads'); | ||
|
||
const worker = new Worker( | ||
__filename, | ||
{ | ||
argv: [ | ||
'runInWorkerThread', | ||
binding_path, | ||
index_for_test_case, | ||
] | ||
} | ||
); | ||
|
||
binding.error.waitForWorkerThread() | ||
|
||
worker.terminate(); | ||
|
||
binding.error.releaseWorkerThread() | ||
|
||
return; | ||
} | ||
|
||
if (process.argv[2] === 'runInWorkerThread') { | ||
const binding_path = process.argv[3]; | ||
const index_for_test_case = Number(process.argv[4]); | ||
|
||
const binding = require(binding_path); | ||
|
||
switch (index_for_test_case) { | ||
case 0: | ||
binding.error.throwJSError('test', true); | ||
break; | ||
case 1: | ||
binding.error.throwTypeError('test', true); | ||
break; | ||
case 2: | ||
binding.error.throwRangeError('test', true); | ||
break; | ||
case 3: | ||
binding.error.throwDefaultError(false, true); | ||
break; | ||
case 4: | ||
binding.error.throwDefaultError(true, true); | ||
break; | ||
default: assert.fail('Invalid index'); | ||
} | ||
|
||
assert.fail('This should not be reachable'); | ||
} | ||
|
||
test(`./build/${buildType}/binding.node`, true); | ||
test(`./build/${buildType}/binding_noexcept.node`, true); | ||
test(`./build/${buildType}/binding_swallowexcept.node`, false); | ||
test(`./build/${buildType}/binding_swallowexcept_noexcept.node`, false); | ||
|
||
function test(bindingPath, process_should_abort) { | ||
const number_of_test_cases = 5; | ||
|
||
for (let i = 0; i < number_of_test_cases; ++i) { | ||
const child_process = require('./napi_child').spawnSync( | ||
process.execPath, | ||
[ | ||
__filename, | ||
'runInChildProcess', | ||
bindingPath, | ||
i, | ||
] | ||
); | ||
|
||
if (process_should_abort) { | ||
assert(child_process.status !== 0, `Test case ${bindingPath} ${i} failed: Process exited with status code 0.`); | ||
} else { | ||
assert(child_process.status === 0, `Test case ${bindingPath} ${i} failed: Process status ${child_process.status} is non-zero`); | ||
} | ||
} | ||
} |
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