-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
PyOS_Readline drops GIL and calls PyOS_StdioReadline, which isn't thread safe #60946
Comments
Relevant thread: http://mail.python.org/pipermail/python-dev/2012-December/123225.html PyOS_StdioReadline features numerous calls that require the GIL to be held. Ideally, the GIL drop-take should be moved closer to the actual underlying read system call. |
So, could you propose a patch? |
My quick and dirty fix is simple: _PyOS_ReadlineTState = PyThreadState_GET();
/* CCP change, cannot release the GIL here because PyOS_StdioReadline uses
* the regular MALLOC
*/
/*
Py_BEGIN_ALLOW_THREADS
*/
#ifdef WITH_THREAD
PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
#endif
#ifdef WITH_THREAD
PyThread_release_lock(_PyOS_ReadlineLock);
#endif Basically, we just comment out the lock release since we don't need it. The reason we found this was that we were using GIL a custom mallocator which should have been run with the GIL but wasn´t. |
I just found the readline/GIL issue while working on bpo-18203. I created bpo-18205 but then I found this issue. I just closed bpo-18205 as a duplicate. Here is a patch for Python 3.4. -- Copy of the initial message (msg191089): The callback PyOS_ReadlineFunctionPointer (used to read a line from the standard input) must return a buffer allocated by PyMem_Malloc(), but PyOS_Readline() releases the GIL before calling PyOS_ReadlineFunctionPointer. Simplified extract of PyOS_Readline():
tok_nextc() calls PyOS_Readline() and calls PyMem_FREE() to release its result. PyOS_ReadlineFunctionPointer should allocate memory using malloc(), not using PyMem_Malloc(). But PyOS_Readline() should copy the line into a buffer allocated by PyMem_Malloc() to keep backward compatibility. |
See the following thread on python-dev, the root problem is that PyMem_Malloc() cannot be called with the GIL held. This is a bug in my opinion, and it should be fixed. |
Updated patch for the final API of bpo-3329. Update also the documentation. PyOS_ReadlineFunctionPointer must now use PyMem_RawMalloc() or PyMem_RawRealloc(), instead of PyMem_Malloc() or PyMem_Realloc(). |
New changeset 98dbe677dfe7 by Victor Stinner in branch 'default': |
New changeset 6c9050ad1afc by Victor Stinner in branch 'default': |
Perhaps in debug builds the memory apis should verify consistency and matching useage. |
Python does check usage of apis in debug mode. Memory allocation failure |
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: