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
Call _PyGILState_Init() earlier in Py_InitializeEx() #52310
Comments
_PyGILState_Init() initialize autoInterpreterState variable. This variable have to be set before the first call to PyGILState_Ensure(). The problem is that _PyGILState_Init() is called late: at the end of Py_InitializeEx(). It's called after initsite(), whereas initsite() may need to call _PyGILState_Init(). Example: add the following lines at the end of site.py: import readline
import rlcompleter
readline.parse_and_bind("tab: complete")
raw_input("press TAB") Run an interpreter and press TAB: $ ./python
press TABpython: Python/pystate.c:595: PyGILState_Ensure: Assertion `autoInterpreterState' failed.
Abandon Other example of functiions using _PyGILState_Init(): _PyObject_Dump() (useful for debug), sqlite module, os.popen*(), ctypes Python callbacks, etc. I don't know the right place for _PyGILState_Init() in Py_InitializeEx(). _PyGILState_Init() calls the following functions:
None of these function use Python functions, so _PyGILState_Init() can be called very early. The earliest position is just after the call to PyThreadState_New(), before PyThreadState_Swap(). It tested it and it works correctly. If _PyGILState_Init() is called before PyThreadState_Swap(), PyThreadState_Swap() can be simplified (it doesn't need to check if GIL is initialized or not). Attached patch implement that. -- I hit this bug when I was debuging Python: I added a call to _PyObject_Dump() which stopped Python (assertion error) because the GIL was not initialized :-/ I already hitted this bug some weeks ago when I was working on the thread state preallocation when creating a new thread: bpo-7544. |
Bootstrap issues are always quite delicate, and I'd suggest being careful in this case. |
See also bpo-3137. |
I would keep the call to PyThreadState_Swap() next to PyThreadState_New(): create the thread state and install it. _PyGILState_Init() may come after. |
haypo, it seems you removed the initial message... |
I know... My mouse clicked on [remove] button, it wasn't me! I don't know how to restore it. The message: msg100432. |
I found the following issue in Roundup tracker to restore a deleted message: I tried to write the URL, but it doesn't work: I tried also using Poster Firefox extension (to post a POST request, not a GET request), but it doesn't work. My URL should be wrong :-/ |
My SIGINT.patch for bpo-3137 moves the call to _PyGILState_Init() just before initsite(), so it doesn't change too much Py_InitializeEx() and it's enough for me :-) |
While working on bpo-9425, I usually hit two annoying issues:
So I'm back on the GIL topic: I still would like to initialize the GIL earlier in Py_InitializeEx(). As Amaury wrote, I think that the right place is just after "(void) PyThreadState_Swap(tstate);". This is exactly what does my new patch (for py3k). I think that only python 3.2 should be patched. |
Commited as r84163 to 3.2. Don't backport because it is not really a bug and I prefer to avoid touching stable branches with such minor details. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: