Check the errno instead of the message

pull/21607/head
Lidi Zheng 5 years ago
parent 305defc7cb
commit ebd2c841e1
  1. 13
      src/python/grpcio_tests/tests/unit/framework/common/__init__.py

@ -15,14 +15,15 @@
import contextlib import contextlib
import os import os
import socket import socket
import errno
_DEFAULT_SOCK_OPTION = socket.SO_REUSEADDR if os.name == 'nt' else socket.SO_REUSEPORT _DEFAULT_SOCK_OPTION = socket.SO_REUSEADDR if os.name == 'nt' else socket.SO_REUSEPORT
_UNRECOVERABLE_ERRORS = ('Address already in use',) _UNRECOVERABLE_ERRNOS = (errno.EADDRINUSE, errno.ENOSR)
def _exception_is_unrecoverable(e): def _is_unrecoverable_os_error(e):
for error in _UNRECOVERABLE_ERRORS: for error in _UNRECOVERABLE_ERRNOS:
if error in str(e): if error == e.errno:
return True return True
return False return False
@ -61,9 +62,9 @@ def get_socket(bind_address='localhost',
if listen: if listen:
sock.listen(1) sock.listen(1)
return bind_address, sock.getsockname()[1], sock return bind_address, sock.getsockname()[1], sock
except socket.error as socket_error: except OSError as os_error:
sock.close() sock.close()
if _exception_is_unrecoverable(socket_error): if _is_unrecoverable_os_error(os_error):
raise raise
else: else:
continue continue

Loading…
Cancel
Save