From 2867c8d8db0943ddb30626e66c782853173def4c Mon Sep 17 00:00:00 2001 From: Marisa Heit Date: Mon, 25 Jul 2022 21:15:29 -0500 Subject: [PATCH] Fix potential race condition when creating detached threads --- src/utils/win32_glibstubs.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/utils/win32_glibstubs.c b/src/utils/win32_glibstubs.c index 3a7d289a..4bb57c30 100644 --- a/src/utils/win32_glibstubs.c +++ b/src/utils/win32_glibstubs.c @@ -213,26 +213,37 @@ GThread *fluid_g_thread_create(GThreadFunc func, void *data, BOOL joinable, GErr info->func = func; info->data = data; - info->handle = (HANDLE)_beginthreadex(NULL, 0, g_thread_wrapper, info, 0, NULL); + + HANDLE thread = (HANDLE)_beginthreadex(NULL, 0, g_thread_wrapper, info, CREATE_SUSPENDED, NULL); + if (error != NULL) { - error_container.code = errno; + error_container.code = thread ? 0 : errno; if (errno != 0) { error_container.message = strerror(errno); } *error = &error_container; } - if (info->handle == 0) + + if (thread == NULL) { free(info); info = NULL; } - else if (!joinable) + else { - /* Release thread reference, if caller doesn't want to join */ - CloseHandle(info->handle); - info->handle = NULL; + if (!joinable) + { + /* Release thread reference, if caller doesn't want to join */ + CloseHandle(thread); + info->handle = NULL; + } + else + { + info->handle = thread; + } + ResumeThread(thread); } return info; }