def retry_on_address_already_in_use_error(func):
    """Reruns a test if it sees "Address already in use" error."""

    @wraps(func)
    def wrapper(*args, **kwargs):
        tries_remaining = 10
        while True:
            try:
                return func(*args, **kwargs)
            except RuntimeError as error:
                if str(error) == "Address already in use":
                    tries_remaining -= 1
                    if tries_remaining == 0:
                        raise
                    time.sleep(random.random())
                    continue
                raise

    return wrapper
Reruns a test if it sees "Address already in use" error.
tests/cpu/common_utils.py