Skip to content

Commit ec3e5b4

Browse files
mhdawsontargos
authored andcommitted
node-api: avoid SecondPassCallback crash
PR #38000 added indirection so that we could stop finalization in cases where it had been scheduled in a second pass callback but we were doing it in advance in environment teardown. Unforunately we missed that the code which tries to clear the second pass parameter checked if the pointer to the parameter (_secondPassParameter) was nullptr and that when the second pass callback was scheduled we set _secondPassParameter to nullptr in order to avoid it being deleted outside of the second pass callback. The net result was that we would not clear the _secondPassParameter contents and failed to avoid the Finalization in the second pass callback. This PR adds an additional boolean for deciding if the secondPassParameter should be deleted outside of the second pass callback instead of setting secondPassParameter to nullptr thus avoiding the conflict between the 2 ways it was being used. See the discussion starting at: #38273 (comment) for how this was discovered on OSX while trying to upgrade to a new V8 version. Signed-off-by: Michael Dawson <[email protected]> PR-URL: #38899 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5b5e07a commit ec3e5b4

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/js_native_api_v8.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ class Reference : public RefBase {
321321
Reference(napi_env env, v8::Local<v8::Value> value, Args&&... args)
322322
: RefBase(env, std::forward<Args>(args)...),
323323
_persistent(env->isolate, value),
324-
_secondPassParameter(new SecondPassCallParameterRef(this)) {
324+
_secondPassParameter(new SecondPassCallParameterRef(this)),
325+
_secondPassScheduled(false) {
325326
if (RefCount() == 0) {
326327
SetWeak();
327328
}
@@ -348,7 +349,7 @@ class Reference : public RefBase {
348349
// If the second pass callback is scheduled, it will delete the
349350
// parameter passed to it, otherwise it will never be scheduled
350351
// and we need to delete it here.
351-
if (_secondPassParameter != nullptr) {
352+
if (!_secondPassScheduled) {
352353
delete _secondPassParameter;
353354
}
354355
}
@@ -445,8 +446,7 @@ class Reference : public RefBase {
445446
reference->_persistent.Reset();
446447
// Mark the parameter not delete-able until the second pass callback is
447448
// invoked.
448-
reference->_secondPassParameter = nullptr;
449-
449+
reference->_secondPassScheduled = true;
450450
data.SetSecondPassCallback(SecondPassCallback);
451451
}
452452

@@ -468,12 +468,14 @@ class Reference : public RefBase {
468468
// the reference itself has already been deleted so nothing to do
469469
return;
470470
}
471+
reference->_secondPassParameter = nullptr;
471472
reference->Finalize();
472473
}
473474

474475
bool env_teardown_finalize_started_ = false;
475476
v8impl::Persistent<v8::Value> _persistent;
476477
SecondPassCallParameterRef* _secondPassParameter;
478+
bool _secondPassScheduled;
477479
};
478480

479481
enum UnwrapAction {

0 commit comments

Comments
 (0)