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 = {}