-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle c++ exception in TSFN callback (#1345)
- Loading branch information
1 parent
4e62db4
commit 16a18c0
Showing
10 changed files
with
204 additions
and
15 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
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,33 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common'); | ||
|
||
module.exports = { | ||
testCall: async binding => { | ||
const { testCall } = 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)); | ||
}); | ||
}, | ||
testCallWithNativeCallback: async binding => { | ||
const { testCallWithNativeCallback } = binding.threadsafe_function_exception; | ||
|
||
await new Promise(resolve => { | ||
process.once('uncaughtException', common.mustCall(err => { | ||
assert.strictEqual(err.message, 'test-from-native'); | ||
resolve(); | ||
}, 1)); | ||
|
||
testCallWithNativeCallback(); | ||
}); | ||
} | ||
}; |
19 changes: 19 additions & 0 deletions
19
test/child_processes/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,19 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common'); | ||
|
||
module.exports = { | ||
testCall: async binding => { | ||
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(); | ||
}); | ||
} | ||
}; |
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,20 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
module.exports = common.runTest(test); | ||
|
||
const execArgv = ['--force-node-api-uncaught-exceptions-policy=true']; | ||
async function test () { | ||
await common.runTestInChildProcess({ | ||
suite: 'threadsafe_function_exception', | ||
testName: 'testCall', | ||
execArgv | ||
}); | ||
|
||
await common.runTestInChildProcess({ | ||
suite: 'threadsafe_function_exception', | ||
testName: 'testCallWithNativeCallback', | ||
execArgv | ||
}); | ||
} |
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 |
13 changes: 13 additions & 0 deletions
13
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,13 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
module.exports = common.runTest(test); | ||
|
||
async function test () { | ||
await common.runTestInChildProcess({ | ||
suite: 'typed_threadsafe_function_exception', | ||
testName: 'testCall', | ||
execArgv: ['--force-node-api-uncaught-exceptions-policy=true'] | ||
}); | ||
} |