Exception Handling in socket connection

prateek3211
Hi Team,

Hope you all are doing well.
I have opened a socket connection for getting market feed using zerodha python library. I am facing below issue after which the connection is lost.


2020-04-01T06:53:51.643249000Z Unhandled Error
2020-04-01T06:53:51.643454000Z Traceback (most recent call last):
2020-04-01T06:53:51.643629000Z File "/usr/local/lib/python3.6/site-packages/twisted/python/log.py", line 103, in callWithLogger
2020-04-01T06:53:51.643801000Z return callWithContext({"system": lp}, func, *args, **kw)
2020-04-01T06:53:51.643997000Z File "/usr/local/lib/python3.6/site-packages/twisted/python/log.py", line 86, in callWithContext
2020-04-01T06:53:51.644181000Z return context.call({ILogContext: newCtx}, func, *args, **kw)
2020-04-01T06:53:51.644350000Z File "/usr/local/lib/python3.6/site-packages/twisted/python/context.py", line 122, in callWithContext
2020-04-01T06:53:51.644518000Z return self.currentContext().callWithContext(ctx, func, *args, **kw)
2020-04-01T06:53:51.644685000Z File "/usr/local/lib/python3.6/site-packages/twisted/python/context.py", line 85, in callWithContext
2020-04-01T06:53:51.644852000Z return func(*args,**kw)
2020-04-01T06:53:51.645042000Z --- ---
2020-04-01T06:53:51.645223000Z File "/usr/local/lib/python3.6/site-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
2020-04-01T06:53:51.645393000Z why = selectable.doRead()
2020-04-01T06:53:51.645561000Z File "/usr/local/lib/python3.6/site-packages/twisted/internet/tcp.py", line 243, in doRead
2020-04-01T06:53:51.645728000Z return self._dataReceived(data)
2020-04-01T06:53:51.645894000Z File "/usr/local/lib/python3.6/site-packages/twisted/internet/tcp.py", line 249, in _dataReceived
2020-04-01T06:53:51.646096000Z rval = self.protocol.dataReceived(data)
2020-04-01T06:53:51.646266000Z File "/usr/local/lib/python3.6/site-packages/twisted/protocols/tls.py", line 330, in dataReceived
2020-04-01T06:53:51.646432000Z self._flushReceiveBIO()
2020-04-01T06:53:51.646598000Z File "/usr/local/lib/python3.6/site-packages/twisted/protocols/tls.py", line 295, in _flushReceiveBIO
2020-04-01T06:53:51.646765000Z ProtocolWrapper.dataReceived(self, bytes)
2020-04-01T06:53:51.646949000Z File "/usr/local/lib/python3.6/site-packages/twisted/protocols/policies.py", line 120, in dataReceived
2020-04-01T06:53:51.647132000Z self.wrappedProtocol.dataReceived(data)
2020-04-01T06:53:51.647303000Z File "/usr/local/lib/python3.6/site-packages/autobahn/twisted/websocket.py", line 288, in dataReceived
2020-04-01T06:53:51.647469000Z self._dataReceived(data)
2020-04-01T06:53:51.647636000Z File "/usr/local/lib/python3.6/site-packages/autobahn/websocket/protocol.py", line 1206, in _dataReceived
2020-04-01T06:53:51.647802000Z self.consumeData()
2020-04-01T06:53:51.647988000Z File "/usr/local/lib/python3.6/site-packages/autobahn/websocket/protocol.py", line 1218, in consumeData
2020-04-01T06:53:51.648170000Z while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
2020-04-01T06:53:51.648340000Z File "/usr/local/lib/python3.6/site-packages/autobahn/websocket/protocol.py", line 1578, in processData
2020-04-01T06:53:51.648506000Z fr = self.onFrameEnd()
2020-04-01T06:53:51.648673000Z File "/usr/local/lib/python3.6/site-packages/autobahn/websocket/protocol.py", line 1703, in onFrameEnd
2020-04-01T06:53:51.648839000Z self._onMessageEnd()
2020-04-01T06:53:51.649025000Z File "/usr/local/lib/python3.6/site-packages/autobahn/twisted/websocket.py", line 316, in _onMessageEnd
2020-04-01T06:53:51.649205000Z self.onMessageEnd()
2020-04-01T06:53:51.649374000Z File "/usr/local/lib/python3.6/site-packages/autobahn/websocket/protocol.py", line 627, in onMessageEnd
2020-04-01T06:53:51.649541000Z self._onMessage(payload, self.message_is_binary)
2020-04-01T06:53:51.649707000Z File "/usr/local/lib/python3.6/site-packages/autobahn/twisted/websocket.py", line 319, in _onMessage
2020-04-01T06:53:51.649874000Z self.onMessage(payload, isBinary)
2020-04-01T06:53:51.650062000Z File "/usr/local/lib/python3.6/site-packages/kiteconnect-3.7.7-py3.6.egg/kiteconnect/ticker.py", line 71, in onMessage



Question 1:
I wanted to know is there a way via which, I can catch any exception which occurs in socket streaming & then I can inside than exception handler method I can write my own logic/action to take.

I have seen handler methods like on_reconnect, on_noreconnect, on_error but in the above case for which I have attached logs no handler method was triggerd and socket was closed

Question 2:
In case exception occurs in methods like on_ticks, ie while appling some logic on response, is the socket connection terminated with no alert ?
  • rakeshr
    @prateek3211
    Are you sure, above is the complete error traceback?
    I wanted to know is there a way via which, I can catch any exception which occurs in socket streaming & then I can inside than exception handler method I can write my own logic/action to take.
    You can have look to this thread.
    In case an exception occurs in methods like on_ticks, ie while appling some logic on response, is the socket connection terminated with no alert?
    If you block main on_tick thread, then WebSocket connection will be closed uncleanly, with no error description.
    You can have look to this thread.
Sign In or Register to comment.