catch errors when writing on closed sockets

avoids issues with direct-checks for closed or closing sockets,
which seems to change across tornado versions

fixes some tornado 6 issues
This commit is contained in:
Min RK 2019-03-26 13:04:58 +01:00
parent eba43aa6c9
commit ca7b8dd163

View File

@ -11,7 +11,8 @@ from urllib.parse import urlparse
import tornado
from tornado import gen, ioloop, web
from tornado.websocket import WebSocketHandler
from tornado.iostream import StreamClosedError
from tornado.websocket import WebSocketHandler, WebSocketClosedError
from jupyter_client.session import Session
from jupyter_client.jsonutil import date_default, extract_dates
@ -181,8 +182,13 @@ class WebSocketMixin(object):
self.log.warning("WebSocket ping timeout after %i ms.", since_last_pong)
self.close()
return
try:
self.ping(b'')
except (StreamClosedError, WebSocketClosedError):
# websocket has been closed, stop pinging
self.ping_callback.stop()
return
self.ping(b'')
self.last_ping = now
def on_pong(self, data):
@ -242,8 +248,14 @@ class ZMQStreamHandler(WebSocketMixin, WebSocketHandler):
msg = self._reserialize_reply(msg_list, channel=channel)
except Exception:
self.log.critical("Malformed message: %r" % msg_list, exc_info=True)
else:
return
try:
self.write_message(msg, binary=isinstance(msg, bytes))
except (StreamClosedError, WebSocketClosedError):
self.log.warning("zmq message arrived on closed channel")
self.close()
return
class AuthenticatedZMQStreamHandler(ZMQStreamHandler, IPythonHandler):