[llvm] Fix thread handle leak on Windows (PR #156854)

Lukasz Mielicki via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 8 02:57:38 PDT 2025


=?utf-8?q?Ɓukasz?= Mielicki <lukasz.mielicki at intel.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/156854 at github.com>


https://github.com/lmielick updated https://github.com/llvm/llvm-project/pull/156854

>From 3644e8f6d888d7d69c541c42e7a9dc4e561991d5 Mon Sep 17 00:00:00 2001
From: Lukasz Mielicki <lukasz.mielicki at intel.com>
Date: Mon, 8 Sep 2025 09:51:32 +0000
Subject: [PATCH 1/2] Fix formatting

---
 llvm/lib/Support/Windows/Threading.inc | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.inc
index b11f216adeba4..d144e46114b5a 100644
--- a/llvm/lib/Support/Windows/Threading.inc
+++ b/llvm/lib/Support/Windows/Threading.inc
@@ -31,23 +31,20 @@ llvm_execute_on_thread_impl(unsigned(__stdcall *ThreadFunc)(void *), void *Arg,
   HANDLE hThread = (HANDLE)::_beginthreadex(NULL, StackSizeInBytes.value_or(0),
                                             ThreadFunc, Arg, 0, NULL);
 
-  if (!hThread) {
+  if (!hThread)
     ReportLastErrorFatal("_beginthreadex failed");
-  }
 
   return hThread;
 }
 
 void llvm_thread_join_impl(HANDLE hThread) {
-  if (::WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED) {
+  if (::WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED)
     ReportLastErrorFatal("WaitForSingleObject failed");
-  }
 }
 
 void llvm_thread_detach_impl(HANDLE hThread) {
-  if (::CloseHandle(hThread) == FALSE) {
+  if (::CloseHandle(hThread) == FALSE)
     ReportLastErrorFatal("CloseHandle failed");
-  }
 }
 
 DWORD llvm_thread_get_id_impl(HANDLE hThread) { return ::GetThreadId(hThread); }
@@ -202,9 +199,9 @@ template <typename F>
 static bool IterateProcInfo(LOGICAL_PROCESSOR_RELATIONSHIP Relationship, F Fn) {
   DWORD Len = 0;
   BOOL R = ::GetLogicalProcessorInformationEx(Relationship, NULL, &Len);
-  if (R || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+  if (R || GetLastError() != ERROR_INSUFFICIENT_BUFFER) 
     return false;
-  }
+
   auto *Info = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)calloc(1, Len);
   R = ::GetLogicalProcessorInformationEx(Relationship, Info, &Len);
   if (R) {

>From f78b03f326de4fb352f1e9e260601eb0be61304e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Mielicki?= <lukasz.mielicki at intel.com>
Date: Wed, 4 Jun 2025 11:55:12 +0200
Subject: [PATCH 2/2] Fix thread handle leak on Windows

---
 llvm/lib/Support/Windows/Threading.inc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.inc
index d144e46114b5a..6fb67ac8c64ff 100644
--- a/llvm/lib/Support/Windows/Threading.inc
+++ b/llvm/lib/Support/Windows/Threading.inc
@@ -40,6 +40,8 @@ llvm_execute_on_thread_impl(unsigned(__stdcall *ThreadFunc)(void *), void *Arg,
 void llvm_thread_join_impl(HANDLE hThread) {
   if (::WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED)
     ReportLastErrorFatal("WaitForSingleObject failed");
+  if (::CloseHandle(hThread) == FALSE)
+    ReportLastErrorFatal("CloseHandle failed");
 }
 
 void llvm_thread_detach_impl(HANDLE hThread) {



More information about the llvm-commits mailing list