Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thread dying without exception

I'm having an issue with some of my worker threads. I've added a catchall exception statement in the thread's run method like so:

 try:
        """Runs the worker process, which is a state machine"""
        while self._set_exitcode is None :
            assert self._state in Worker.STATES
            state_methodname = "_state_%s" % self._state
            assert hasattr(self, state_methodname)
            state_method = getattr(self, state_methodname)
            self._state = state_method() # execute method for current state

        self._stop_heartbeat()
        sys.exit( self._set_exitcode )
 except:

        self.log.debug(sys.exc_info())

I read this was the defacto way to catch everything that may be causing an issue instead of using Exception, e. I've found some great little bugs thanks to this method but my problem is that the workers' are still dying and I'm not sure how to further record what's going on or troubleshoot.

Any thoughts would be greatly appreciated.

Thanks!

like image 490
deecodameeko Avatar asked Sep 14 '26 22:09

deecodameeko


1 Answers

You could try examining the execution trace of your program using the trace module. For example:

% python -m trace -c -t -C ./coverage test_exit.py

Source:

import sys
import threading

class Worker(object):
    def run(self):
        try:
            sys.exit(1)
        except:
            print sys.exc_info()

threading.Thread(target=Worker().run).start()

It will dump out each line as it is executed, and you should get a coverage report in the coverage directory:

...
threading.py(482):         try:
threading.py(483):             if self.__target:
threading.py(484):                 self.__target(*self.__args, **self.__kwargs)
 --- modulename: test_exit, funcname: run
test_exit.py(7):         try:
test_exit.py(8):             sys.exit(1)
test_exit.py(9):         except:
test_exit.py(10):             print sys.exc_info()
(<type 'exceptions.SystemExit'>, SystemExit(1,), <traceback object at 0x7f23098822d8>)
threading.py(488):             del self.__target, self.__args, self.__kwargs
...
like image 104
samplebias Avatar answered Sep 16 '26 13:09

samplebias