Issue1596321
Created on 2006-11-14 14:02 by cwalther, last changed 2008-01-18 08:55 by cwalther.
| File name |
Uploaded |
Description |
Edit |
Remove |
|
threadingbug.py
|
nobody,
2006-11-15 07:00
|
minimal reproducing example |
|
|
|
thread_crash.py
|
amaury.forgeotdarc,
2008-01-17 20:04
|
|
|
|
|
msg30544 - (view) |
Author: Christian Walther (cwalther) |
Date: 2006-11-14 14:02 |
|
Python 2.4.3 on Windows 2000, though the code in
question seems unchanged in current SVN (r46919).
I'm using Python embedded in a multithreaded C++
application. When 'import threading' is first done in
some Python script that runs in thread A, I get the
following exception when a different thread B calls
Py_Finalize():
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Python24\lib\atexit.py", line 24, in
_run_exitfuncs
func(*targs, **kargs)
File "C:\Python24\lib\threading.py", line 636, in
__exitfunc
self._Thread__delete()
File "C:\Python24\lib\threading.py", line 522, in
__delete
del _active[_get_ident()]
KeyError: 680
Error in sys.exitfunc:
Traceback (most recent call last):
File "C:\Python24\lib\atexit.py", line 24, in
_run_exitfuncs
func(*targs, **kargs)
File "C:\Python24\lib\threading.py", line 636, in
__exitfunc
self._Thread__delete()
File "C:\Python24\lib\threading.py", line 522, in
__delete
del _active[_get_ident()]
KeyError: 680
The reason seems to be that the threading module uses
the thread ID of the calling thread as a key to store
its _MainThread instance on initialization, and again
the thread ID of the calling thread to delete it in its
exit function. If these two threads are not the same,
the described KeyError occurs.
I didn't study this in all detail, but it seems to me
that threading.Thread.__delete() does the wrong thing.
By doing 'del _active[_get_ident()]', it removes the
instance for the calling thread from the _active
dictionary. What it should be doing is removing *self*
from that dictionary. Is that correct?
|
|
msg30545 - (view) |
Author: Brett Cannon (brett.cannon) |
Date: 2006-11-14 21:59 |
|
Well, I don't think you should be calling Py_Finalize() from the non-main thread. That just seems unsafe to me.
Regardless, though, could you write up some quick Python code that triggers this?
|
|
msg30546 - (view) |
Author: Christian Walther (cwalther) |
Date: 2006-11-15 07:02 |
|
I'm not calling Py_Finalize from a non-main thread. What I called "thread B" is the main thread. It's the script that first imports the threading module that runs in a non-main thread (and running user-defined scripts in non-main threads is hopefully not unsafe, or there wouldn't be much point in supporting multithreading at all in Python).
It didn't even occur to me that this could be reproduced in pure Python code, so I didn't include an example in my original post. Of course, it can - see attachment. Tested on Python 2.3.5 on Mac OS X.
|
|
msg30547 - (view) |
Author: Brett Cannon (brett.cannon) |
Date: 2006-11-15 18:51 |
|
Thanks for the test code. I have no clue when I or anyone else will get to this, but the report and testing code is appreciated.
|
|
msg60013 - (view) |
Author: Gregory P. Smith (gregory.p.smith) |
Date: 2008-01-17 01:47 |
|
threadingbug.py doesn't fail for me on trunk (linux), anyone else?
the output I get is always:
Main thread ID: -134346528
Secondary thread ID: -135349328
Exception KeyError: KeyError(-134346528,) in <module 'threading' from
'/home/gps/oss/python/trunk/Lib/threading.pyc'> ignored
|
|
msg60035 - (view) |
Author: Christian Walther (cwalther) |
Date: 2008-01-17 17:13 |
|
I'm not sure what you mean by "doesn't fail" - from the output you quote,
I'd say that it does fail. It's in fact the same output as I get right now
with Python 2.5.1 on Mac OS X.
Would you classify that KeyError as expected behavior?
|
|
msg60041 - (view) |
Author: Gregory P. Smith (gregory.p.smith) |
Date: 2008-01-17 18:14 |
|
gah, sorry i misread the report. you are correct.
|
|
msg60050 - (view) |
Author: Adam Olsen (Rhamphoryncus) |
Date: 2008-01-17 18:54 |
|
Is the bug avoided if you import threading first and use it instead of
thread? I'd like to see thread removed in 3.0 (renamed to _thread or
the like.)
|
|
msg60052 - (view) |
Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) |
Date: 2008-01-17 20:04 |
|
If a python daemon thread is still running when the interpreter exits,
it is likely to fail in random ways.
Here is another example, which does not use imports.
I run the script many times, with latest version in trunk, on Windows
XP, debug build.
In the majority of runs, I get an error message:
"""
Exception in thread Thread-1 (most likely raised during interpreter
shutdown):
"""
Other tests seem to show that all modules are emptied by the cleanup
process, but the thread insists to get "time.sleep".
And more interestingly, about every 50 runs, the process segfaults...
I suspect that this is a problem similar to http://bugs.python.org/issue1856
|
|
msg60083 - (view) |
Author: Christian Walther (cwalther) |
Date: 2008-01-18 08:54 |
|
> Is the bug avoided if you import threading first and use it instead of thread?
Yes. The bug happens when the (first) import of threading and the call to Py_Finalize()
happen in different threads. To reproduce the problem in pure Python, I therefore have to
use thread instead of threading to create the secondary thread. (In the C++ application,
it's created on the C++ side.)
Has anyone checked if the solution I propose in the first post makes sense?
|
|
| Date |
User |
Action |
Args |
| 2009-02-17 17:20:49 | amaury.forgeotdarc | link | issue1159425 superseder |
| 2008-01-18 08:55:01 | cwalther | set | messages:
+ msg60083 |
| 2008-01-17 20:04:11 | amaury.forgeotdarc | set | files:
+ thread_crash.py nosy:
+ amaury.forgeotdarc messages:
+ msg60052 |
| 2008-01-17 18:54:46 | Rhamphoryncus | set | nosy:
+ Rhamphoryncus messages:
+ msg60050 |
| 2008-01-17 18:14:06 | gregory.p.smith | set | messages:
+ msg60041 versions:
+ Python 2.6, Python 2.5, Python 2.4 |
| 2008-01-17 17:13:26 | cwalther | set | messages:
+ msg60035 |
| 2008-01-17 01:47:55 | gregory.p.smith | set | nosy:
+ gregory.p.smith messages:
+ msg60013 |
| 2006-11-14 14:02:45 | cwalther | create | |
|