This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Should not release GIL while running RegEnumValue
Type: crash Stage: resolved
Components: Extension Modules, Windows Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, ocean-city, steve.dower, stutzbach, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2010-10-12 11:43 by ocean-city, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg118416 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-12 11:57
Currently, PC/winreg.c releases GIL while calling registry
API, but I found this in Remarks section of RegEnumValue.

http://msdn.microsoft.com/en-us/library/ms724865%28VS.85%29.aspx

> While using RegEnumValue, an application should not call any registry
> functions that might change the key being queried.

Maybe we shouldn't release GIL in PC/winreg.c? Thank you.
msg118493 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-10-13 00:35
Some of your submission appears to have gotten lost (I saw it via email).
Below is the patch you proposed. I haven't experienced this crash on my machines, but the solution seems fine to me.


# I sometimes experienced crash of test_changing_value(test_winreg)
# on release27-maint. It happens when 2 threads calls PyEnumValue and
# PySetValue simultaneously. And I could stop it by following patch.
# I'll attach the stack trace of crash.

Index: PC/_winreg.c
===================================================================
--- PC/_winreg.c        (revision 85344)
+++ PC/_winreg.c        (working copy)
@@ -1219,7 +1219,6 @@
    }

    while (1) {
-        Py_BEGIN_ALLOW_THREADS
        rc = RegEnumValue(hKey,
                  index,
                  retValueBuf,
@@ -1228,7 +1227,6 @@
                  &typ,
                  (BYTE *)retDataBuf,
                  &retDataSize);
-        Py_END_ALLOW_THREADS

        if (rc != ERROR_MORE_DATA)
            break;
@@ -1577,9 +1575,7 @@
        if (subKey == NULL)
            return NULL;
    }
-    Py_BEGIN_ALLOW_THREADS
    rc = RegSetValue(hKey, subKey, REG_SZ, str, len+1);
-    Py_END_ALLOW_THREADS
    if (rc != ERROR_SUCCESS)
        return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
    Py_INCREF(Py_None);
msg118499 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-10-13 02:43
Hirokazu, could you run Python in a debugger and figure out exactly which line crashes and what the error is?  I'm curious.

If we make this change, the same change should be applied to RegEnumKey, etc., since the RegEnumKey docs contain similar language.
msg118508 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-13 09:55
One thread is exactly on RegEnumValue
or RegQueryValue, and another thread is a bit after
RegSetValue (the place varies case by case). Type is
SEGV.
msg192649 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-08 13:11
LGTM

I suggest that you add a comment with a link to this issue. People may wonder why some places don't release the GIL.
msg224597 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-08-03 00:06
Apart from the request for a comment made in msg192649 it looks as if  this can be commited.
msg224732 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2014-08-04 15:21
I don't think this is an appropriate fix, since in most cases there is no need to prevent other Python threads running while inside RegSetValue. There are also other ways that a context switch may occur during the enumeration which will put the program in exactly the same state. (It isn't clear from the patch, but the two sections of changed code are from completely different functions.)

The correct fix should be at the user's application level, or in a higher-level module than _winreg is supposed to be.
msg404253 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2021-10-19 00:11
With Steve's opposition and the fact that we've gotten along for 11 years since this issue was opened without it (and also without further reports of issues), I'm going to go ahead and close the issue.
History
Date User Action Args
2022-04-11 14:57:07adminsetgithub: 54280
2021-10-19 00:11:04zach.waresetstatus: open -> closed
resolution: rejected
messages: + msg404253

stage: patch review -> resolved
2019-04-26 20:24:22BreamoreBoysetnosy: - BreamoreBoy
2014-08-04 15:21:07steve.dowersetmessages: + msg224732
2014-08-03 00:06:28BreamoreBoysetnosy: + BreamoreBoy, tim.golden, zach.ware, steve.dower, - brian.curtin

messages: + msg224597
versions: + Python 3.5, - Python 3.3
2013-07-08 13:11:59christian.heimessetnosy: + christian.heimes

messages: + msg192649
versions: + Python 3.3, Python 3.4, - Python 3.1, Python 3.2
2010-10-13 09:55:40ocean-citysetmessages: + msg118508
2010-10-13 09:47:06ocean-citysetmessages: - msg118507
2010-10-13 09:36:52ocean-citysetmessages: + msg118507
2010-10-13 02:43:15stutzbachsetmessages: + msg118499
2010-10-13 00:35:05brian.curtinsetnosy: + stutzbach, brian.curtin
messages: + msg118493

components: + Windows
type: crash
stage: patch review
2010-10-12 11:57:30ocean-citysetstatus: closed -> open

messages: + msg118416
2010-10-12 11:56:58ocean-citysetstatus: open -> closed
2010-10-12 11:56:28ocean-cityset -> (no value)
messages: - msg118414
2010-10-12 11:56:16ocean-citysetfiles: - py27_test_winreg_crash_stack_trace.txt
2010-10-12 11:48:19ocean-citysetmessages: + msg118414
2010-10-12 11:46:17ocean-cityset -> (no value)
messages: - msg118413
2010-10-12 11:43:22ocean-citycreate