node-api: clear the pending exception before handling it

CallIntoModule() reset last_exception only after handle_exception()
returned. When handle_exception() reports the exception through
'uncaughtException', the listener runs while the exception is still
pending, so calling back into the module from it saw the same
exception again: an addon function failed with napi_pending_exception
and rethrew it, and a finalizer run by gc() in an experimental module
reported it a second time, which aborted the process with
"Re-entrant uncaught exception detected".

Take the exception out of last_exception before handling it.

Fixes: https://github.com/nodejs/node/issues/67421
Assisted-by: a coding agent
Signed-off-by: Manjeet Singh <024manjeetsingh@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67448
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Manjeet Singh authored and GitHub committed 2026-11-27 04:42:09 +00:00
1 parent eaf46fc7a9
commit 0089699ae3
4 files changed
+166 -1

No files matched your search

+5 -1
View File
@@ -94,8 +94,12 @@ struct napi_env__ {
CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
if (!last_exception.IsEmpty()) {
handle_exception(this, last_exception.Get(this->isolate));
// Clear the exception before handling it: handle_exception may run
// JavaScript (e.g. an 'uncaughtException' listener) that calls back into
// the module, which must not see this exception as still pending.
v8::Local<v8::Value> exception = last_exception.Get(this->isolate);
last_exception.Reset();
handle_exception(this, exception);
}
}
@@ -18,6 +18,20 @@
'NAPI_EXPERIMENTAL'
],
'sources': ['test_uncaught_exception.c']
},
{
'target_name': 'test_uncaught_exception_reentry',
'defines': [
'NAPI_EXPERIMENTAL'
],
'sources': ['test_uncaught_exception_reentry.c']
},
{
'target_name': 'test_uncaught_exception_reentry_v10',
'defines': [
'NAPI_VERSION=10'
],
'sources': ['test_uncaught_exception_reentry.c']
}
]
}
@@ -0,0 +1,89 @@
#include <node_api.h>
#include "../../js-native-api/common.h"
static int wrapped_data;
static void NoopFinalize(node_api_basic_env env, void* data, void* hint) {
(void)env;
(void)data;
(void)hint;
}
// Wrap(object): attaches a finalizer that does nothing to `object`.
static napi_value Wrap(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value object;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &object, NULL, NULL));
NODE_API_CALL(
env, napi_wrap(env, object, &wrapped_data, NoopFinalize, NULL, NULL));
return NULL;
}
static void ThrowFromCallJs(napi_env env,
napi_value js_callback,
void* context,
void* data) {
(void)js_callback;
(void)context;
(void)data;
NODE_API_CALL_RETURN_VOID(env, napi_throw_error(env, NULL, "call_js error"));
}
// ThrowFromThreadSafeFunction(): calls a thread-safe function once, whose
// call_js callback throws.
static napi_value ThrowFromThreadSafeFunction(napi_env env,
napi_callback_info info) {
(void)info;
napi_value name;
NODE_API_CALL(env,
napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &name));
napi_threadsafe_function tsfn;
NODE_API_CALL(env,
napi_create_threadsafe_function(env,
NULL,
NULL,
name,
0,
1,
NULL,
NULL,
NULL,
ThrowFromCallJs,
&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;
}
// CallFunction(fn): returns the result of calling `fn` through
// napi_call_function, which does not run while an exception is pending.
static napi_value CallFunction(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value fn;
napi_value recv;
napi_value result;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &fn, NULL, NULL));
NODE_API_CALL(env, napi_get_undefined(env, &recv));
NODE_API_CALL(env, napi_call_function(env, recv, fn, 0, NULL, &result));
return result;
}
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NODE_API_PROPERTY("Wrap", Wrap),
DECLARE_NODE_API_PROPERTY("ThrowFromThreadSafeFunction",
ThrowFromThreadSafeFunction),
DECLARE_NODE_API_PROPERTY("CallFunction", CallFunction),
};
NODE_API_CALL(
env,
napi_define_properties(env,
exports,
sizeof(properties) / sizeof(properties[0]),
properties));
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
@@ -0,0 +1,58 @@
'use strict';
// An exception thrown from the call_js callback of a thread-safe function is
// reported through 'uncaughtException'. The listener runs while Node-API is
// still handling that exception, so calling back into the module from it,
// directly or through a finalizer run by the garbage collector, must not see
// the exception as pending and report it a second time.
// Refs: https://github.com/nodejs/node/issues/67421
const common = require('../../common');
const assert = require('assert');
const [, , addonName, scenario] = process.argv;
if (addonName === undefined) {
const { spawnSyncAndAssert } = require('../../common/child_process');
for (const [name, scenarios] of [
// Finalizers run inside the garbage collector in experimental modules.
['test_uncaught_exception_reentry', ['gc', 'call']],
['test_uncaught_exception_reentry_v10', ['call']],
]) {
for (const scenario of scenarios) {
spawnSyncAndAssert(
process.execPath,
['--expose-gc', __filename, name, scenario],
{ stdout: 'listener finished\n', stderr: '' },
);
}
}
return;
}
const binding = require(`./build/${common.buildType}/${addonName}`);
// Objects with a native finalizer, kept alive until the listener runs so that
// they are collected by the gc() call in it.
let wrapped = [];
process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err.message, 'call_js error');
if (scenario === 'gc') {
wrapped = null;
globalThis.gc();
} else {
assert.strictEqual(binding.CallFunction(() => 42), 42);
}
console.log('listener finished');
}));
if (scenario === 'gc') {
for (let i = 0; i < 100; i++) {
const object = {};
binding.Wrap(object);
wrapped.push(object);
}
}
binding.ThrowFromThreadSafeFunction();