Skip to content

Commit 6f15db1

Browse files
Thom747MichalPrincNXP
authored andcommitted
Fix possible AttributeError and OSError on calling TCPTransport.close()
1 parent 88869ae commit 6f15db1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

erpc_python/erpc/transport.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def __init__(self, host, port, isServer):
144144
self._port = port
145145
self._isServer = isServer
146146
self._sock = None
147+
self._socket_lock = threading.Lock()
147148

148149
if self._isServer:
149150
self._serverThread = threading.Thread(target=self._serve)
@@ -170,9 +171,17 @@ def _serve(self):
170171
def close(self):
171172
if self._isServer:
172173
self._serverSockEventStart.clear()
173-
self._sock.shutdown(SHUT_RDWR)
174-
self._sock.close()
175-
self._sock = None
174+
175+
with self._socket_lock:
176+
if self._sock is not None:
177+
try:
178+
self._sock.shutdown(SHUT_RDWR)
179+
self._sock.close()
180+
except OSError:
181+
# May be raised by the OS if the socket was closed externally,
182+
# thus invalidating the file descriptor.
183+
pass
184+
self._sock = None
176185

177186
def _base_send(self, message):
178187
if self._isServer:

0 commit comments

Comments
 (0)