Skip to content

Commit f93ab4f

Browse files
committed
session: Keep a local socket to avoid multithreading issues
The SessionMonitor thread can call session.close() on a separate thread which can result in session.__sock being reset to None unexpectedly: keep a local copy to avoid problems.
1 parent e93a8e4 commit f93ab4f

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

pynuodb/session.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ def __init__(self, host, port=None, service="Identity", timeout=None,
135135
if strToBool(tls_options.get('allowSRPFallback', "False")):
136136
# fall back to SRP, do not attempt to TLS handshake
137137
self.close()
138-
self._open_socket(connect_timeout, self.__address, self.__port, af, read_timeout)
138+
self._open_socket(connect_timeout, self.__address,
139+
self.__port, af, read_timeout)
139140
else:
140141
raise
141142

@@ -429,17 +430,20 @@ def __readFully(self, msgLength, timeout=None):
429430

430431
def close(self, force=False):
431432
""" Close the current socket connection with the server """
432-
if self.__sock is None:
433+
# The SessionMonitor thread also calls close() which resets
434+
# self.__sock to None, so cache the value here.
435+
sock = self.__sock
436+
if sock is None:
433437
return
434438

435439
try:
436440
if force:
437441
try:
438-
self.__sock.shutdown(socket.SHUT_RDWR)
442+
sock.shutdown(socket.SHUT_RDWR)
439443
except (OSError, socket.error):
440444
# On MacOS this can raise "Socket is not connected"
441445
pass
442-
self.__sock.close()
446+
sock.close()
443447
finally:
444448
self.__sock = None
445449

0 commit comments

Comments
 (0)