@@ -624,26 +624,31 @@ async def _connect_addr(
624
624
params_retry = params ._replace (ssl = None )
625
625
else :
626
626
# skip retry if we don't have to
627
- return await __connect_addr (params , timeout , * args )
627
+ return await __connect_addr (params , timeout , False , * args )
628
628
629
629
# first attempt
630
630
before = time .monotonic ()
631
631
try :
632
- return await __connect_addr (params , timeout , * args )
633
- except ConnectionError :
632
+ return await __connect_addr (params , timeout , True , * args )
633
+ except _Retry :
634
634
pass
635
635
636
636
# second attempt
637
637
timeout -= time .monotonic () - before
638
638
if timeout <= 0 :
639
639
raise asyncio .TimeoutError
640
640
else :
641
- return await __connect_addr (params_retry , timeout , * args )
641
+ return await __connect_addr (params_retry , timeout , False , * args )
642
+
643
+
644
+ class _Retry (Exception ):
645
+ pass
642
646
643
647
644
648
async def __connect_addr (
645
649
params ,
646
650
timeout ,
651
+ retry ,
647
652
addr ,
648
653
loop ,
649
654
config ,
@@ -681,17 +686,18 @@ async def __connect_addr(
681
686
):
682
687
tr .close ()
683
688
684
- if (
689
+ # retry=True here is a redundant check because we don't want to
690
+ # accidentally raise the internal _Retry to the outer world
691
+ if retry and (
685
692
params .sslmode == SSLMode .allow and not pr .is_ssl or
686
693
params .sslmode == SSLMode .prefer and pr .is_ssl
687
694
):
688
- # Elevate the error to ConnectionError to trigger retry when:
695
+ # Trigger retry when:
689
696
# 1. First attempt with sslmode=allow, ssl=None failed
690
697
# 2. First attempt with sslmode=prefer, ssl=ctx failed while the
691
698
# server claimed to support SSL (returning "S" for SSLRequest)
692
699
# (likely because pg_hba.conf rejected the connection)
693
- raise ConnectionError ("Connection rejected trying {} SSL" .format (
694
- 'with' if pr .is_ssl else 'without' ))
700
+ raise _Retry ()
695
701
696
702
else :
697
703
# but will NOT retry if:
0 commit comments