gh-112278: Improve error handling in wmi module and tests (GH-117818)

This commit is contained in:
Steve Dower 2024-04-15 15:43:11 +01:00 committed by GitHub
parent 185999bb3a
commit 7d9d6b53bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 21 deletions

View File

@ -14,6 +14,8 @@ def wmi_exec_query(query):
# gh-112278: WMI maybe slow response when first call. # gh-112278: WMI maybe slow response when first call.
try: try:
return _wmi.exec_query(query) return _wmi.exec_query(query)
except BrokenPipeError:
pass
except WindowsError as e: except WindowsError as e:
if e.winerror != 258: if e.winerror != 258:
raise raise

View File

@ -279,10 +279,12 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query)
// a timeout. The initEvent will be set after COM initialization, it will // a timeout. The initEvent will be set after COM initialization, it will
// take a longer time when first initialized. The connectEvent will be set // take a longer time when first initialized. The connectEvent will be set
// after connected to WMI. // after connected to WMI.
if (!err) {
err = wait_event(data.initEvent, 1000); err = wait_event(data.initEvent, 1000);
if (!err) { if (!err) {
err = wait_event(data.connectEvent, 100); err = wait_event(data.connectEvent, 100);
} }
}
while (!err) { while (!err) {
if (ReadFile( if (ReadFile(
@ -305,28 +307,33 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query)
CloseHandle(data.readPipe); CloseHandle(data.readPipe);
} }
if (hThread) {
// Allow the thread some time to clean up // Allow the thread some time to clean up
int thread_err;
switch (WaitForSingleObject(hThread, 100)) { switch (WaitForSingleObject(hThread, 100)) {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
// Thread ended cleanly // Thread ended cleanly
if (!GetExitCodeThread(hThread, (LPDWORD)&err)) { if (!GetExitCodeThread(hThread, (LPDWORD)&thread_err)) {
err = GetLastError(); thread_err = GetLastError();
} }
break; break;
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
// Probably stuck - there's not much we can do, unfortunately // Probably stuck - there's not much we can do, unfortunately
if (err == 0 || err == ERROR_BROKEN_PIPE) { thread_err = WAIT_TIMEOUT;
err = WAIT_TIMEOUT;
}
break; break;
default: default:
if (err == 0 || err == ERROR_BROKEN_PIPE) { thread_err = GetLastError();
err = GetLastError();
}
break; break;
} }
// An error on our side is more likely to be relevant than one from
// the thread, but if we don't have one on our side we'll take theirs.
if (err == 0 || err == ERROR_BROKEN_PIPE) {
err = thread_err;
}
CloseHandle(hThread); CloseHandle(hThread);
}
CloseHandle(data.initEvent); CloseHandle(data.initEvent);
CloseHandle(data.connectEvent); CloseHandle(data.connectEvent);
hThread = NULL; hThread = NULL;