From 2aa9ca1ea93b9b9b92a303d3c8def777f3a1dfee 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 Backport-PR-URL: https://github.com/nodejs/node/pull/42512 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 e969adc3918ec4..5106711bd2d1c1 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -373,8 +373,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);