@@ -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
514518void 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
662667void Environment::ScheduleTimer (int64_t duration_ms) {
668+ if (started_cleanup_) return ;
663669 uv_timer_start (timer_handle (), RunTimers, duration_ms, 0 );
664670}
665671
666672void 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
765773void 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 *){ });
0 commit comments