Skip to content

Commit 03ffcf7

Browse files
Gabriel Schulhofaddaleax
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 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 4c22d6e commit 03ffcf7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/js_native_api_v8.cc

+15
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,25 @@ class Reference : private Finalizer {
281281
}
282282

283283
private:
284+
// The N-API finalizer callback may make calls into the engine. V8's heap is
285+
// not in a consistent state during the weak callback, and therefore it does
286+
// not support calls back into it. However, it provides a mechanism for adding
287+
// a finalizer which may make calls back into the engine by allowing us to
288+
// attach such a second-pass finalizer from the first pass finalizer. Thus,
289+
// we do that here to ensure that the N-API finalizer callback is free to call
290+
// into the engine.
284291
static void FinalizeCallback(const v8::WeakCallbackInfo<Reference>& data) {
285292
Reference* reference = data.GetParameter();
293+
294+
// The reference must be reset during the first pass.
286295
reference->_persistent.Reset();
287296

297+
data.SetSecondPassCallback(SecondPassCallback);
298+
}
299+
300+
static void SecondPassCallback(const v8::WeakCallbackInfo<Reference>& data) {
301+
Reference* reference = data.GetParameter();
302+
288303
napi_env env = reference->_env;
289304

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

0 commit comments

Comments
 (0)