Skip to content

Commit 4ddc8ce

Browse files
author
James William Pye
committed
Fix close() to always close pq.socket if pq.socket is not None.
If there isn't a fatal transaction, be sure push Closing().
1 parent 2b2b875 commit 4ddc8ce

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

postgresql/driver/pq3.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

postgresql/test/test_driver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,7 @@ def testNamedTuples(self):
17201720

17211721
@pg_tmp
17221722
def testBadFD(self):
1723+
db.pq.socket.close()
17231724
# bad fd now.
17241725
self.assertRaises(
17251726
pg_exc.ConnectionFailureError,

0 commit comments

Comments
 (0)