Skip to content

Commit f1fafb8

Browse files
authored
Merge pull request #2 from mujin/rs-ensure-socket-close
Ensure socket is closed immediately on exception
2 parents d055326 + e611985 commit f1fafb8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

python/mujinasync/asynctcp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ def SpinOnce(self, timeout=0):
306306
# connect for client
307307
for client in self._clients:
308308
if not client._connections:
309+
clientSocket = None
309310
try:
310311
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
311312
clientSocket.connect(client._endpoint)
@@ -314,7 +315,9 @@ def SpinOnce(self, timeout=0):
314315
clientSocket = client._sslContext.wrap_socket(clientSocket, server_side=False)
315316
clientSocket.setblocking(0) # TODO: deferred non-blocking after connect finishes, not ideal
316317
except Exception as e:
317-
log.exception('error while trying to create client connection: %s', e)
318+
if clientSocket:
319+
clientSocket.close()
320+
log.exception('error while trying to create client connection to %s: %s', client._endpoint, e)
318321
continue
319322
connection = client._connectionClass(connectionSocket=clientSocket, remoteAddress=client._endpoint)
320323
client._connections.append(connection)

0 commit comments

Comments
 (0)