Skip to content

Commit 94cafc0

Browse files
author
James William Pye
committed
In cases where socket_makers can't be created, raise ClientCannotConnect from.
This provides a distinction for connection failures and a complete inability to resolve the sequence of socket makers. Specifically, no connection attempt was made so there were no connection failures, but the client cannot connect due to name resolution error.
1 parent a13316a commit 94cafc0

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

postgresql/driver/pq3.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,9 +1987,24 @@ def connect(self, timeout = None):
19871987
timeout = timeout or self.connector.connect_timeout
19881988
sslmode = self.connector.sslmode or 'prefer'
19891989

1990-
# get the list of sockets to try
1991-
socket_makers = self.connector.socket_factory_sequence()
19921990
connection_failures = []
1991+
socket_makers = ()
1992+
1993+
try:
1994+
# get the list of sockets to try
1995+
socket_makers = self.connector.socket_factory_sequence()
1996+
except Exception as exc:
1997+
err = pg_exc.ClientCannotConnectError(
1998+
"failed to resolve socket makers",
1999+
details = {
2000+
"severity" : "FATAL",
2001+
},
2002+
source = 'DRIVER'
2003+
)
2004+
self.ife_descend(err)
2005+
err.database = self
2006+
err.set_connection_failures(connection_failures)
2007+
err.raise_exception(raise_from = exc)
19932008

19942009
# resolve when to do SSL.
19952010
with_ssl = zip(repeat(True, len(socket_makers)), socket_makers)
@@ -2016,8 +2031,14 @@ def connect(self, timeout = None):
20162031
else:
20172032
raise ValueError("invalid sslmode {0!r}".format(sslmode))
20182033

2019-
# for each potential socket connection
2034+
# can_skip is used when 'prefer' is the sslmode.
2035+
# if the ssl negotiation returns 'N' (nossl), then
2036+
# ssl "failed", but the socket is still usable for nossl.
2037+
# in these cases, can_skip is set to True so that the
2038+
# subsequent non-ssl attempt is skipped.
20202039
can_skip = False
2040+
2041+
# for each potential socket connection
20212042
for (dossl, socket_maker) in socket_makers:
20222043
supported = None
20232044
if can_skip is True:
@@ -2079,13 +2100,13 @@ def connect(self, timeout = None):
20792100
(dossl, socket_maker, e)
20802101
)
20812102
else:
2082-
# No servers available.
2103+
# No servers available. (see the break-statement after establishing)
20832104
err = pg_exc.ClientCannotConnectError(
20842105
"failed to connect to server",
20852106
details = {
20862107
"severity" : "FATAL",
20872108
},
2088-
# It's really a collection of exceptions.
2109+
# It's really a sequence of exceptions.
20892110
source = 'DRIVER'
20902111
)
20912112
self.ife_descend(err)

0 commit comments

Comments
 (0)