8000 GH-130727: Avoid race condition in _wmimodule by copying shared data … · lkollar/cpython@9ae095a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ae095a

Browse files
chris-eibllkollar
authored andcommitted
pythonGH-130727: Avoid race condition in _wmimodule by copying shared data (pythonGH-134313)
1 parent 95d1558 commit 9ae095a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a race in internal calls into WMI that can result in an "invalid handle"
2+
exception under high load. Patch by Chris Eibl.

PC/_wmimodule.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ _query_thread(LPVOID param)
5757
IEnumWbemClassObject* enumerator = NULL;
5858
HRESULT hr = S_OK;
5959
BSTR bstrQuery = NULL;
60-
struct _query_data *data = (struct _query_data*)param;
60+
_query_data data = *(struct _query_data*)param;
6161

6262
// gh-125315: Copy the query string first, so that if the main thread gives
6363
// up on waiting we aren't left with a dangling pointer (and a likely crash)
64-
bstrQuery = SysAllocString(data->query);
64+
bstrQuery = SysAllocString(data.query);
6565
if (!bstrQuery) {
6666
hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
6767
}
@@ -71,7 +71,7 @@ _query_thread(LPVOID param)
7171
}
7272

7373
if (FAILED(hr)) {
74-
CloseHandle(data->writePipe);
74+
CloseHandle(data.writePipe);
7575
if (bstrQuery) {
7676
SysFreeString(bstrQuery);
7777
}
@@ -96,7 +96,7 @@ _query_thread(LPVOID param)
9696
IID_IWbemLocator, (LPVOID *)&locator
9797
);
9898
}
99-
if (SUCCEEDED(hr) && !SetEvent(data->initEvent)) {
99+
if (SUCCEEDED(hr) && !SetEvent(data.initEvent)) {
100100
hr = HRESULT_FROM_WIN32(GetLastError());
101101
}
102102
if (SUCCEEDED(hr)) {
@@ -105,7 +105,7 @@ _query_thread(LPVOID param)
105105
NULL, NULL, 0, NULL, 0, 0, &services
106106
);
107107
}
108-
if (SUCCEEDED(hr) && !SetEvent(data->connectEvent)) {
108+
if (SUCCEEDED(hr) && !SetEvent(data.connectEvent)) {
109109
hr = HRESULT_FROM_WIN32(GetLastError());
110110
}
111111
if (SUCCEEDED(hr)) {
@@ -143,7 +143,7 @@ _query_thread(LPVOID param)
143143
if (FAILED(hr) || got != 1 || !value) {
144144
continue;
145145
}
146-
if (!startOfEnum && !WriteFile(data->writePipe, (LPVOID)L"\0", 2, &written, NULL)) {
146+
if (!startOfEnum && !WriteFile(data.writePipe, (LPVOID)L"\0", 2, &written, NULL)) {
147147
hr = HRESULT_FROM_WIN32(GetLastError());
148148
break;
149149
}
@@ -171,10 +171,10 @@ _query_thread(LPVOID param)
171171
DWORD cbStr1, cbStr2;
172172
cbStr1 = (DWORD)(wcslen(propName) * sizeof(propName[0]));
173173
cbStr2 = (DWORD)(wcslen(propStr) * sizeof(propStr[0]));
174-
if (!WriteFile(data->writePipe, propName, cbStr1, &written, NULL) ||
175-
!WriteFile(data->writePipe, (LPVOID)L"=", 2, &written, NULL) ||
176-
!WriteFile(data->writePipe, propStr, cbStr2, &written, NULL) ||
177-
!WriteFile(data->writePipe, (LPVOID)L"\0", 2, &written, NULL)
174+
if (!WriteFile(data.writePipe, propName, cbStr1, &written, NULL) ||
175+
!WriteFile(data.writePipe, (LPVOID)L"=", 2, &written, NULL) ||
176+
!WriteFile(data.writePipe, propStr, cbStr2, &written, NULL) ||
177+
!WriteFile(data.writePipe, (LPVOID)L"\0", 2, &written, NULL)
178178
) {
179179
hr = HRESULT_FROM_WIN32(GetLastError());
180180
}
@@ -200,7 +200,7 @@ _query_thread(LPVOID param)
200200
locator->Release();
201201
}
202202
CoUninitialize();
203-
CloseHandle(data->writePipe);
203+
CloseHandle(data.writePipe);
204204
return (DWORD)hr;
205205
}
206206

0 commit comments

Comments
 (0)
0