Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node-api: fix shutdown crashes #38492

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/js_native_api_v8.h
Expand Up @@ -54,7 +54,8 @@ class RefTracker {
struct napi_env__ {
explicit napi_env__(v8::Local<v8::Context> context)
: isolate(context->GetIsolate()),
context_persistent(isolate, context) {
context_persistent(isolate, context),
is_env_teardown(false) {
CHECK_EQ(isolate, context->GetIsolate());
}
virtual ~napi_env__() {
Expand All @@ -63,11 +64,15 @@ struct napi_env__ {
// they delete during their `napi_finalizer` callbacks. If we deleted such
// references here first, they would be doubly deleted when the
// `napi_finalizer` deleted them subsequently.
is_env_teardown = true;
v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
v8impl::RefTracker::FinalizeAll(&reflist);
}
v8::Isolate* const isolate; // Shortcut for context()->GetIsolate()
v8impl::Persistent<v8::Context> context_persistent;
bool is_env_teardown;

inline bool isEnvTeardown() { return is_env_teardown; }

inline v8::Local<v8::Context> context() const {
return v8impl::PersistentToLocal::Strong(context_persistent);
Expand Down
34 changes: 28 additions & 6 deletions src/node_api.cc
Expand Up @@ -39,13 +39,21 @@ 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) {
if (!env->isEnvTeardown()) {
node_env()->SetImmediate([=](node::Environment* node_env) {
v8::HandleScope handle_scope(env->isolate);
v8::Context::Scope context_scope(env->context());
env->CallIntoModule([&](napi_env env) {
cb(env, data, hint);
});
});
} else {
v8::HandleScope handle_scope(env->isolate);
v8::Context::Scope context_scope(env->context());
env->CallIntoModule([&](napi_env env) {
cb(env, data, hint);
});
});
}
}

const char* GetFilename() const { return filename.c_str(); }
Expand All @@ -69,8 +77,22 @@ class BufferFinalizer : private Finalizer {

node::Environment* node_env =
static_cast<node_napi_env>(finalizer->_env)->node_env();
node_env->SetImmediate(
[finalizer = std::move(finalizer)](node::Environment* env) {
if (!finalizer->_env->isEnvTeardown()) {
node_env->SetImmediate(
[finalizer = std::move(finalizer)](node::Environment* env) {
if (finalizer->_finalize_callback == nullptr) return;

v8::HandleScope handle_scope(finalizer->_env->isolate);
v8::Context::Scope context_scope(finalizer->_env->context());

finalizer->_env->CallIntoModule([&](napi_env env) {
finalizer->_finalize_callback(
env,
finalizer->_finalize_data,
finalizer->_finalize_hint);
});
});
} else {
if (finalizer->_finalize_callback == nullptr) return;

v8::HandleScope handle_scope(finalizer->_env->isolate);
Expand All @@ -81,8 +103,8 @@ class BufferFinalizer : private Finalizer {
env,
finalizer->_finalize_data,
finalizer->_finalize_hint);
});
});
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you store the lambda above in a variable and either pass it to SetImmediate or call it directly, instead of duplicating the code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, just pushed a an update which looks like it resolved the crashes on master. Will look at refactoring once I confirm that it fixes the crashses we were seeing on master.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is not quite the same anymore, but there is still some that should be able to be shared.

}

struct Deleter {
Expand Down