Skip to content

gh-130000: Release the GIL in winreg when doing Windows API calls #130001

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 2 commits into from
May 15, 2025

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant

Merged
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
41 changes: 32 additions & 9 deletions PC/winreg.c
Original file line number Diff line number Diff line change
@@ -428,7 +428,9 @@ PyHKEY_Close(winreg_state *st, PyObject *ob_handle)
if (PyHKEY_Check(st, ob_handle)) {
((PyHKEYObject*)ob_handle)->hkey = 0;
}
Py_BEGIN_ALLOW_THREADS
rc = key ? RegCloseKey(key) : ERROR_SUCCESS;
Py_END_ALLOW_THREADS
if (rc != ERROR_SUCCESS)
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
return rc == ERROR_SUCCESS;
@@ -501,14 +503,21 @@ PyWinObject_CloseHKEY(winreg_state *st, PyObject *obHandle)
}
#if SIZEOF_LONG >= SIZEOF_HKEY
else if (PyLong_Check(obHandle)) {
long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
long rc;
Py_BEGIN_ALLOW_THREADS
rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
Py_END_ALLOW_THREADS
ok = (rc == ERROR_SUCCESS);
if (!ok)
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
}
#else
else if (PyLong_Check(obHandle)) {
long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
long rc;
HKEY hkey = (HKEY)PyLong_AsVoidPtr(obHandle);
Py_BEGIN_ALLOW_THREADS
rc = RegCloseKey(hkey);
Py_END_ALLOW_THREADS
ok = (rc == ERROR_SUCCESS);
if (!ok)
PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
@@ -926,7 +935,9 @@ winreg_CreateKey_impl(PyObject *module, HKEY key, const wchar_t *sub_key)
(Py_ssize_t)KEY_WRITE) < 0) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
rc = RegCreateKeyW(key, sub_key, &retKey);
Py_END_ALLOW_THREADS
if (rc != ERROR_SUCCESS) {
PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
return NULL;
@@ -975,8 +986,10 @@ winreg_CreateKeyEx_impl(PyObject *module, HKEY key, const wchar_t *sub_key,
(Py_ssize_t)access) < 0) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
access, NULL, &retKey, NULL);
Py_END_ALLOW_THREADS
if (rc != ERROR_SUCCESS) {
PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
return NULL;
@@ -1189,10 +1202,12 @@ winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
(Py_ssize_t)key, index) < 0) {
return NULL;
}
if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
NULL,
&retValueSize, &retDataSize, NULL, NULL))
!= ERROR_SUCCESS)

Py_BEGIN_ALLOW_THREADS
rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&retValueSize, &retDataSize, NULL, NULL);
Py_END_ALLOW_THREADS
if (rc != ERROR_SUCCESS)
return PyErr_SetFromWindowsErrWithFunction(rc,
"RegQueryInfoKey");
++retValueSize; /* include null terminators */
@@ -1479,9 +1494,11 @@ winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
if (PySys_Audit("winreg.QueryInfoKey", "n", (Py_ssize_t)key) < 0) {
return NULL;
}
if ((rc = RegQueryInfoKeyW(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
&nValues, NULL, NULL, NULL, &ft))
!= ERROR_SUCCESS) {
Py_BEGIN_ALLOW_THREADS
rc = RegQueryInfoKeyW(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
&nValues, NULL, NULL, NULL, &ft);
Py_END_ALLOW_THREADS
if (rc != ERROR_SUCCESS) {
return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
}
li.LowPart = ft.dwLowDateTime;
@@ -1589,7 +1606,9 @@ winreg_QueryValue_impl(PyObject *module, HKEY key, const wchar_t *sub_key)
PyMem_Free(pbuf);
}
if (childKey != key) {
Py_BEGIN_ALLOW_THREADS
RegCloseKey(childKey);
Py_END_ALLOW_THREADS
}
return result;
}
@@ -1627,7 +1646,9 @@ winreg_QueryValueEx_impl(PyObject *module, HKEY key, const wchar_t *name)
(Py_ssize_t)key, NULL, name) < 0) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
Py_END_ALLOW_THREADS
if (rc == ERROR_MORE_DATA)
bufSize = 256;
else if (rc != ERROR_SUCCESS)
@@ -1639,8 +1660,10 @@ winreg_QueryValueEx_impl(PyObject *module, HKEY key, const wchar_t *name)

while (1) {
retSize = bufSize;
Py_BEGIN_ALLOW_THREADS
rc = RegQueryValueExW(key, name, NULL, &typ,
(BYTE *)retBuf, &retSize);
Py_END_ALLOW_THREADS
if (rc != ERROR_MORE_DATA)
break;

Loading
Oops, something went wrong.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant