def _close_socket(self):
"""Close the socket safely."""
# Is a no-op if the socket is already closed.
if self._sock is None:
return
try:
# Get the write lock, so we can be certain data sending
# in another thread is sent.
with self._lock:
self._sock.shutdown(socket.SHUT_RDWR)
self._sock.close()
except socket.error:
# Socket is already closed.
# That's fine, just a no-op.
pass
except Exception as error:
# Paranoia
log.warning('error closing socket (%s)', error)
finally:
self._sock = None
评论列表
文章目录