@@ -482,6 +482,8 @@ async def connect(self) -> None:
482
482
483
483
GRAPHQLWS_SUBPROTOCOL : Subprotocol = cast (Subprotocol , "graphql-ws" )
484
484
485
+ log .debug ("connect: starting" )
486
+
485
487
if self .websocket is None and not self ._connecting :
486
488
487
489
# Set connecting to True to avoid a race condition if user is trying
@@ -537,6 +539,8 @@ async def connect(self) -> None:
537
539
else :
538
540
raise TransportAlreadyConnected ("Transport is already connected" )
539
541
542
+ log .debug ("connect: done" )
543
+
540
544
async def _clean_close (self , e : Exception ) -> None :
541
545
"""Coroutine which will:
542
546
@@ -569,35 +573,81 @@ async def _close_coro(self, e: Exception, clean_close: bool = True) -> None:
569
573
- close the websocket connection
570
574
- send the exception to all the remaining listeners
571
575
"""
572
- if self .websocket :
576
+
577
+ log .debug ("_close_coro: starting" )
578
+
579
+ try :
580
+
581
+ # We should always have an active websocket connection here
582
+ assert self .websocket is not None
573
583
574
584
# Saving exception to raise it later if trying to use the transport
575
585
# after it has already closed.
576
586
self .close_exception = e
577
587
578
588
if clean_close :
579
- await self ._clean_close (e )
589
+ log .debug ("_close_coro: starting clean_close" )
590
+ try :
591
+ await self ._clean_close (e )
592
+ except Exception as exc : # pragma: no cover
593
+ log .warning ("Ignoring exception in _clean_close: " + repr (exc ))
594
+
595
+ log .debug ("_close_coro: sending exception to listeners" )
580
596
581
597
# Send an exception to all remaining listeners
582
598
for query_id , listener in self .listeners .items ():
583
599
await listener .set_exception (e )
584
600
601
+ log .debug ("_close_coro: close websocket connection" )
602
+
585
603
await self .websocket .close ()
586
604
587
- self .websocket = None
605
+ log .debug ("_close_coro: websocket connection closed" )
606
+
607
+ except Exception as exc : # pragma: no cover
608
+ log .warning ("Exception catched in _close_coro: " + repr (exc ))
609
+
610
+ finally :
588
611
612
+ log .debug ("_close_coro: start cleanup" )
613
+
614
+ self .websocket = None
589
615
self .close_task = None
616
+
590
617
self ._wait_closed .set ()
591
618
619
+ log .debug ("_close_coro: exiting" )
620
+
592
621
async def _fail (self , e : Exception , clean_close : bool = True ) -> None :
622
+ log .debug ("_fail: starting with exception: " + repr (e ))
623
+
593
624
if self .close_task is None :
594
- self .close_task = asyncio .shield (
595
- asyncio .ensure_future (self ._close_coro (e , clean_close = clean_close ))
625
+
626
+ if self .websocket is None :
627
+ log .debug ("_fail started with self.websocket == None -> already closed" )
628
+ else :
629
+ self .close_task = asyncio .shield (
630
+ asyncio .ensure_future (self ._close_coro (e , clean_close = clean_close ))
631
+ )
632
+ else :
633
+ log .debug (
634
+ "close_task is not None in _fail. Previous exception is: "
635
+ + repr (self .close_exception )
636
+ + " New exception is: "
637
+ + repr (e )
596
638
)
597
639
598
640
async def close (self ) -> None :
641
+ log .debug ("close: starting" )
642
+
599
643
await self ._fail (TransportClosed ("Websocket GraphQL transport closed by user" ))
600
644
await self .wait_closed ()
601
645
646
+ log .debug ("close: done" )
647
+
602
648
async def wait_closed (self ) -> None :
649
+ log .debug ("wait_close: starting" )
650
+
603
651
await self ._wait_closed .wait ()
652
+
653
+ log .debug ("wait_close: done" )
0 commit comments