diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-14 12:49:26 -0800 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-14 12:49:26 -0800 |
commit | bc0168ad4a5467ab369f120e646fda811175b210 (patch) | |
tree | 152c14a8720bc83da5db3c4fdc91d70cab9d51f2 | |
parent | 346999344bf94084e9bed09457abe18260d3bfd1 (diff) | |
download | psutil-bc0168ad4a5467ab369f120e646fda811175b210.tar.gz |
properly cleanup C thread
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | psutil/_psutil_windows.c | 7 | ||||
-rw-r--r-- | psutil/arch/windows/process_handles.c | 35 | ||||
-rw-r--r-- | psutil/tests/__init__.py | 2 | ||||
-rwxr-xr-x | scripts/internal/winmake.py | 2 |
5 files changed, 37 insertions, 13 deletions
@@ -256,11 +256,11 @@ print-timeline: ## Print releases' timeline. print-access-denied: ## Print AD exceptions ${MAKE} install - @$(PYTHON) scripts/internal/print_access_denied.py + @$(TEST_PREFIX) $(PYTHON) scripts/internal/print_access_denied.py print-api-speed: ## Benchmark all API calls ${MAKE} install - @$(PYTHON) scripts/internal/print_api_speed.py $(ARGS) + @$(TEST_PREFIX) $(PYTHON) scripts/internal/print_api_speed.py $(ARGS) # =================================================================== # Misc diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 3bd07f68..9985e1d3 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -581,7 +581,7 @@ psutil_GetProcWsetInformation( SIZE_T bufferSize; bufferSize = 0x8000; - buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize); + buffer = MALLOC_ZERO(bufferSize); while ((status = NtQueryVirtualMemory( hProcess, @@ -591,16 +591,15 @@ psutil_GetProcWsetInformation( bufferSize, NULL)) == STATUS_INFO_LENGTH_MISMATCH) { - HeapFree(GetProcessHeap(), 0, buffer); + FREE(buffer); bufferSize *= 2; - psutil_debug("NtQueryVirtualMemory increase bufsize %i", bufferSize); // Fail if we're resizing the buffer to something very large. if (bufferSize > 256 * 1024 * 1024) { PyErr_SetString(PyExc_RuntimeError, "NtQueryVirtualMemory bufsize is too large"); return 1; } - buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufferSize); + buffer = MALLOC_ZERO(bufferSize); } if (!NT_SUCCESS(status)) { diff --git a/psutil/arch/windows/process_handles.c b/psutil/arch/windows/process_handles.c index 4b230595..e5dd74a8 100644 --- a/psutil/arch/windows/process_handles.c +++ b/psutil/arch/windows/process_handles.c @@ -166,20 +166,43 @@ psutil_threaded_get_filename(HANDLE hFile) { // If the thread hangs, kill it and cleanup. if (dwWait == WAIT_TIMEOUT) { psutil_debug( - "get file name thread timed out after %i ms", THREAD_TIMEOUT); - TerminateThread(hThread, 1); + "get handle name thread timed out after %i ms", THREAD_TIMEOUT); + if (TerminateThread(hThread, 0) == 0) { + PyErr_SetFromOSErrnoWithSyscall("TerminateThread"); + CloseHandle(hThread); + return 1; + } CloseHandle(hThread); return 0; } - else { - if (GetExitCodeThread(hThread, &threadRetValue) == 0) { + + if (dwWait == WAIT_FAILED) { + psutil_debug("WaitForSingleObject -> WAIT_FAILED"); + if (TerminateThread(hThread, 0) == 0) { + PyErr_SetFromOSErrnoWithSyscall( + "WaitForSingleObject -> WAIT_FAILED -> TerminateThread"); CloseHandle(hThread); - PyErr_SetFromOSErrnoWithSyscall("GetExitCodeThread"); return 1; } + PyErr_SetFromOSErrnoWithSyscall("WaitForSingleObject"); CloseHandle(hThread); - return threadRetValue; + return 1; + } + + if (GetExitCodeThread(hThread, &threadRetValue) == 0) { + if (TerminateThread(hThread, 0) == 0) { + PyErr_SetFromOSErrnoWithSyscall( + "GetExitCodeThread (failed) -> TerminateThread"); + CloseHandle(hThread); + return 1; + } + + CloseHandle(hThread); + PyErr_SetFromOSErrnoWithSyscall("GetExitCodeThread"); + return 1; } + CloseHandle(hThread); + return threadRetValue; } diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 3931f1c6..2844360d 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -127,7 +127,7 @@ NO_RETRIES = 10 # bytes tolerance for system-wide memory related tests MEMORY_TOLERANCE = 500 * 1024 # 500KB # the timeout used in functions which have to wait -GLOBAL_TIMEOUT = 3 +GLOBAL_TIMEOUT = 5 # be more tolerant if we're on travis / appveyor in order to avoid # false positives if TRAVIS or APPVEYOR: diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index 6349215a..ac08c03f 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -505,12 +505,14 @@ def bench_oneshot_2(): def print_access_denied(): """Print AD exceptions raised by all Process methods.""" install() + test_setup() sh("%s -Wa scripts\\internal\\print_access_denied.py" % PYTHON) def print_api_speed(): """Benchmark all API calls.""" install() + test_setup() sh("%s -Wa scripts\\internal\\print_api_speed.py" % PYTHON) |