node-api: emit uncaught-exception on unhandled tsfn callbacks

PR-URL: https://github.com/nodejs/node/pull/36510
Fixes: https://github.com/nodejs/node/issues/36402
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
legendecas authored and Michaël Zasso committed 2022-07-31 09:44:36 +02:00
1 parent 49103e3240
commit 73f852e1e3
13 files changed
+304 -46

No files matched your search

@@ -269,6 +269,35 @@ static napi_value StartThreadNoJsFunc(napi_env env, napi_callback_info info) {
/** block_on_full */true, /** alt_ref_js_cb */true);
}
// Testing calling into JavaScript
static void ThreadSafeFunctionFinalize(napi_env env,
void* finalize_data,
void* finalize_hint) {
napi_ref js_func_ref = (napi_ref) finalize_data;
napi_value js_func;
napi_value recv;
NODE_API_CALL_RETURN_VOID(env, napi_get_reference_value(env, js_func_ref, &js_func));
NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &recv));
NODE_API_CALL_RETURN_VOID(env, napi_call_function(env, recv, js_func, 0, NULL, NULL));
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, js_func_ref));
}
// Testing calling into JavaScript
static napi_value CallIntoModule(napi_env env, napi_callback_info info) {
size_t argc = 4;
napi_value argv[4];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
napi_ref finalize_func;
NODE_API_CALL(env, napi_create_reference(env, argv[3], 1, &finalize_func));
napi_threadsafe_function tsfn;
NODE_API_CALL(env, napi_create_threadsafe_function(env, argv[0], argv[1], argv[2], 0, 1, finalize_func, ThreadSafeFunctionFinalize, NULL, NULL, &tsfn));
NODE_API_CALL(env, napi_call_threadsafe_function(tsfn, NULL, napi_tsfn_blocking));
NODE_API_CALL(env, napi_release_threadsafe_function(tsfn, napi_tsfn_release));
return NULL;
}
// Module init
static napi_value Init(napi_env env, napi_value exports) {
size_t index;
@@ -307,6 +336,7 @@ static napi_value Init(napi_env env, napi_value exports) {
DECLARE_NODE_API_PROPERTY("StopThread", StopThread),
DECLARE_NODE_API_PROPERTY("Unref", Unref),
DECLARE_NODE_API_PROPERTY("Release", Release),
DECLARE_NODE_API_PROPERTY("CallIntoModule", CallIntoModule),
};
NODE_API_CALL(env, napi_define_properties(env, exports,
@@ -0,0 +1,22 @@
'use strict';
// Flags: --no-force-node-api-uncaught-exceptions-policy
const common = require('../../common');
const binding = require(`./build/${common.buildType}/binding`);
process.on(
'uncaughtException',
common.mustNotCall('uncaught callback errors should be suppressed ' +
'with the option --no-force-node-api-uncaught-exceptions-policy')
);
binding.CallIntoModule(
common.mustCall(() => {
throw new Error('callback error');
}),
{},
'resource_name',
common.mustCall(function finalizer() {
throw new Error('finalizer error');
})
);
@@ -0,0 +1,27 @@
'use strict';
// Flags: --force-node-api-uncaught-exceptions-policy
const common = require('../../common');
const assert = require('assert');
const binding = require(`./build/${common.buildType}/binding`);
const callbackCheck = common.mustCall((err) => {
assert.throws(() => { throw err; }, /callback error/);
process.removeListener('uncaughtException', callbackCheck);
process.on('uncaughtException', finalizerCheck);
});
const finalizerCheck = common.mustCall((err) => {
assert.throws(() => { throw err; }, /finalizer error/);
});
process.on('uncaughtException', callbackCheck);
binding.CallIntoModule(
common.mustCall(() => {
throw new Error('callback error');
}),
{},
'resource_name',
common.mustCall(function finalizer() {
throw new Error('finalizer error');
})
);