diff --git a/tests/libs/util/socket_helper.cpp b/tests/libs/util/socket_helper.cpp index 7cdb930b7c..aaa1f4d8a5 100644 --- a/tests/libs/util/socket_helper.cpp +++ b/tests/libs/util/socket_helper.cpp @@ -114,7 +114,11 @@ _base_socket::_base_socket( local_addr.ss_family = address_family; INETADDR_SET_PORT((PSOCKADDR)&local_addr, htons(port)); - // Retry bind operation a few times if it fails with WSAENOBUFS (10055) error as it may be transient. + auto is_transient_error = [](int err) { + return err == WSAENOBUFS || err == WSAEADDRNOTAVAIL || err == WSAEADDRINUSE; + }; + + // Retry bind operation a few times if it fails with transient network errors (10055, 10049, 10048). for (int i = 0; i < 5; ++i) { error = bind(socket, (PSOCKADDR)&local_addr, sizeof(local_addr)); if (error == 0) { @@ -128,19 +132,23 @@ _base_socket::_base_socket( // If expecting a bind error, capture it and validate later. if (expected_bind_error != 0) { _bind_succeeded = false; - // Still retry on WSAENOBUFS as it may be transient. - if (_actual_bind_error != WSAENOBUFS) { + // Still retry on transient errors. + if (!is_transient_error(_actual_bind_error)) { break; } } else { - // Default behavior: FAIL on non-transient errors. - if (_actual_bind_error != WSAENOBUFS) { + // Default behavior: Allow retries on transient network errors. + if (!is_transient_error(_actual_bind_error)) { FAIL("Failed to bind socket with error: " << _actual_bind_error); } } Sleep(1000); } + if (expected_bind_error == 0 && !_bind_succeeded) { + FAIL("Failed to bind socket after retries with error: " << _actual_bind_error); + } + // Validate expected bind error. if (expected_bind_error != 0) { if (_bind_succeeded) {