Skip to content

Commit b5588da

Browse files
Gabriel SchulhofBethGriggs
Gabriel Schulhof
authored andcommitted
n-api: finalize during second-pass callback
Calling into the engine from a weak callback is unsafe, however, the engine offers a way to attach a second-pass weak callback which gets called when it is safe to call into JavaScript. This moves the point at which the N-API finalize callback gets called to this latter point. Fixes: #25927 PR-URL: #25992 Backport-PR-URL: #26058 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 48a5241 commit b5588da

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/node_api.cc

+15
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,25 @@ class Reference : private Finalizer {
471471
}
472472

473473
private:
474+
// The N-API finalizer callback may make calls into the engine. V8's heap is
475+
// not in a consistent state during the weak callback, and therefore it does
476+
// not support calls back into it. However, it provides a mechanism for adding
477+
// a finalizer which may make calls back into the engine by allowing us to
478+
// attach such a second-pass finalizer from the first pass finalizer. Thus,
479+
// we do that here to ensure that the N-API finalizer callback is free to call
480+
// into the engine.
474481
static void FinalizeCallback(const v8::WeakCallbackInfo<Reference>& data) {
475482
Reference* reference = data.GetParameter();
483+
484+
// The reference must be reset during the first pass.
476485
reference->_persistent.Reset();
477486

487+
data.SetSecondPassCallback(SecondPassCallback);
488+
}
489+
490+
static void SecondPassCallback(const v8::WeakCallbackInfo<Reference>& data) {
491+
Reference* reference = data.GetParameter();
492+
478493
napi_env env = reference->_env;
479494

480495
if (reference->_finalize_callback != nullptr) {

0 commit comments

Comments
 (0)