0

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...
James Riley
  • 127
  • 9
  • Does the program stop? Aren't you in (some sort of) _Debug_ mode and the exception traceback is only displayed? Apparently (after reading the output) that seems to be the case: "_root : WARNING ...Im about to raise an exception from inside a try block!_". – CristiFati Aug 31 '17 at 22:29
  • The program execution does not stop. The traceback is generated both in and out of debug mode, and no other tracebacks are generated (even though other exceptions are handled).That warning statement is from me- see the log.warning line in the try block of my code. Thanks for looking! – James Riley Aug 31 '17 at 22:47
  • 1
    Then, I don't think there's a problem. I've never used _thrift_, but I think It just displays the exception (even if it was caught), because it prevented it to do something, but I'm sure this can be suppressed via a setting. – CristiFati Aug 31 '17 at 22:54

1 Answers1

1

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)
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265