diff options
author | Michael Dawson <mdawson@devrus.com> | 2021-05-01 00:51:11 +0300 |
---|---|---|
committer | Michael Dawson <mdawson@devrus.com> | 2021-05-20 17:17:45 +0300 |
commit | c32c5f0d433996695bcef02737038b0213f1831e (patch) | |
tree | 46da9acff69c903b5a42e5cacd1aafe02e367fdf /src/node_api.cc | |
parent | 3f025cb5d036a20ef92526360fa6b5356a7ee51a (diff) |
node-api: fix shutdown crashes
Refs: https://github.com/nodejs/node-addon-api/issues/906
Ensure that finalization is not defered during shutdown.
The env for the addon is deleted immediately after
iterating the list of finalizers to be run. Defering
causes crashes as the finalization uses the already
deleted env.
Signed-off-by: Michael Dawson <mdawson@devrus.com>
PR-URL: https://github.com/nodejs/node/pull/38492
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com>
Diffstat (limited to 'src/node_api.cc')
-rw-r--r-- | src/node_api.cc | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/node_api.cc b/src/node_api.cc index 4216356cb59..f28ca121932 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -39,8 +39,13 @@ struct node_napi_env__ : public napi_env__ { } void CallFinalizer(napi_finalize cb, void* data, void* hint) override { - napi_env env = static_cast<napi_env>(this); - node_env()->SetImmediate([=](node::Environment* node_env) { + // we need to keep the env live until the finalizer has been run + // EnvRefHolder provides an exception safe wrapper to Ref and then + // Unref once the lamba is freed + EnvRefHolder liveEnv(static_cast<napi_env>(this)); + node_env()->SetImmediate([=, liveEnv = std::move(liveEnv)] + (node::Environment* node_env) { + napi_env env = liveEnv.env(); v8::HandleScope handle_scope(env->isolate); v8::Context::Scope context_scope(env->context()); env->CallIntoModule([&](napi_env env) { |