@@ -2303,17 +2303,26 @@ def close(self, getattr = getattr):
23032303 # Write out the disconnect message if the socket is around.
23042304 # If the connection is known to be lost, don't bother. It will
23052305 # generate an extra exception.
2306+ if getattr (self , 'pq' , None ) is None or getattr (self .pq , 'socket' , None ) is None :
2307+ # No action to take.
2308+ return
2309+
23062310 x = getattr (self .pq , 'xact' , None )
2307- if x :
2308- # complete the existing transaction, if possible.
2309- # XXX: don't raise?
2311+ if x is not None and x .fatal is not True :
2312+ # finish the existing pq transaction iff it's not Closing.
23102313 self .pq .complete ()
2311- if self . closed :
2312- return
2313- if getattr ( self , 'pq' , None ):
2314+
2315+ if self . pq . xact is None :
2316+ # It completed the existing transaction.
23142317 self .pq .push (xact .Closing ())
23152318 self .pq .complete ()
2319+ if self .pq .socket :
2320+ self .pq .complete ()
2321+
2322+ # Close the socket if there is one.
2323+ if self .pq .socket :
23162324 self .pq .socket .close ()
2325+ self .pq .socket = None
23172326
23182327 @property
23192328 def state (self ) -> str :
@@ -2446,6 +2455,8 @@ def _establish(self):
24462455 # Close out the sockets ourselves.
24472456 pq .socket .close ()
24482457
2458+ # Identify whether or not we can skip the attempt.
2459+ # Whether or not we can skip depends entirely on the SSL parameter.
24492460 if sslmode == 'prefer' and ssl is False and didssl is False :
24502461 # In this case, the server doesn't support SSL or it's
24512462 # turned off. Therefore, the "without_ssl" attempt need
0 commit comments