From 3349b024d59a78a6a7282257941b4623af72d7c9 Mon Sep 17 00:00:00 2001
From: Miguel Grinberg <miguel.grinberg@gmail.com>
Date: Sat, 26 Dec 2020 00:17:47 +0000
Subject: [PATCH] Invoke the disconnect handler when the client initiates a
 disconnection (Fixes #594)

---
 socketio/asyncio_client.py           | 1 -
 socketio/client.py                   | 1 -
 tests/asyncio/test_asyncio_client.py | 2 ++
 tests/common/test_client.py          | 2 ++
 4 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/socketio/asyncio_client.py b/socketio/asyncio_client.py
index 06bf644..b2ad1ea 100644
--- a/socketio/asyncio_client.py
+++ b/socketio/asyncio_client.py
@@ -255,7 +255,6 @@ class AsyncClient(client.Client):
         for n in self.namespaces:
             await self._send_packet(packet.Packet(packet.DISCONNECT,
                                     namespace=n))
-        self.connected = False
         await self.eio.disconnect(abort=True)
 
     def start_background_task(self, target, *args, **kwargs):
diff --git a/socketio/client.py b/socketio/client.py
index aab90ed..d42d0d7 100644
--- a/socketio/client.py
+++ b/socketio/client.py
@@ -408,7 +408,6 @@ class Client(object):
         # later in _handle_eio_disconnect we invoke the disconnect handler
         for n in self.namespaces:
             self._send_packet(packet.Packet(packet.DISCONNECT, namespace=n))
-        self.connected = False
         self.eio.disconnect(abort=True)
 
     def get_sid(self, namespace=None):
diff --git a/tests/asyncio/test_asyncio_client.py b/tests/asyncio/test_asyncio_client.py
index 95abb1c..a96ca0e 100644
--- a/tests/asyncio/test_asyncio_client.py
+++ b/tests/asyncio/test_asyncio_client.py
@@ -406,6 +406,7 @@ class TestAsyncClient(unittest.TestCase):
 
     def test_disconnect(self):
         c = asyncio_client.AsyncClient()
+        c.connected = True
         c.namespaces = {'/': '1'}
         c._trigger_event = AsyncMock()
         c._send_packet = AsyncMock()
@@ -413,6 +414,7 @@ class TestAsyncClient(unittest.TestCase):
         c.eio.disconnect = AsyncMock()
         c.eio.state = 'connected'
         _run(c.disconnect())
+        assert c.connected
         assert c._trigger_event.mock.call_count == 0
         assert c._send_packet.mock.call_count == 1
         expected_packet = packet.Packet(packet.DISCONNECT, namespace='/')
diff --git a/tests/common/test_client.py b/tests/common/test_client.py
index f7bee9a..5c6ff2c 100644
--- a/tests/common/test_client.py
+++ b/tests/common/test_client.py
@@ -508,12 +508,14 @@ class TestClient(unittest.TestCase):
 
     def test_disconnect(self):
         c = client.Client()
+        c.connected = True
         c.namespaces = {'/': '1'}
         c._trigger_event = mock.MagicMock()
         c._send_packet = mock.MagicMock()
         c.eio = mock.MagicMock()
         c.eio.state = 'connected'
         c.disconnect()
+        assert c.connected
         assert c._trigger_event.call_count == 0
         assert c._send_packet.call_count == 1
         expected_packet = packet.Packet(packet.DISCONNECT, namespace='/')