@@ -279,9 +279,11 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query)
279279 // a timeout. The initEvent will be set after COM initialization, it will
280280 // take a longer time when first initialized. The connectEvent will be set
281281 // after connected to WMI.
282- err = wait_event (data.initEvent , 1000 );
283282 if (!err) {
284- err = wait_event (data.connectEvent , 100 );
283+ err = wait_event (data.initEvent , 1000 );
284+ if (!err) {
285+ err = wait_event (data.connectEvent , 100 );
286+ }
285287 }
286288
287289 while (!err) {
@@ -305,28 +307,33 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query)
305307 CloseHandle (data.readPipe );
306308 }
307309
308- // Allow the thread some time to clean up
309- switch (WaitForSingleObject (hThread, 100 )) {
310- case WAIT_OBJECT_0:
311- // Thread ended cleanly
312- if (!GetExitCodeThread (hThread, (LPDWORD)&err)) {
313- err = GetLastError ();
314- }
315- break ;
316- case WAIT_TIMEOUT:
317- // Probably stuck - there's not much we can do, unfortunately
318- if (err == 0 || err == ERROR_BROKEN_PIPE) {
319- err = WAIT_TIMEOUT;
310+ if (hThread) {
311+ // Allow the thread some time to clean up
312+ int thread_err;
313+ switch (WaitForSingleObject (hThread, 100 )) {
314+ case WAIT_OBJECT_0:
315+ // Thread ended cleanly
316+ if (!GetExitCodeThread (hThread, (LPDWORD)&thread_err)) {
317+ thread_err = GetLastError ();
318+ }
319+ break ;
320+ case WAIT_TIMEOUT:
321+ // Probably stuck - there's not much we can do, unfortunately
322+ thread_err = WAIT_TIMEOUT;
323+ break ;
324+ default :
325+ thread_err = GetLastError ();
326+ break ;
320327 }
321- break ;
322- default :
328+ // An error on our side is more likely to be relevant than one from
329+ // the thread, but if we don't have one on our side we'll take theirs.
323330 if (err == 0 || err == ERROR_BROKEN_PIPE) {
324- err = GetLastError () ;
331+ err = thread_err ;
325332 }
326- break ;
333+
334+ CloseHandle (hThread);
327335 }
328336
329- CloseHandle (hThread);
330337 CloseHandle (data.initEvent );
331338 CloseHandle (data.connectEvent );
332339 hThread = NULL ;
0 commit comments