-
Notifications
You must be signed in to change notification settings - Fork 465
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
fix: handle c++ exception in TSFN callback #1345
Merged
Merged
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <cstdlib> | ||
#include "napi.h" | ||
#include "test_helper.h" | ||
|
||
#if (NAPI_VERSION > 3) | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
void CallJS(napi_env env, napi_value /* callback */, void* /*data*/) { | ||
Napi::Error error = Napi::Error::New(env, "test-from-native"); | ||
NAPI_THROW_VOID(error); | ||
} | ||
|
||
void TestCall(const CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
|
||
ThreadSafeFunction wrapped = | ||
ThreadSafeFunction::New(env, | ||
info[0].As<Napi::Function>(), | ||
Object::New(env), | ||
String::New(env, "Test"), | ||
0, | ||
1); | ||
wrapped.BlockingCall(static_cast<void*>(nullptr)); | ||
wrapped.Release(); | ||
} | ||
|
||
void TestCallWithNativeCallback(const CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
|
||
ThreadSafeFunction wrapped = ThreadSafeFunction::New( | ||
env, Napi::Function(), Object::New(env), String::New(env, "Test"), 0, 1); | ||
wrapped.BlockingCall(static_cast<void*>(nullptr), CallJS); | ||
wrapped.Release(); | ||
} | ||
|
||
} // namespace | ||
|
||
Object InitThreadSafeFunctionException(Env env) { | ||
Object exports = Object::New(env); | ||
exports["testCall"] = Function::New(env, TestCall); | ||
exports["testCallWithNativeCallback"] = | ||
Function::New(env, TestCallWithNativeCallback); | ||
|
||
return exports; | ||
} | ||
|
||
#endif |
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,55 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common'); | ||
const { spawnSync } = require('../napi_child'); | ||
|
||
function test (bindingPath) { | ||
const { status } = spawnSync( | ||
process.execPath, | ||
[ | ||
'--force-node-api-uncaught-exceptions-policy=true', | ||
__filename, | ||
'child', | ||
bindingPath | ||
], | ||
{ stdio: 'inherit' } | ||
); | ||
|
||
assert.strictEqual(status, 0); | ||
} | ||
|
||
if (process.argv[2] === 'child') { | ||
child(process.argv[3]) | ||
.catch(err => { | ||
process.exitCode = 1; | ||
console.error(err); | ||
}); | ||
} else { | ||
module.exports = common.runTestWithBindingPath(test); | ||
} | ||
|
||
async function child (bindingPath) { | ||
const binding = require(bindingPath); | ||
const { testCall, testCallWithNativeCallback } = binding.threadsafe_function_exception; | ||
|
||
await new Promise(resolve => { | ||
process.once('uncaughtException', common.mustCall(err => { | ||
assert.strictEqual(err.message, 'test'); | ||
resolve(); | ||
}, 1)); | ||
|
||
testCall(common.mustCall(() => { | ||
throw new Error('test'); | ||
}, 1)); | ||
}); | ||
|
||
await new Promise(resolve => { | ||
process.once('uncaughtException', common.mustCall(err => { | ||
assert.strictEqual(err.message, 'test-from-native'); | ||
resolve(); | ||
}, 1)); | ||
|
||
testCallWithNativeCallback(); | ||
}); | ||
} |
39 changes: 39 additions & 0 deletions
39
test/typed_threadsafe_function/typed_threadsafe_function_exception.cc
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,39 @@ | ||
#include <cstdlib> | ||
#include "napi.h" | ||
#include "test_helper.h" | ||
|
||
#if (NAPI_VERSION > 3) | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
void CallJS(Napi::Env env, | ||
Napi::Function /* callback */, | ||
std::nullptr_t* /* context */, | ||
void* /*data*/) { | ||
Napi::Error error = Napi::Error::New(env, "test-from-native"); | ||
NAPI_THROW_VOID(error); | ||
} | ||
|
||
using TSFN = TypedThreadSafeFunction<std::nullptr_t, void, CallJS>; | ||
|
||
void TestCall(const CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
|
||
TSFN wrapped = TSFN::New( | ||
env, Napi::Function(), Object::New(env), String::New(env, "Test"), 0, 1); | ||
wrapped.BlockingCall(static_cast<void*>(nullptr)); | ||
wrapped.Release(); | ||
} | ||
|
||
} // namespace | ||
|
||
Object InitTypedThreadSafeFunctionException(Env env) { | ||
Object exports = Object::New(env); | ||
exports["testCall"] = Function::New(env, TestCall); | ||
|
||
return exports; | ||
} | ||
|
||
#endif |
45 changes: 45 additions & 0 deletions
45
test/typed_threadsafe_function/typed_threadsafe_function_exception.js
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,45 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common'); | ||
|
||
const { spawnSync } = require('../napi_child'); | ||
|
||
function test (bindingPath) { | ||
const { status } = spawnSync( | ||
process.execPath, | ||
[ | ||
'--force-node-api-uncaught-exceptions-policy=true', | ||
__filename, | ||
'child', | ||
bindingPath | ||
], | ||
{ stdio: 'inherit' } | ||
); | ||
|
||
assert.strictEqual(status, 0); | ||
} | ||
|
||
if (process.argv[2] === 'child') { | ||
child(process.argv[3]) | ||
.catch(err => { | ||
process.exitCode = 1; | ||
console.error(err); | ||
}); | ||
} else { | ||
module.exports = common.runTestWithBindingPath(test); | ||
} | ||
|
||
async function child (bindingPath) { | ||
const binding = require(bindingPath); | ||
const { testCall } = binding.typed_threadsafe_function_exception; | ||
|
||
await new Promise(resolve => { | ||
process.once('uncaughtException', common.mustCall(err => { | ||
assert.strictEqual(err.message, 'test-from-native'); | ||
resolve(); | ||
}, 1)); | ||
|
||
testCall(); | ||
}); | ||
} |
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.
nit: For the tests, you could use
common.runTestInChildProcess()
. You need to extend it to also accept Node.js command line arguments, but I think it might make the code a little cleaner.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.
Yeah, thanks for the suggestion! It's definitely an improvement after #1325 landed.