Skip to content

Commit 1f60715

Browse files
author
Jehan
committed
Fix usage of TerminateThread() causing critical section corruption.
This patch was submitted to the GIMP project by a publisher wishing to keep confidentiality (hence anonymously). I just pass along the patch. Here is the patch explanation which came with: First they remind us what Microsoft documentation says about TerminateThread: > TerminateThread is a dangerous function that should only be used in > the most extreme cases. You should call TerminateThread only if you > know exactly what the target thread is doing, and you control all of > the code that the target thread could possibly be running at the time > of the termination. (https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminatethread) Then they say that 5 milliseconds time-out might not be long enough for the thread to exit gracefully. They propose to set it to a much higher value (for instance here 5 seconds). And finally you should always check the return value of WaitForSingleObject(). In particular you want to run TerminateThread() only if WaitForSingleObject() failed, not on success case.
1 parent 73128f3 commit 1f60715

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

driver/others/blas_server_win32.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,15 @@ int BLASFUNC(blas_thread_shutdown)(void){
462462

463463
for(i = 0; i < blas_num_threads - 1; i++){
464464
// Could also just use WaitForMultipleObjects
465-
WaitForSingleObject(blas_threads[i], 5); //INFINITE);
465+
DWORD wait_thread_value = WaitForSingleObject(blas_threads[i], 5000);
466+
466467
#ifndef OS_WINDOWSSTORE
467-
// TerminateThread is only available with WINAPI_DESKTOP and WINAPI_SYSTEM not WINAPI_APP in UWP
468-
TerminateThread(blas_threads[i],0);
468+
// TerminateThread is only available with WINAPI_DESKTOP and WINAPI_SYSTEM not WINAPI_APP in UWP
469+
if (WAIT_OBJECT_0 != wait_thread_value) {
470+
TerminateThread(blas_threads[i],0);
471+
}
469472
#endif
473+
470474
CloseHandle(blas_threads[i]);
471475
}
472476

0 commit comments

Comments
 (0)