From ebd2c841e1de5f5479fcd059168725cbf7565fcb Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Fri, 10 Jan 2020 14:21:47 -0800 Subject: [PATCH] Check the errno instead of the message --- .../tests/unit/framework/common/__init__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/python/grpcio_tests/tests/unit/framework/common/__init__.py b/src/python/grpcio_tests/tests/unit/framework/common/__init__.py index 083828104af..bb1aa62745d 100644 --- a/src/python/grpcio_tests/tests/unit/framework/common/__init__.py +++ b/src/python/grpcio_tests/tests/unit/framework/common/__init__.py @@ -15,14 +15,15 @@ import contextlib import os import socket +import errno _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): - for error in _UNRECOVERABLE_ERRORS: - if error in str(e): +def _is_unrecoverable_os_error(e): + for error in _UNRECOVERABLE_ERRNOS: + if error == e.errno: return True return False @@ -61,9 +62,9 @@ def get_socket(bind_address='localhost', if listen: sock.listen(1) return bind_address, sock.getsockname()[1], sock - except socket.error as socket_error: + except OSError as os_error: sock.close() - if _exception_is_unrecoverable(socket_error): + if _is_unrecoverable_os_error(os_error): raise else: continue