Skip to content

Commit 6c0f7a5

Browse files
authored
Fix thread handle leak on Windows (llvm#156854)
1 parent ad3a0ae commit 6c0f7a5

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

llvm/lib/Support/Windows/Threading.inc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,22 @@ llvm_execute_on_thread_impl(unsigned(__stdcall *ThreadFunc)(void *), void *Arg,
3131
HANDLE hThread = (HANDLE)::_beginthreadex(NULL, StackSizeInBytes.value_or(0),
3232
ThreadFunc, Arg, 0, NULL);
3333

34-
if (!hThread) {
34+
if (!hThread)
3535
ReportLastErrorFatal("_beginthreadex failed");
36-
}
3736

3837
return hThread;
3938
}
4039

4140
void llvm_thread_join_impl(HANDLE hThread) {
42-
if (::WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED) {
41+
if (::WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED)
4342
ReportLastErrorFatal("WaitForSingleObject failed");
44-
}
43+
if (::CloseHandle(hThread) == FALSE)
44+
ReportLastErrorFatal("CloseHandle failed");
4545
}
4646

4747
void llvm_thread_detach_impl(HANDLE hThread) {
48-
if (::CloseHandle(hThread) == FALSE) {
48+
if (::CloseHandle(hThread) == FALSE)
4949
ReportLastErrorFatal("CloseHandle failed");
50-
}
5150
}
5251

5352
DWORD llvm_thread_get_id_impl(HANDLE hThread) { return ::GetThreadId(hThread); }
@@ -202,9 +201,9 @@ template <typename F>
202201
static bool IterateProcInfo(LOGICAL_PROCESSOR_RELATIONSHIP Relationship, F Fn) {
203202
DWORD Len = 0;
204203
BOOL R = ::GetLogicalProcessorInformationEx(Relationship, NULL, &Len);
205-
if (R || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
204+
if (R || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
206205
return false;
207-
}
206+
208207
auto *Info = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)calloc(1, Len);
209208
R = ::GetLogicalProcessorInformationEx(Relationship, Info, &Len);
210209
if (R) {

0 commit comments

Comments
 (0)