Skip to content

Commit

Permalink
src: do not access Environment-owned handles after cleanup
Browse files Browse the repository at this point in the history
Do not access handles that have already begun to be closed
or are closed.

PR-URL: #26256
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
addaleax committed Mar 1, 2019
1 parent 74d11e7 commit 75ae77d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,11 @@ void Environment::RegisterHandleCleanups() {
void* arg) {
handle->data = env;

env->CloseHandle(handle, [](uv_handle_t* handle) {});
env->CloseHandle(handle, [](uv_handle_t* handle) {
#ifdef DEBUG
memset(handle, 0xab, uv_handle_size(handle->type));
#endif
});
};

RegisterHandleCleanup(
Expand Down Expand Up @@ -512,6 +516,7 @@ void Environment::PrintSyncTrace() const {
}

void Environment::RunCleanup() {
started_cleanup_ = true;
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
"RunCleanup", this);
CleanupHandles();
Expand Down Expand Up @@ -660,10 +665,13 @@ void Environment::RunAndClearNativeImmediates() {


void Environment::ScheduleTimer(int64_t duration_ms) {
if (started_cleanup_) return;
uv_timer_start(timer_handle(), RunTimers, duration_ms, 0);
}

void Environment::ToggleTimerRef(bool ref) {
if (started_cleanup_) return;

if (ref) {
uv_ref(reinterpret_cast<uv_handle_t*>(timer_handle()));
} else {
Expand Down Expand Up @@ -763,6 +771,8 @@ void Environment::CheckImmediate(uv_check_t* handle) {
}

void Environment::ToggleImmediateRef(bool ref) {
if (started_cleanup_) return;

if (ref) {
// Idle handle is needed only to stop the event loop from blocking in poll.
uv_idle_start(immediate_idle_handle(), [](uv_idle_t*){ });
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ class Environment {
CleanupHookCallback::Hash,
CleanupHookCallback::Equal> cleanup_hooks_;
uint64_t cleanup_hook_counter_ = 0;
bool started_cleanup_ = false;

static void EnvPromiseHook(v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
Expand Down

0 comments on commit 75ae77d

Please sign in to comment.