1

I have created a Python program that uses Autobahn to make WebSocket connections to a remote host, and receive a data flow over these connections.

From time to time, some different exceptions occur during these connections, most often either an exception immediately when attempting to connect, stating that the initial WebSocket handshake failed (most likely due to an overloaded server), like this:

2017-05-03T20:31:10 dropping connection to peer tcp:1.2.3.4:443 with abort=True: WebSocket opening handshake timeout (peer did not finish the opening handshake in time)

Or a later exception during a successful and ongoing connection, saying that the connection timed out due to lack of pong response to a ping, as follows:

2017-05-04T13:33:40 dropping connection to peer tcp:1.2.3.4:443 with abort=True: WebSocket ping timeout (peer did not respond with pong in time)

2017-05-04T13:33:40 session closed with reason wamp.close.transport_lost [WAMP transport was lost without closing the session before]

2017-05-04T13:33:40 While firing onDisconnect: Traceback (most recent call last):

File "c:\Python36\lib\site-packages\txaio\aio.py", line 450, in done

f._result = x

AttributeError: attribute '_result' of '_asyncio.Future' objects is not writable

As can be seen above, this also triggers some other strange exception in the txaio module in this particular case.

No matter what kind of exception that occurs, I would like to catch them and handle them gracefully, but for some reason the exceptions (none of them) seem to bubble up to the code that initiated these connections (i.e. get caught by my try ... except clause there), which looks like this:

from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner

...

class MyComponent(ApplicationSession):

...

try:
    runner = ApplicationRunner("wss://my.websocket.server.com:443", "realm1")
    runner.run(MyComponent)
except Exception as e:
    print('Unexpected connection error')
    ...

Instead, all these exceptions just hang my program completely after the error messages have been dumped out to the terminal as above, why is this?

So, the question is: How and where in the code can I catch these exceptions that occur during the WebSocket connections in Autobahn, and react/handle them gracefully?

Community
  • 1
  • 1
QuestionOverflow
  • 649
  • 8
  • 20
  • You meant to say those exceptions never reach your ```except:``` block? – gipsy May 04 '17 at 13:41
  • @gipsy Yes, that is exactly what I meant. Given that the terminal-dumped exception text is a fatal/uncaught exception as it looks like, I thought that not triggering a higher-level try ... except-clause like this was only possible when the exception occurred in another thread? But perhaps Autobahn uses threads internally? Either way I still very much need to know where I need to put my try ... except clause in order to catch and handle these kinds of connection exceptions gracefully. I guess it is in one of the event handlers of the ApplicationSession-derived object then, or am I wrong? – QuestionOverflow May 04 '17 at 18:27
  • This question may be related to: https://stackoverflow.com/questions/44744630/poloniex-push-wamp-api-through-autobahn-dropping-connection-to-peer-tcp/44771058#44771058 – iDigLiveMusic Jun 27 '17 at 14:43
  • Did you ever find an answer to this? Exception handling in Autobahn seems to be a nightmare and I'm regularly left with client applications hanging because of connectivity issues with the server. – ydaetskcoR Apr 06 '22 at 09:11

0 Answers0