From 3ab223dd3221d1c548cbad4859512f716b7223f6 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 23 Mar 2021 11:26:18 -0400 Subject: [PATCH] node-api: fix crash in finalization Refs: https://github.com/nodejs/node-addon-api/issues/906 Refs: https://github.com/nodejs/node/pull/37616 Fix crash introduced by https://github.com/nodejs/node/pull/37616 Signed-off-by: Michael Dawson PR-URL: https://github.com/nodejs/node/pull/37876 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott Reviewed-By: James M Snell --- src/js_native_api_v8.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 602ded21569cd7..664aa2d41bbc98 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -374,8 +374,11 @@ class Reference : public RefBase { inline void Finalize(bool is_env_teardown = false) override { // 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 - // `Finalize()`, so let's reset the persistent here. - if (is_env_teardown) _persistent.ClearWeak(); + // `Finalize()`, so let's reset the persistent here if nothing is + // keeping it alive. + if (is_env_teardown && _persistent.IsWeak()) { + _persistent.ClearWeak(); + } // Chain up to perform the rest of the finalization. RefBase::Finalize(is_env_teardown);