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 5 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
31 changes: 31 additions & 0 deletions src/js_native_api_v8.h
Expand Up @@ -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 LiveEnv {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can call this EnvRefHolder or similar in a more explicit way?

Copy link
Member Author

Choose a reason for hiding this comment

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

@gabrielschulhof, @addaleax, @KevinEady, thoughts on the name? Does EnvRefHolder sound good to you?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm good with EnvRefHolder and see Anna was +1 as well. Will update.

Copy link
Member Author

Choose a reason for hiding this comment

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

@legendecas, @addaleax updated if you want to take another look.

public:
explicit LiveEnv(napi_env env) : _env(env) {
_env->Ref();
}

explicit LiveEnv(const LiveEnv& other): _env(other.env()) {
_env->Ref();
}

LiveEnv(LiveEnv&& other) {
_env = other._env;
other._env = nullptr;
}

~LiveEnv() {
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;

Expand Down
9 changes: 7 additions & 2 deletions src/node_api.cc
Expand Up @@ -38,8 +38,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
// LiveEnv provides an exception safe wrapper to Ref and then
// Unref once the lamba is freed
LiveEnv 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) {
Expand Down