From a58c184b0ffe76d8b00912b0084b12c26bd85631 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sun, 4 Sep 2016 11:54:55 -0700 Subject: [PATCH] do not disconnect an already disconnected client See https://github.com/miguelgrinberg/Flask-SocketIO/issues/312 --- socketio/server.py | 11 ++++++----- tests/test_server.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/socketio/server.py b/socketio/server.py index 952641a..21e067e 100644 --- a/socketio/server.py +++ b/socketio/server.py @@ -296,11 +296,12 @@ class Server(object): argument is omitted the default namespace is used. """ namespace = namespace or '/' - self.logger.info('Disconnecting %s [%s]', sid, namespace) - self._send_packet(sid, packet.Packet(packet.DISCONNECT, - namespace=namespace)) - self._trigger_event('disconnect', namespace, sid) - self.manager.disconnect(sid, namespace=namespace) + if self.manager.is_connected(sid, namespace=namespace): + self.logger.info('Disconnecting %s [%s]', sid, namespace) + self._send_packet(sid, packet.Packet(packet.DISCONNECT, + namespace=namespace)) + self._trigger_event('disconnect', namespace, sid) + self.manager.disconnect(sid, namespace=namespace) def transport(self, sid): """Return the name of the transport used by the client. diff --git a/tests/test_server.py b/tests/test_server.py index bcc2638..a390685 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -389,6 +389,23 @@ class TestServer(unittest.TestCase): s.disconnect('123', namespace='/foo') s.eio.send.assert_any_call('123', '1/foo', binary=False) + def test_disconnect_twice(self, eio): + s = server.Server() + s._handle_eio_connect('123', 'environ') + s.disconnect('123') + calls = s.eio.send.call_count + s.disconnect('123') + self.assertEqual(calls, s.eio.send.call_count) + + def test_disconnect_twice_namespace(self, eio): + s = server.Server() + s._handle_eio_connect('123', 'environ') + s._handle_eio_message('123', '0/foo') + s.disconnect('123', namespace='/foo') + calls = s.eio.send.call_count + s.disconnect('123', namespace='/foo') + self.assertEqual(calls, s.eio.send.call_count) + def test_namespace_handler(self, eio): result = {}