close `connection._rfile` in `Connection._force_close` (#1184) · PyMySQL/PyMySQL@9204b64 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 9204b64

Browse files
authored
close connection._rfile in Connection._force_close (#1184)
fix #1183.
1 parent d93cde9 commit 9204b64

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

pymysql/connections.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class Connection:
161161
"""
162162

163163
_sock = None
164+
_rfile = None
164165
_auth_plugin_name = ""
165166
_closed = False
166167
_secure = False
@@ -430,6 +431,8 @@ def open(self):
430431

431432
def _force_close(self):
432433
"""Close connection without QUIT message."""
434+
if self._rfile:
435+
self._rfile.close()
433436
if self._sock:
434437
try:
435438
self._sock.close()
@@ -696,12 +699,7 @@ def connect(self, sock=None):
696699
if self.autocommit_mode is not None:
697700
self.autocommit(self.autocommit_mode)
698701
except BaseException as e:
699-
self._rfile = None
700-
if sock is not None:
701-
try:
702-
sock.close()
703-
except: # noqa
704-
pass
702+
self._force_close()
705703

706704
if isinstance(e, (OSError, IOError)):
707705
exc = err.OperationalError(

pymysql/tests/test_connection.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,40 @@ def test_commit_during_multi_result(self):
883883
con.commit()
884884
cur.execute("SELECT 3")
885885
self.assertEqual(cur.fetchone()[0], 3)
886+
887+
def test_force_close_closes_socketio(self):
888+
con = self.connect()
889+
sock = con._sock
890+
fileno = sock.fileno()
891+
rfile = con._rfile
892+
893+
con._force_close()
894+
assert rfile.closed
895+
assert sock._closed
896+
assert sock.fileno() != fileno # should be set to -1
897+
898+
def test_socket_closed_on_exception_in_connect(self):
899+
con = self.connect(defer_connect=True)
900+
sock = None
901+
rfile = None
902+
fileno = -1
903+
904+
def _request_authentication():
905+
nonlocal sock, rfile, fileno
906+
sock = con._sock
907+
assert sock is not None
908+
fileno = sock.fileno()
909+
rfile = con._rfile
910+
assert rfile is not None
911+
raise TypeError
912+
913+
con._request_authentication = _request_authentication
914+
915+
with pytest.raises(TypeError):
916+
con.connect()
917+
assert not con.open
918+
assert con._rfile is None
919+
assert con._sock is None
920+
assert rfile.closed
921+
assert sock._closed
922+
assert sock.fileno() != fileno # should be set to -1

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.