@@ -255,7 +255,7 @@ struct loader_impl_node_type
255255 int64_t base_active_handles;
256256 int64_t extra_active_handles;
257257 uv_prepare_t destroy_prepare;
258- uv_check_t destroy_check ;
258+ std::atomic_bool event_loop_empty ;
259259};
260260
261261typedef struct loader_impl_node_function_type
@@ -3618,6 +3618,7 @@ void *node_loader_impl_register(void *node_impl_ptr, void *env_ptr, void *functi
36183618 * so we can count the user defined async handles */
36193619 node_impl->base_active_handles = node_loader_impl_async_handles_count (node_impl);
36203620 node_impl->extra_active_handles = 0 ;
3621+ node_impl->event_loop_empty .store (false );
36213622
36223623 /* Signal start condition */
36233624 uv_cond_signal (&node_impl->cond );
@@ -4446,45 +4447,19 @@ static void node_loader_impl_destroy_prepare_close_cb(uv_handle_t *handle)
44464447 node_loader_impl_try_destroy (node_impl);
44474448}
44484449
4449- static void node_loader_impl_destroy_check_close_cb (uv_handle_t *handle)
4450- {
4451- uv_check_t *check = (uv_check_t *)handle;
4452- loader_impl_node node_impl = container_of (check, struct loader_impl_node_type , destroy_check);
4453- node_loader_impl_try_destroy (node_impl);
4454- }
4455-
44564450static void node_loader_impl_destroy_prepare_cb (uv_prepare_t *handle)
44574451{
44584452 loader_impl_node node_impl = container_of (handle, struct loader_impl_node_type , destroy_prepare);
44594453
4460- /* TODO: Delete this */
4461- node_loader_impl_print_handles (node_impl);
4462-
44634454 if (node_loader_impl_user_async_handles_count (node_impl) <= 0 )
44644455 {
4465- uv_check_stop (& node_impl->destroy_check );
4466- uv_close (( uv_handle_t *)& node_impl->destroy_check , NULL ) ;
4456+ node_impl->event_loop_empty . store ( true );
4457+ node_impl->extra_active_handles = 0 ;
44674458 uv_prepare_stop (handle);
44684459 uv_close ((uv_handle_t *)handle, &node_loader_impl_destroy_prepare_close_cb);
44694460 }
44704461}
44714462
4472- static void node_loader_impl_destroy_check_cb (uv_check_t *handle)
4473- {
4474- loader_impl_node node_impl = container_of (handle, struct loader_impl_node_type , destroy_check);
4475-
4476- /* TODO: Delete this */
4477- node_loader_impl_print_handles (node_impl);
4478-
4479- if (node_loader_impl_user_async_handles_count (node_impl) <= 0 )
4480- {
4481- uv_prepare_stop (&node_impl->destroy_prepare );
4482- uv_close ((uv_handle_t *)&node_impl->destroy_prepare , NULL );
4483- uv_check_stop (handle);
4484- uv_close ((uv_handle_t *)handle, node_loader_impl_destroy_check_close_cb);
4485- }
4486- }
4487-
44884463void node_loader_impl_destroy_safe (napi_env env, loader_impl_async_destroy_safe destroy_safe)
44894464{
44904465 napi_status status;
@@ -4497,21 +4472,16 @@ void node_loader_impl_destroy_safe(napi_env env, loader_impl_async_destroy_safe
44974472
44984473 node_loader_impl_exception (env, status);
44994474
4500- /* TODO: Delete this */
4501- node_loader_impl_print_handles (node_impl);
4502-
45034475 /* Check if there are async handles, destroy if the queue is empty, otherwise request the destroy */
4504- if (node_loader_impl_user_async_handles_count (node_impl) <= 0 )
4476+ if (node_loader_impl_user_async_handles_count (node_impl) <= 0 || node_impl-> event_loop_empty . load () == true )
45054477 {
45064478 node_loader_impl_destroy_safe_impl (node_impl, env);
45074479 }
45084480 else
45094481 {
4510- node_impl->extra_active_handles = 2 ;
4482+ node_impl->extra_active_handles = 1 ;
45114483 uv_prepare_init (node_impl->thread_loop , &node_impl->destroy_prepare );
45124484 uv_prepare_start (&node_impl->destroy_prepare , &node_loader_impl_destroy_prepare_cb);
4513- uv_check_init (node_impl->thread_loop , &node_impl->destroy_check );
4514- uv_check_start (&node_impl->destroy_check , &node_loader_impl_destroy_check_cb);
45154485 }
45164486
45174487 /* Close scope */
@@ -4544,8 +4514,8 @@ int64_t node_loader_impl_user_async_handles_count(loader_impl_node node_impl)
45444514
45454515void node_loader_impl_print_handles (loader_impl_node node_impl)
45464516{
4547- printf (" Number of active handles: %" PRIuS " \n " , node_loader_impl_async_handles_count (node_impl));
4548- printf (" Number of user active handles: %" PRIuS " \n " , node_loader_impl_user_async_handles_count (node_impl));
4517+ printf (" Number of active handles: %" PRId64 " \n " , node_loader_impl_async_handles_count (node_impl));
4518+ printf (" Number of user active handles: %" PRId64 " \n " , node_loader_impl_user_async_handles_count (node_impl));
45494519 uv_print_active_handles (node_impl->thread_loop , stdout);
45504520 fflush (stdout);
45514521}
0 commit comments