From e707514c80371971027cd42682931f171aedd125 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 15 Apr 2021 09:05:48 -0700 Subject: [PATCH] src: fix finalization crash PR-URL: https://github.com/nodejs/node/pull/38250 Backport-PR-URL: https://github.com/nodejs/node/pull/42512 Fixes: https://github.com/nodejs/node/issues/38040 Reviewed-By: Beth Griggs Reviewed-By: Antoine du Hamel --- src/js_native_api_v8.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 8254dc7d6c8aa2..81619b914153c6 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -379,6 +379,9 @@ class Reference : public RefBase { protected: inline void Finalize(bool is_env_teardown = false) override { + if (is_env_teardown) env_teardown_finalize_started_ = true; + if (!is_env_teardown && env_teardown_finalize_started_) return; + // During env teardown, `~napi_env()` alone is responsible for finalizing. // Thus, we don't want any stray gc passes to trigger a second call to // `RefBase::Finalize()`. ClearWeak will ensure that even if the @@ -467,6 +470,7 @@ class Reference : public RefBase { reference->Finalize(); } + bool env_teardown_finalize_started_ = false; v8impl::Persistent _persistent; SecondPassCallParameterRef* _secondPassParameter; };