diff options
author | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2019-02-07 12:11:01 -0800 |
---|---|---|
committer | Beth Griggs <Bethany.Griggs@uk.ibm.com> | 2019-03-20 17:27:29 +0000 |
commit | 2b2ad96ef284be7370ddf535b39fa05cf1d77fb1 (patch) | |
tree | 1823e9f4483a12e5f0605df1cb5f95fdb1a1294a | |
parent | ac9b8f7645da1818068336c269e605025b0fb467 (diff) | |
download | node-new-2b2ad96ef284be7370ddf535b39fa05cf1d77fb1.tar.gz |
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: https://github.com/nodejs/node/issues/25927
PR-URL: https://github.com/nodejs/node/pull/25992
Backport-PR-URL: https://github.com/nodejs/node/pull/26060
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
-rw-r--r-- | src/node_api.cc | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/node_api.cc b/src/node_api.cc index 196f5512c9..ec477e1d23 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -448,10 +448,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(); + // Check before calling the finalize callback, because the callback might // delete it. bool delete_self = reference->_delete_self; |