Repository navigation
Conversation
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
With LibNode 26 the error thrown by a linked module's init function no longer reaches the caller of process._linkedBinding(): Node.js GetLinkedBinding() reads module.exports after the init function returns, and that V8 API call clears the pending exception. This affects every linked binding and is fixed in nodejs/node#67571. The test now checks that the error does not stay pending on the runtime's env, which fails without moving it to the module's env. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
936418e to
e354615
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #67571 +/- ##
==========================================
+ Coverage 90.70% 90.72% +0.02%
==========================================
Files 801 801
Lines 280726 280811 +85
Branches 54180 54224 +44
==========================================
+ Hits 254634 254776 +142
+ Misses 16259 16205 -54
+ Partials 9833 9830 -3
🚀 New features to boost your workflow:
|
Signed-off-by: René Larch <renelarch@gmail.com>
|
@2246250622 Thanks for the review, |
With LibNode 26 the error thrown by a linked module's init function no longer reaches the caller of process._linkedBinding(): Node.js GetLinkedBinding() reads module.exports after the init function returns, and that V8 API call clears the pending exception. This affects every linked binding and is fixed in nodejs/node#67571. The test now checks that the error does not stay pending on the runtime's env, which fails without moving it to the module's env. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Return right after the register function when it left an exception pending, instead of catching and rethrowing it with a TryCatch. This keeps the code structure of main and matches the check in napi_module_register_by_symbol. Signed-off-by: René Larch <renelarch@gmail.com>
When a Node-API module's init function throws and returns an object other than the exports it received, napi_module_register_by_symbol() sets module.exports to that object. That V8 API call clears the pending exception, so require() of such an addon returned the new exports instead of throwing, and process._linkedBinding() did the same for a linked Node-API binding. Return before setting module.exports when the init function left an exception pending. Signed-off-by: René Larch <renelarch@gmail.com>
|
Following @vmoroz's suggestion in microsoft/node-api-dotnet#512 (comment) The |
After talking to @vmoroz and following his suggestion in microsoft/node-api-dotnet#512 (comment)
I've changed the PR to adopt his changes in microsoft/v8-jsi#289
When the initializer of a linked binding or a Node-API addon throws, the error
can get lost and the call returns the module's exports instead:
process._linkedBinding()of a linked binding whose register function throwsreturns the empty exports object.
require()of a Node-API addon whose init function throws and returns a newexports object returns that object.
In both cases Node.js makes a V8 API call after the initializer returns:
GetLinkedBinding()readsmodule.exports, andnapi_module_register_by_symbol()sets it. Such calls clear a pending exception (they enter through
PrepareForExecutionScope), so the error never reaches the caller. Plain C++ andNode-API linked bindings are both affected. Addons loaded with
process.dlopen()are only affected through the Node-API path, because
DLOpen()itself makes no V8call after the register function.
The fix returns right after the initializer when it left an exception pending
(
isolate->HasPendingException(), as already used insrc/node_sqlite.cc), inboth places, before
module.exportsis touched.The approach follows @vmoroz's fix in LibNode (microsoft/v8-jsi#289), who
suggested bringing it upstream in this PR:
microsoft/node-api-dotnet#512 (comment).
An earlier revision of this PR used a
TryCatchandReThrow()inGetLinkedBinding()only; the pending-exception check is simpler and also coversthe Node-API path.
I found this while embedding Node.js 26 from .NET
(microsoft/node-api-dotnet#512): with LibNode 20 (Node.js 20.18) the error of a
linked Node-API module's initializer reached
process._linkedBinding(), withLibNode 26 it no longer did. LibNode 26.1.2 ships the fix above, and
node-api-dotnet's test asserts the error again with it.
Tests
test/cctest/test_linked_binding.cc: four linked bindings whose initializerthrows (plain C++, plain C++ that sets
module.exportsfirst, Node-API,Node-API that returns new exports); each expects
init failedfromprocess._linkedBinding().test/node-api/test_init_exception: an addon whose init throws and returns newexports;
require()must throwError during Init.With
main'ssrc/files, all four cctests and the addon test fail; with thechange, all 15
LinkedBindingTest.*tests and the addon test pass. cpplint andeslint are clean. I ran these on Linux (x64, GCC 13.3), not the full test suite.