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 <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/37876
Backport-PR-URL: https://github.com/nodejs/node/pull/42512
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Michael Dawson authored and Richard Lau committed 2022-03-30 10:48:44 -04:00
1 parent a2f4206415
commit 2aa9ca1ea9
1 file changed
+5 -2
+5 -2
View File
@@ -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);