Python readline hooks and threading
jamcguir at yahoo.com
Mon Feb 4 17:15:18 EST 2002
I'm a bit confused about how the readline module (Modules/readline.c)
is supposed to work, especially with regards to threading. It *looks*
like the code is broken, and my own testing seems to bear this out,
but I think I may just be confused.
In the most recent version of readline.c , there is a comment in
on_hook() that says "Note that readline is called with the interpreter
lock released", followed immediately by a call to
PyThreadState_Swap(). The Python documentation says that the lock
must be held during any calls to PyThreadState_Swap, and looking at
the source shows that PyThreadState_Swap is indeed not thread-safe,
accessing a global variable without any locking.
In my application (multi-threaded, with all threads using C extension
functions and one thread using readline hooks) I was getting all kinds
of tstate mix-up errors. Debugging showed that although readline is
indeed always called inside of a Py_BEGIN_ALLOW_THREADS block,
sometimes the call to PyThreadState_Swap() returned a non-null value.
Commenting out the calls to PyThreadState_Swap() in on_hook seemed to
make these go away.
Is this a bug in the current Python distributions, or am I just on
More information about the Python-list