[Python-Dev] Bug in generator if the generator in created in a C thread (original) (raw)

Guido van Rossum guido at python.org
Wed Mar 28 16:43:27 CEST 2012


Interesting bug. :-(

It seems bugs.python.org is back up, so can you file it there too?

On Wed, Mar 28, 2012 at 3:45 AM, Victor Stinner <victor.stinner at gmail.com> wrote:

Hi,

bugs.python.org is down so I'm reporting the bug here :-) We have a crash in our product when tracing is enabled by sys.settrace() and threading.settrace(). If a Python generator is created in a C thread, calling the generator later in another thread may crash if Python tracing is enabled.  - the C thread calls PyGILStateEnsure() which creates a temporary Python thread state  - a generator is created, the generator has a reference to a Python frame which keeps a reference to the temporary Python thread state  - the C thread calls PyGILStateReleases() which destroys the temporary Python thread state  - when the generator is called later in another thread, calltrace() reads the Python thread state from the generator frame, which is the destroyed frame => it does crash on a pointer dereference if the memory was reallocated (by malloc()) and the data were erased To reproduce the crash, unpack the attached generatorframebug.tar.gz, compile the C module using "python setup.py build" and then run "PYTHONPATH=$(ls -d build/lib*/) python test.py" (or just "python test.py if you installed the test module). You may need to use Valgrind to see the error, or call memset(tstate, 0xFF, sizeof(*tstate)) before free(tstate) in tstatedeletecommon(). Calling the generator should update its reference to the Python state thread in its frame. The generator may also clears frame->ftstate (to detect bugs earlier), as it does for frame->fback (to avoid a reference cycle). Attached patch implements this fix for Python 3.3. Victor


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org

-- --Guido van Rossum (python.org/~guido)



More information about the Python-Dev mailing list