From 94c4323d56c1f05b389d5aec7a8dab3e76d89a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Sch=C3=BCnemann?= Date: Wed, 21 Dec 2016 11:36:50 +0100 Subject: [PATCH] async_wrap: close the destroy_ids_idle_handle_ The destroy_ids_idle_handle_ needs to be closed on environment destruction. Not closing the handle leaves a dangling pointer in the used uv loop. This leads to undefined behavior when the uv loop is used after the environment has been destroyed. PR-URL: https://github.com/nodejs/node/pull/10385 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- src/env-inl.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/env-inl.h b/src/env-inl.h index 83db3d33b6d18f..db9f5c205e4132 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -207,6 +207,19 @@ inline Environment::~Environment() { delete hc; } + while (handle_cleanup_waiting_ != 0) + uv_run(event_loop(), UV_RUN_ONCE); + + // Closing the destroy_ids_idle_handle_ within the handle cleanup queue + // prevents the async wrap destroy hook from being called. + uv_handle_t* handle = + reinterpret_cast(&destroy_ids_idle_handle_); + handle->data = this; + handle_cleanup_waiting_ = 1; + uv_close(handle, [](uv_handle_t* handle) { + static_cast(handle->data)->FinishHandleCleanup(handle); + }); + while (handle_cleanup_waiting_ != 0) uv_run(event_loop(), UV_RUN_ONCE);