Skip to content

Commit 60c415b

Browse files
authored
gh-125315: Avoid crashing in _wmimodule due to slow WMI calls on some Windows machines (GH-126141)
1 parent c29bbe2 commit 60c415b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid crashing in :mod:`platform` due to slow WMI calls on some Windows
2+
machines.

PC/_wmimodule.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,26 @@ _query_thread(LPVOID param)
5555
IWbemLocator *locator = NULL;
5656
IWbemServices *services = NULL;
5757
IEnumWbemClassObject* enumerator = NULL;
58+
HRESULT hr = S_OK;
5859
BSTR bstrQuery = NULL;
5960
struct _query_data *data = (struct _query_data*)param;
6061

61-
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
62+
// gh-125315: Copy the query string first, so that if the main thread gives
63+
// up on waiting we aren't left with a dangling pointer (and a likely crash)
64+
bstrQuery = SysAllocString(data->query);
65+
if (!bstrQuery) {
66+
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
67+
}
68+
69+
if (SUCCEEDED(hr)) {
70+
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
71+
}
72+
6273
if (FAILED(hr)) {
6374
CloseHandle(data->writePipe);
75+
if (bstrQuery) {
76+
SysFreeString(bstrQuery);
77+
}
6478
return (DWORD)hr;
6579
}
6680

@@ -101,12 +115,6 @@ _query_thread(LPVOID param)
101115
NULL, EOAC_NONE
102116
);
103117
}
104-
if (SUCCEEDED(hr)) {
105-
bstrQuery = SysAllocString(data->query);
106-
if (!bstrQuery) {
107-
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
108-
}
109-
}
110118
if (SUCCEEDED(hr)) {
111119
hr = services->ExecQuery(
112120
bstr_t("WQL"),

0 commit comments

Comments
 (0)