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

src: distinguish env stopping flags #45907

Merged
merged 1 commit into from Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/api/environment.cc
Expand Up @@ -424,6 +424,9 @@ void FreeEnvironment(Environment* env) {
Context::Scope context_scope(env->context());
SealHandleScope seal_handle_scope(isolate);

// Set the flag in accordance with the DisallowJavascriptExecutionScope
// above.
env->set_can_call_into_js(false);
env->set_stopping(true);
env->stop_sub_worker_contexts();
env->RunCleanup();
Expand Down
7 changes: 5 additions & 2 deletions src/env.cc
Expand Up @@ -910,10 +910,13 @@ void Environment::InitializeLibuv() {
}

void Environment::ExitEnv() {
set_can_call_into_js(false);
// Should not access non-thread-safe methods here.
set_stopping(true);
isolate_->TerminateExecution();
SetImmediateThreadsafe([](Environment* env) { uv_stop(env->event_loop()); });
SetImmediateThreadsafe([](Environment* env) {
env->set_can_call_into_js(false);
uv_stop(env->event_loop());
});
}

void Environment::RegisterHandleCleanups() {
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Expand Up @@ -780,6 +780,7 @@ class Environment : public MemoryRetainer {
void stop_sub_worker_contexts();
template <typename Fn>
inline void ForEachWorker(Fn&& iterator);
// Determine if the environment is stopping. This getter is thread-safe.
inline bool is_stopping() const;
inline void set_stopping(bool value);
inline std::list<node_module>* extra_linked_bindings();
Expand Down
2 changes: 1 addition & 1 deletion src/node_http2.cc
Expand Up @@ -1127,7 +1127,7 @@ int Http2Session::OnStreamClose(nghttp2_session* handle,
// Don't close synchronously in case there's pending data to be written. This
// may happen when writing trailing headers.
if (code == NGHTTP2_NO_ERROR && nghttp2_session_want_write(handle) &&
!env->is_stopping()) {
env->can_call_into_js()) {
env->SetImmediate([handle, id, code, user_data](Environment* env) {
OnStreamClose(handle, id, code, user_data);
});
Expand Down
2 changes: 1 addition & 1 deletion src/stream_base.cc
Expand Up @@ -609,7 +609,7 @@ void ReportWritesToJSStreamListener::OnStreamAfterReqFinished(
StreamReq* req_wrap, int status) {
StreamBase* stream = static_cast<StreamBase*>(stream_);
Environment* env = stream->stream_env();
if (env->is_stopping()) return;
if (!env->can_call_into_js()) return;
AsyncWrap* async_wrap = req_wrap->GetAsyncWrap();
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Expand Down