8000 gh-125315: Avoid crashing in _wmimodule due to slow WMI calls on some Windows machines by zooba · Pull Request #126141 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-125315: Avoid crashing in _wmimodule due to slow WMI calls on some Windows machines #126141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid crashing in :mod:`platform` due to slow WMI calls on some Windows
machines.
22 changes: 15 additions & 7 deletions PC/_wmimodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,26 @@ _query_thread(LPVOID param)
IWbemLocator *locator = NULL;
IWbemServices *services = NULL;
IEnumWbemClassObject* enumerator = NULL;
HRESULT hr = S_OK;
BSTR bstrQuery = NULL;
struct _query_data *data = (struct _query_data*)param;

HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
// gh-125315: Copy the query string first, so that if the main thread gives
// up on waiting we aren't left with a dangling pointer (and a likely crash)
bstrQuery = SysAllocString(data->query);
if (!bstrQuery) {
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
}

if (SUCCEEDED(hr)) {
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
}

if (FAILED(hr)) {
CloseHandle(data->writePipe);
if (bstrQuery) {
SysFreeString(bstrQuery);
}
return (DWORD)hr;
}

Expand Down Expand Up @@ -101,12 +115,6 @@ _query_thread(LPVOID param)
NULL, EOAC_NONE
);
}
if (SUCCEEDED(hr)) {
bstrQuery = SysAllocString(data->query);
if (!bstrQuery) {
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
}
}
if (SUCCEEDED(hr)) {
hr = services->ExecQuery(
bstr_t("WQL"),
Expand Down
Loading
0