Search code examples
pythonpython-2.7flaskthriftwerkzeug

Why does my console display a stack trace from a Thrift exception raised within a try block?


My thrift client is not currently connected, so the call to self.transport.open() should time out and my program execution should continue as it does. But I want to know why the time out raises an exception, why the stack trace prints to console, and whether this indicates a problem.

The code:

def connect(self, url=None):
    """Attempt to connect to supervisor Thrift server"""
    if conf.TESTING:
        return

    try:
        self.url = url if url is not None else self.url
        self.socket = TSocket.TSocket(self.url, constants.kSupervisorPort)
        self.socket.setTimeout(1000)
        self.transport = TTransport.TBufferedTransport(self.socket)
        self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        self.client = Supervisor.Client(self.protocol)
        log.warning("...Im about to raise an exception from inside a try block!")
        self.transport.open()
        self.connected = True
        log.info('[TaskStateClient] Connected at url: ' + self.url)

    except Exception:
        log.warning("...and now Im handling the raised exception...")

And the stack trace:

->  Running
python app.py
werkzeug    : INFO      * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
root        : WARNING  ...Im about to raise an exception from inside a try block!
thrift.transport.TSocket: INFO     Could not connect to ('192.168.1.219', 9002)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/thrift/transport/TSocket.py", line 104, in open
handle.connect(sockaddr)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
timeout: timed out
thrift.transport.TSocket: ERROR    Could not connect to any of [('192.168.1.219', 9002)]
root        : WARNING  ...and now Im handling the raised exception...

Solution

  • You print the active loggers using

    import logging
    for key in logging.Logger.manager.loggerDict:
        print(key)
    

    Then for the logger related to TSocket, you can set the debug level to critical

    logging.getLogger('thrift.transport.TSocket').setLevel(logging.CRITICAL)
    

    or

    logging.getLogger('thrift.transport.TSocket').setLevel(logging.NOTSET)