Skip to content

Commit 7a504b3

Browse files
authored
pythongh-130000: Release the GIL in winreg when doing Windows API calls (pythonGH-130001)
1 parent 6a22963 commit 7a504b3

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

PC/winreg.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@ PyHKEY_Close(winreg_state *st, PyObject *ob_handle)
426426
if (PyHKEY_Check(st, ob_handle)) {
427427
((PyHKEYObject*)ob_handle)->hkey = 0;
428428
}
429+
Py_BEGIN_ALLOW_THREADS
429430
rc = key ? RegCloseKey(key) : ERROR_SUCCESS;
431+
Py_END_ALLOW_THREADS
430432
if (rc != ERROR_SUCCESS)
431433
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
432434
return rc == ERROR_SUCCESS;
@@ -499,14 +501,21 @@ PyWinObject_CloseHKEY(winreg_state *st, PyObject *obHandle)
499501
}
500502
#if SIZEOF_LONG >= SIZEOF_HKEY
501503
else if (PyLong_Check(obHandle)) {
502-
long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
504+
long rc;
505+
Py_BEGIN_ALLOW_THREADS
506+
rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
507+
Py_END_ALLOW_THREADS
503508
ok = (rc == ERROR_SUCCESS);
504509
if (!ok)
505510
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
506511
}
507512
#else
508513
else if (PyLong_Check(obHandle)) {
509-
long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
514+
long rc;
515+
HKEY hkey = (HKEY)PyLong_AsVoidPtr(obHandle);
516+
Py_BEGIN_ALLOW_THREADS
517+
rc = RegCloseKey(hkey);
518+
Py_END_ALLOW_THREADS
510519
ok = (rc == ERROR_SUCCESS);
511520
if (!ok)
512521
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
@@ -924,7 +933,9 @@ winreg_CreateKey_impl(PyObject *module, HKEY key, const wchar_t *sub_key)
924933
(Py_ssize_t)KEY_WRITE) < 0) {
925934
return NULL;
926935
}
936+
Py_BEGIN_ALLOW_THREADS
927937
rc = RegCreateKeyW(key, sub_key, &retKey);
938+
Py_END_ALLOW_THREADS
928939
if (rc != ERROR_SUCCESS) {
929940
PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
930941
return NULL;
@@ -973,8 +984,10 @@ winreg_CreateKeyEx_impl(PyObject *module, HKEY key, const wchar_t *sub_key,
973984
(Py_ssize_t)access) < 0) {
974985
return NULL;
975986
}
987+
Py_BEGIN_ALLOW_THREADS
976988
rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
977989
access, NULL, &retKey, NULL);
990+
Py_END_ALLOW_THREADS
978991
if (rc != ERROR_SUCCESS) {
979992
PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
980993
return NULL;
@@ -1187,10 +1200,12 @@ winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
11871200
(Py_ssize_t)key, index) < 0) {
11881201
return NULL;
11891202
}
1190-
if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
1191-
NULL,
1192-
&retValueSize, &retDataSize, NULL, NULL))
1193-
!= ERROR_SUCCESS)
1203+
1204+
Py_BEGIN_ALLOW_THREADS
1205+
rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1206+
&retValueSize, &retDataSize, NULL, NULL);
1207+
Py_END_ALLOW_THREADS
1208+
if (rc != ERROR_SUCCESS)
11941209
return PyErr_SetFromWindowsErrWithFunction(rc,
11951210
"RegQueryInfoKey");
11961211
++retValueSize; /* include null terminators */
@@ -1477,9 +1492,11 @@ winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
14771492
if (PySys_Audit("winreg.QueryInfoKey", "n", (Py_ssize_t)key) < 0) {
14781493
return NULL;
14791494
}
1480-
if ((rc = RegQueryInfoKeyW(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1481-
&nValues, NULL, NULL, NULL, &ft))
1482-
!= ERROR_SUCCESS) {
1495+
Py_BEGIN_ALLOW_THREADS
1496+
rc = RegQueryInfoKeyW(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1497+
&nValues, NULL, NULL, NULL, &ft);
1498+
Py_END_ALLOW_THREADS
1499+
if (rc != ERROR_SUCCESS) {
14831500
return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
14841501
}
14851502
li.LowPart = ft.dwLowDateTime;
@@ -1587,7 +1604,9 @@ winreg_QueryValue_impl(PyObject *module, HKEY key, const wchar_t *sub_key)
15871604
PyMem_Free(pbuf);
15881605
}
15891606
if (childKey != key) {
1607+
Py_BEGIN_ALLOW_THREADS
15901608
RegCloseKey(childKey);
1609+
Py_END_ALLOW_THREADS
15911610
}
15921611
return result;
15931612
}
@@ -1625,7 +1644,9 @@ winreg_QueryValueEx_impl(PyObject *module, HKEY key, const wchar_t *name)
16251644
(Py_ssize_t)key, NULL, name) < 0) {
16261645
return NULL;
16271646
}
1647+
Py_BEGIN_ALLOW_THREADS
16281648
rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
1649+
Py_END_ALLOW_THREADS
16291650
if (rc == ERROR_MORE_DATA)
16301651
bufSize = 256;
16311652
else if (rc != ERROR_SUCCESS)
@@ -1637,8 +1658,10 @@ winreg_QueryValueEx_impl(PyObject *module, HKEY key, const wchar_t *name)
16371658

16381659
while (1) {
16391660
retSize = bufSize;
1661+
Py_BEGIN_ALLOW_THREADS
16401662
rc = RegQueryValueExW(key, name, NULL, &typ,
16411663
(BYTE *)retBuf, &retSize);
1664+
Py_END_ALLOW_THREADS
16421665
if (rc != ERROR_MORE_DATA)
16431666
break;
16441667

0 commit comments

Comments
 (0)