Skip to content

Commit

Permalink
n-api: finalize during second-pass callback
Browse files Browse the repository at this point in the history
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: nodejs#25927
PR-URL: nodejs#25992
Backport-PR-URL: nodejs#26058
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
Gabriel Schulhof committed Mar 4, 2019
1 parent 156e4c8 commit e66b09c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,25 @@ class Reference : private Finalizer {
}

private:
// The N-API finalizer callback may make calls into the engine. V8's heap is
// not in a consistent state during the weak callback, and therefore it does
// not support calls back into it. However, it provides a mechanism for adding
// a finalizer which may make calls back into the engine by allowing us to
// attach such a second-pass finalizer from the first pass finalizer. Thus,
// we do that here to ensure that the N-API finalizer callback is free to call
// into the engine.
static void FinalizeCallback(const v8::WeakCallbackInfo<Reference>& data) {
Reference* reference = data.GetParameter();

// The reference must be reset during the first pass.
reference->_persistent.Reset();

data.SetSecondPassCallback(SecondPassCallback);
}

static void SecondPassCallback(const v8::WeakCallbackInfo<Reference>& data) {
Reference* reference = data.GetParameter();

napi_env env = reference->_env;

if (reference->_finalize_callback != nullptr) {
Expand Down

0 comments on commit e66b09c

Please sign in to comment.