From 5486f6bd517382284109ff2f5355b61de417c4e8 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 20 Nov 2017 20:35:02 +0100 Subject: [PATCH] unix,win: wait for threads to start It appears that, at least on Windows, the process terminates abnormally when the program exits before the worker threads complete initializing. Wait for the threads to spin up in `init_threads()` to avoid that. Refs: https://github.com/libuv/libuv/pull/1613#issuecomment-344958863 PR-URL: https://github.com/libuv/libuv/pull/1639 Reviewed-By: Bartosz Sosnowski Reviewed-By: Colin Ihrig Reviewed-By: Santiago Gimeno --- src/threadpool.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/threadpool.c b/src/threadpool.c index 10893411..ff64a3ae 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -38,7 +38,6 @@ static uv_thread_t* threads; static uv_thread_t default_threads[4]; static QUEUE exit_message; static QUEUE wq; -static volatile int initialized; static void uv__cancelled(struct uv__work* w) { @@ -105,7 +104,7 @@ static void post(QUEUE* q) { UV_DESTRUCTOR(static void cleanup(void)) { unsigned int i; - if (initialized == 0) + if (nthreads == 0) return; post(&exit_message); @@ -122,7 +121,6 @@ UV_DESTRUCTOR(static void cleanup(void)) { threads = NULL; nthreads = 0; - initialized = 0; } #endif @@ -130,6 +128,7 @@ UV_DESTRUCTOR(static void cleanup(void)) { static void init_threads(void) { unsigned int i; const char* val; + int spin; nthreads = ARRAY_SIZE(default_threads); val = getenv("UV_THREADPOOL_SIZE"); @@ -161,7 +160,11 @@ static void init_threads(void) { if (uv_thread_create(threads + i, worker, NULL)) abort(); - initialized = 1; + do { + uv_mutex_lock(&mutex); + spin = (idle_threads < nthreads); + uv_mutex_unlock(&mutex); + } while (spin); }