Skip to content

Commit 75ae77d

Browse files
committed
src: do not access Environment-owned handles after cleanup
Do not access handles that have already begun to be closed or are closed. PR-URL: #26256 Reviewed-By: Joyee Cheung <[email protected]>
1 parent 74d11e7 commit 75ae77d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/env.cc

+11-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,11 @@ void Environment::RegisterHandleCleanups() {
398398
void* arg) {
399399
handle->data = env;
400400

401-
env->CloseHandle(handle, [](uv_handle_t* handle) {});
401+
env->CloseHandle(handle, [](uv_handle_t* handle) {
402+
#ifdef DEBUG
403+
memset(handle, 0xab, uv_handle_size(handle->type));
404+
#endif
405+
});
402406
};
403407

404408
RegisterHandleCleanup(
@@ -512,6 +516,7 @@ void Environment::PrintSyncTrace() const {
512516
}
513517

514518
void Environment::RunCleanup() {
519+
started_cleanup_ = true;
515520
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
516521
"RunCleanup", this);
517522
CleanupHandles();
@@ -660,10 +665,13 @@ void Environment::RunAndClearNativeImmediates() {
660665

661666

662667
void Environment::ScheduleTimer(int64_t duration_ms) {
668+
if (started_cleanup_) return;
663669
uv_timer_start(timer_handle(), RunTimers, duration_ms, 0);
664670
}
665671

666672
void Environment::ToggleTimerRef(bool ref) {
673+
if (started_cleanup_) return;
674+
667675
if (ref) {
668676
uv_ref(reinterpret_cast<uv_handle_t*>(timer_handle()));
669677
} else {
@@ -763,6 +771,8 @@ void Environment::CheckImmediate(uv_check_t* handle) {
763771
}
764772

765773
void Environment::ToggleImmediateRef(bool ref) {
774+
if (started_cleanup_) return;
775+
766776
if (ref) {
767777
// Idle handle is needed only to stop the event loop from blocking in poll.
768778
uv_idle_start(immediate_idle_handle(), [](uv_idle_t*){ });

src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ class Environment {
11771177
CleanupHookCallback::Hash,
11781178
CleanupHookCallback::Equal> cleanup_hooks_;
11791179
uint64_t cleanup_hook_counter_ = 0;
1180+
bool started_cleanup_ = false;
11801181

11811182
static void EnvPromiseHook(v8::PromiseHookType type,
11821183
v8::Local<v8::Promise> promise,

0 commit comments

Comments
 (0)