[ python-Bugs-1230540 ] sys.excepthook doesn't work in threads

SourceForge.net noreply at sourceforge.net
Fri Jun 15 04:04:21 CEST 2007


Bugs item #1230540, was opened at 2005-06-30 19:06
Message generated for change (Comment added) made by ellisj
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1230540&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Threads
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Jonathan Ellis (ellisj)
Assigned to: Nobody/Anonymous (nobody)
Summary: sys.excepthook doesn't work in threads

Initial Comment:
simple script to reproduce:

import sys, threading

def log_exception(*args):
    print 'got exception %s' % (args,)
sys.excepthook = log_exception

def foo():
    a = 1 / 0
threading.Thread(target=foo).start()

Note that a normal traceback is printed instead of "got
exception."

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

>Comment By: Jonathan Ellis (ellisj)
Date: 2007-06-15 02:04

Message:
Logged In: YES 
user_id=657828
Originator: YES

Here is a workaround in the meantime:

def install_thread_excepthook():
    """
    Workaround for sys.excepthook thread bug
   
(https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1230540&group_id=5470).
    Call once from __main__ before creating any threads.
    If using psyco, call psycho.cannotcompile(threading.Thread.run)
    since this replaces a new-style class method.
    """
    import sys
    run_old = threading.Thread.run
    def run(*args, **kwargs):
        try:
            run_old(*args, **kwargs)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            sys.excepthook(*sys.exc_info())
    threading.Thread.run = run


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

Comment By: Michael Hudson (mwh)
Date: 2007-06-06 11:11

Message:
Logged In: YES 
user_id=6656
Originator: NO

I've just run into this, and it's very annoying.  The stupid part is that
threads started with the thread module don't have this problem, it's just a
problem with threading.Thread()s trying to be too clever.

I would vote for deleting all the code to do with exception printing in
threading.py and letting the C machinery take care of it.

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

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


More information about the Python-bugs-list mailing list