]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130000: Release the GIL in winreg when doing Windows API calls (GH-130001)
authorAN Long <aisk@users.noreply.github.com>
Thu, 15 May 2025 23:00:06 +0000 (08:00 +0900)
committerGitHub <noreply@github.com>
Thu, 15 May 2025 23:00:06 +0000 (00:00 +0100)
PC/winreg.c

index a8e2e4cc10c882917a762890084fb723270c745b..d1a1c3d1c9785098ded6585b9fc7c61152bed0cd 100644 (file)
@@ -426,7 +426,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;
@@ -499,14 +501,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");
@@ -924,7 +933,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;
@@ -973,8 +984,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;
@@ -1187,10 +1200,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 */
@@ -1477,9 +1492,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;
@@ -1587,7 +1604,9 @@ exit:
         PyMem_Free(pbuf);
     }
     if (childKey != key) {
+        Py_BEGIN_ALLOW_THREADS
         RegCloseKey(childKey);
+        Py_END_ALLOW_THREADS
     }
     return result;
 }
@@ -1625,7 +1644,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)
@@ -1637,8 +1658,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;