Commit 2fd4580d4ad for nodejs

commit 2fd4580d4ad47a64f48d575b49c800fe1f4fe706
Author: René Larch <renelarch@gmail.com>
Date:   Wed Nov 25 15:28:20 2026 +0100

    src: rethrow linked binding initializer errors

    GetLinkedBinding() reads module.exports after it calls the register
    function of a linked binding. If the register function threw, that V8
    API call clears the pending exception (PrepareForExecutionScope), so
    the error never reaches the caller of process._linkedBinding(), which
    gets the empty exports object instead. This affects every linked
    binding, including Node-API ones, whose module registration already
    rethrows the error of the initializer.

    Addons loaded with process.dlopen() are not affected, because DLOpen()
    does not call into V8 after the register function. Rethrow the error
    before reading module.exports.

    Signed-off-by: René Larch <renelarch@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67571
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
    Reviewed-By: Chengzhong Wu <legendecas@gmail.com>

diff --git a/src/node_api.cc b/src/node_api.cc
index 64d26a7a502..cc6ec220b59 100644
--- a/src/node_api.cc
+++ b/src/node_api.cc
@@ -786,6 +786,10 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
     _exports = init(env, v8impl::JsValueFromV8LocalValue(exports));
   });

+  // Setting module.exports below would clear an exception thrown by the
+  // module's init function, so return and let it reach the caller.
+  if (env->isolate->HasPendingException()) return;
+
   // If register function returned a non-null exports object different from
   // the exports object we passed it, set that as the "exports" property of
   // the module.
diff --git a/src/node_binding.cc b/src/node_binding.cc
index b8a1a6f64c4..b4560045eba 100644
--- a/src/node_binding.cc
+++ b/src/node_binding.cc
@@ -1090,6 +1090,10 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
         env, "Linked binding has no declared entry point.");
   }

+  // Reading module.exports below would clear an exception thrown by the
+  // binding's initializer, so return and let it reach the caller.
+  if (env->isolate()->HasPendingException()) return;
+
   Local<Value> effective_exports;
   if (module->Get(env->context(), env->exports_string())
           .ToLocal(&effective_exports)) {
diff --git a/test/cctest/test_linked_binding.cc b/test/cctest/test_linked_binding.cc
index d36b5874e74..360f0a81852 100644
--- a/test/cctest/test_linked_binding.cc
+++ b/test/cctest/test_linked_binding.cc
@@ -419,3 +419,111 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) {
   CHECK_EQ(strcmp(*utf8val, "world"), 0);
   CHECK_EQ(calls, 5);
 }
+
+void InitializeThrowingBinding(v8::Local<v8::Object> exports,
+                               v8::Local<v8::Value> module,
+                               v8::Local<v8::Context> context,
+                               void* priv) {
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  isolate->ThrowException(v8::Exception::Error(
+      v8::String::NewFromUtf8Literal(isolate, "init failed")));
+}
+
+TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingThrowsTest) {
+  const v8::HandleScope handle_scope(isolate_);
+  const Argv argv;
+  Env test_env{handle_scope, argv};
+
+  AddLinkedBinding(
+      *test_env, "local_linked_throws", InitializeThrowingBinding, nullptr);
+
+  const char* run_script =
+      "try { process._linkedBinding('local_linked_throws'); 'not thrown'; }"
+      " catch (err) { err.message; }";
+  CHECK_EQ(RunScript(isolate_, run_script), "init failed");
+}
+
+void InitializeExportsThenThrowingBinding(v8::Local<v8::Object> exports,
+                                          v8::Local<v8::Value> module,
+                                          v8::Local<v8::Context> context,
+                                          void* priv) {
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  v8::Local<v8::Object> new_exports = v8::Object::New(isolate);
+  CHECK(new_exports
+            ->Set(context,
+                  v8::String::NewFromUtf8Literal(isolate, "key"),
+                  v8::String::NewFromUtf8Literal(isolate, "value"))
+            .FromJust());
+  CHECK(module.As<v8::Object>()
+            ->Set(context,
+                  v8::String::NewFromUtf8Literal(isolate, "exports"),
+                  new_exports)
+            .FromJust());
+  isolate->ThrowException(v8::Exception::Error(
+      v8::String::NewFromUtf8Literal(isolate, "init failed")));
+}
+
+TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingExportsThenThrowsTest) {
+  const v8::HandleScope handle_scope(isolate_);
+  const Argv argv;
+  Env test_env{handle_scope, argv};
+
+  AddLinkedBinding(*test_env,
+                   "local_linked_exports_throws",
+                   InitializeExportsThenThrowingBinding,
+                   nullptr);
+
+  const char* run_script =
+      "try { process._linkedBinding('local_linked_exports_throws').key; }"
+      " catch (err) { err.message; }";
+  CHECK_EQ(RunScript(isolate_, run_script), "init failed");
+}
+
+napi_value InitializeThrowingNapiBinding(napi_env env, napi_value exports) {
+  CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok);
+  return nullptr;
+}
+
+TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiThrowsTest) {
+  const v8::HandleScope handle_scope(isolate_);
+  const Argv argv;
+  Env test_env{handle_scope, argv};
+
+  AddLinkedBinding(*test_env,
+                   "local_linked_napi_throws",
+                   InitializeThrowingNapiBinding,
+                   NAPI_VERSION);
+
+  const char* run_script =
+      "try { process._linkedBinding('local_linked_napi_throws');"
+      " 'not thrown'; } catch (err) { err.message; }";
+  CHECK_EQ(RunScript(isolate_, run_script), "init failed");
+}
+
+napi_value InitializeExportsThenThrowingNapiBinding(napi_env env,
+                                                    napi_value exports) {
+  napi_value new_exports;
+  napi_value value;
+  CHECK_EQ(napi_create_object(env, &new_exports), napi_ok);
+  CHECK_EQ(napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value),
+           napi_ok);
+  CHECK_EQ(napi_set_named_property(env, new_exports, "key", value), napi_ok);
+  CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok);
+  return new_exports;
+}
+
+TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiExportsThrowsTest) {
+  const v8::HandleScope handle_scope(isolate_);
+  const Argv argv;
+  Env test_env{handle_scope, argv};
+
+  AddLinkedBinding(*test_env,
+                   "local_linked_napi_exports_throws",
+                   InitializeExportsThenThrowingNapiBinding,
+                   NAPI_VERSION);
+
+  const char* run_script =
+      "try { process._linkedBinding('local_linked_napi_exports_throws').key; }"
+      " catch (err) { err.message; }";
+  CHECK_EQ(RunScript(isolate_, run_script), "init failed");
+}
diff --git a/test/node-api/test_init_exception/binding.gyp b/test/node-api/test_init_exception/binding.gyp
new file mode 100644
index 00000000000..bfd03ff5d9b
--- /dev/null
+++ b/test/node-api/test_init_exception/binding.gyp
@@ -0,0 +1,8 @@
+{
+  "targets": [
+    {
+      "target_name": "test_init_exception",
+      "sources": [ "test_init_exception.c" ]
+    }
+  ]
+}
diff --git a/test/node-api/test_init_exception/test.js b/test/node-api/test_init_exception/test.js
new file mode 100644
index 00000000000..41524301840
--- /dev/null
+++ b/test/node-api/test_init_exception/test.js
@@ -0,0 +1,10 @@
+'use strict';
+const common = require('../../common');
+const assert = require('assert');
+
+// An exception thrown by the init function must reach the caller of
+// require(), even when the init function also returns new exports.
+assert.throws(
+  () => require(`./build/${common.buildType}/test_init_exception`),
+  { message: 'Error during Init' },
+);
diff --git a/test/node-api/test_init_exception/test_init_exception.c b/test/node-api/test_init_exception/test_init_exception.c
new file mode 100644
index 00000000000..6c32521f5f5
--- /dev/null
+++ b/test/node-api/test_init_exception/test_init_exception.c
@@ -0,0 +1,17 @@
+#include <node_api.h>
+#include "../../js-native-api/common.h"
+
+// Throws after creating new exports. require() must throw the error instead
+// of returning the new exports.
+static napi_value Init(napi_env env, napi_value exports) {
+  napi_value new_exports;
+  napi_value value;
+  NODE_API_CALL(env, napi_create_object(env, &new_exports));
+  NODE_API_CALL(
+      env, napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value));
+  NODE_API_CALL(env, napi_set_named_property(env, new_exports, "key", value));
+  NODE_API_CALL(env, napi_throw_error(env, NULL, "Error during Init"));
+  return new_exports;
+}
+
+NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)