@@ -72,7 +72,7 @@ class SignalRHub:
7272 Lifecycle:
7373 ``run()`` — negotiate fresh token, build client, block until disconnect
7474 ``invoke()`` — send a hub method invocation
75- ``disconnect()`` — stop the transport cleanly
75+ ``disconnect()`` — cancel the task awaiting ``run()`` to stop cleanly
7676 """
7777
7878 def __init__ (
@@ -85,6 +85,7 @@ def __init__(
8585 """
8686 self ._negotiate = negotiate_callback
8787 self ._client : Optional [SignalRClient ] = None
88+ self ._task : Optional [asyncio .Task ] = None
8889 self ._connect_callbacks : list [Callable [..., Any ]] = []
8990 self ._disconnect_callbacks : list [Callable [..., Any ]] = []
9091 self ._event_callbacks : list [Callable [..., Any ]] = []
@@ -177,10 +178,12 @@ async def _handler(arguments: Any) -> None:
177178 self ._client .on_close (self ._on_close )
178179 self ._client .on_error (self ._on_error )
179180
181+ self ._task = asyncio .current_task ()
180182 try :
181183 await self ._client .run ()
182184 finally :
183185 self ._client = None
186+ self ._task = None
184187
185188 async def invoke (self , method : str , args : list [Any ]) -> None :
186189 """Invoke a hub method on the server.
@@ -195,10 +198,23 @@ async def invoke(self, method: str, args: list[Any]) -> None:
195198 await self ._client .send (method , args )
196199
197200 async def disconnect (self ) -> None :
198- """Request the client to stop."""
199- if self ._client is not None :
201+ """Request the client to stop.
202+
203+ ``pysignalr.SignalRClient`` has no ``stop()``/``close()`` method --
204+ its ``run()`` coroutine blocks until the connection ends and is
205+ meant to be cancelled by the caller (see pysignalr's
206+ ``WebsocketTransport.run()``, an unconditional reconnect loop with
207+ no external stop hook). Cancel the task that's awaiting ``run()``
208+ instead of calling a nonexistent client method.
209+ """
210+ task = self ._task
211+ if task is not None :
200212 LOG .info ("SignalRHub: disconnecting" )
201- await self ._client .stop ()
213+ task .cancel ()
214+ try :
215+ await task
216+ except asyncio .CancelledError :
217+ pass
202218
203219 # ------------------------------------------------------------------
204220 # Internal pysignalr hooks
0 commit comments