Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Dawson <mdawson@devrus.com>2021-05-01 00:51:11 +0300
committerDanielle Adams <adamzdanielle@gmail.com>2021-05-31 22:34:48 +0300
commit496f7eae925a3a6174b167ceec4e5c2bc815e911 (patch)
treea96a2c4e2f9cfae75d2d1c91b7180ba9dfe4f649 /src
parent6da4aa30c6f31dbcb83579bf82192d8418e0dc9b (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')
-rw-r--r--src/js_native_api_v8.h31
-rw-r--r--src/node_api.cc9
2 files changed, 38 insertions, 2 deletions
diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h
index 1a62782c1ad..f619248a843 100644
--- a/src/js_native_api_v8.h
+++ b/src/js_native_api_v8.h
@@ -122,6 +122,37 @@ struct napi_env__ {
void* instance_data = nullptr;
};
+// This class is used to keep a napi_env live in a way that
+// is exception safe versus calling Ref/Unref directly
+class EnvRefHolder {
+ public:
+ explicit EnvRefHolder(napi_env env) : _env(env) {
+ _env->Ref();
+ }
+
+ explicit EnvRefHolder(const EnvRefHolder& other): _env(other.env()) {
+ _env->Ref();
+ }
+
+ EnvRefHolder(EnvRefHolder&& other) {
+ _env = other._env;
+ other._env = nullptr;
+ }
+
+ ~EnvRefHolder() {
+ if (_env != nullptr) {
+ _env->Unref();
+ }
+ }
+
+ napi_env env(void) const {
+ return _env;
+ }
+
+ private:
+ napi_env _env;
+};
+
static inline napi_status napi_clear_last_error(napi_env env) {
env->last_error.error_code = napi_ok;
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) {