[ python-Bugs-1041645 ] Thread management corrupts heap

SourceForge.net noreply at sourceforge.net
Thu Oct 7 03:57:00 CEST 2004


Bugs item #1041645, was opened at 2004-10-06 14:15
Message generated for change (Comment added) made by benson_basis
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1041645&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
Priority: 7
Submitted By: benson margulies (benson_basis)
Assigned to: Nobody/Anonymous (nobody)
Summary: Thread management corrupts heap

Initial Comment:
The PyGILState_Ensure mechanism appears to have a 
built-in heap-corrupting race condition.

If a new thread calls PyGILState_Ensure, then the code 
allocates a new 'tstate' for it on the heap. This 
allocation is not protected by any lock. So, multiple 
racing threads can hit the heap at the same time, and 
corrupt it.

I have observed this with both 2.3 and with 2.4a3.

I will attach a sample application. The application is 
Win32, but should be easy enough to adapt to Unix if 
someone cares to.

Since the stated purpose of this mechanism, in PEP311, 
is to allow any-old-thread to call into Python, I believe 
that the usage model here is legitimate.

To watch this explode, run the attached with arguments 
like, oh, 1 100 40 against the debug python build.




----------------------------------------------------------------------

>Comment By: benson margulies (benson_basis)
Date: 2004-10-06 21:57

Message:
Logged In: YES 
user_id=876734

The rather funny-looking initialization was recommended by 
the author of the PEP. Once InitThreads is called, the GIL is 
held. To get it unheld, you have to release it. Here's his 
explanation ..

OK - here is my explanation which you can adapt
 
A simple application is one that initializes Python, and then 
executes some Python code.  This Python code may itself 
cause other threads, or may indirectly cause other 
applications to "callback" into Python, but once the main 
thread returns, all the code has finished - you application 
terminates. With these applications, your init code need take 
no further action - just initialize Python and execute your 
code.  Only your extension functions or callback entry points 
need manage the GIL state.  python.exe is an example of this 
kind of app - it initializes Python, then executes the code 
specified on the command line.
 
More complex applications may need to initialize Python, 
perform a little bit of initialization, but then do nothing else on 
that thread.  An example would be where that initialization 
code bootstraps a few threads, then terminates immediately.  
The application however continues to live until explicitly 
terminated by the running code.
 
In this scenario, you must use the following code - this code 
calls into Python.  Once this call has been made, the thread 
still holds the GIL.  This thread must release the GIL before 
other threads can work.
 
[Technical aside: This works in the first case as the main 
thread which holds the lock continues to run.  When it calls 
out to external libraries and at periodic times during 
execution, the thread-lock is unlocked allowing other threads 
to run.  Once the main thread returns, it does still hold the 
lock - but as the application is terminating, that is OK - no 
other threads are competing for the lock.  In our complicated 
example, the main thread returning does *not* indicate 
application termination, so the lock must be manually 
dropped.]
 
That doesn't make a huge amount of sense, but I hope it is 
useful.


All this being said, I have two contradictory bits of evidence.

1) We all agree that the code, as written, would fail due to 
the unsafe debug malloc.

2) When I reverse the calls, it produces a very different 
situation. I think, perhaps, that the GIL never gets unlocked 
properly, and then instead of crashing everything just gets 
stuck somehow.

The change to call malloc seems pretty reasonable to me. 
Some tests seem called for in the tree on this. If order is not 
supposed to matter, the test should test both orders.




----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-06 20:46

Message:
Logged In: YES 
user_id=1038590

I'd suggest assigning the bug report to Tim Peters (aka
tim_one), to see if he still agrees with the fix he proposed
last month. 

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-06 20:43

Message:
Logged In: YES 
user_id=1038590

*rereads bug description and comments*

I think I'm on the right page now. The offending call would
appear to be PyMem_NEW inside PyThreadState_New.

Under a release build, this resolves into a direct call to
malloc(), but under debug it resolves to _PyObjectDebugMalloc.

Which is what you said in the bug report - I just read it
incorrectly. Sorry for the confusion.

I just remembered that this came up a few weeks ago on
Python dev, with a proposed solution (changing this
particular call to use malloc() directly):
http://mail.python.org/pipermail/python-dev/2004-September/048683.html



----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-06 20:15

Message:
Logged In: YES 
user_id=1038590

Disregard the previous comment regarding PyEval_InitThreads
and Py_Initialize - I was reading an older version of the
documentation which suggested the order of the calls mattered.

This does leak the constructed strings (no call to
Py_XDECREF(trash)), but that shouldn't cause anything too
catastrophic.

However, the call to PyGILState_Release without a preceding
call to PyGILState_Ensure looks dubious (it ends up deleting
the main thread's thread state, which seems unhealthy).

Do the same symptoms occur when using the
Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros
around the call to run_group()? (These macros are
recommended when you *know* you already have the GIL. The
PyGILState functions are for when you don't know if you have
the GIL or not)

----------------------------------------------------------------------

Comment By: benson margulies (benson_basis)
Date: 2004-10-06 19:56

Message:
Logged In: YES 
user_id=876734

On the one hand, you are correct that changing the order 
improves the behavior. On the other hand, the doc is as I 
recalled it, asserting that you can call it first. Perhaps this 
should be registered as a doc problem?

This is a no-op when called for a second time. It is safe to 
call this function before calling Py_Initialize() . 

----------------------------------------------------------------------

Comment By: benson margulies (benson_basis)
Date: 2004-10-06 19:49

Message:
Logged In: YES 
user_id=876734

The documentation very specifically states that the order 
doesn't matter. Further, I've read the code, and the problem 
is unprotected data structures in the python debug heap, 
which won't be any more protected in the other order.

I'll switch them and report back, just in case.


----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-06 19:32

Message:
Logged In: YES 
user_id=1038590

I haven't checked if that would fix the problem, since I'm
currently on Linux.

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2004-10-06 19:31

Message:
Logged In: YES 
user_id=1038590

The attached program calls PyEval_InitThreads and
Py_Initialize in the wrong order.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1041645&group_id=5470


More information about the Python-bugs-list mailing list