Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions tests/libs/util/socket_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FAIL

If I get it right, by default FAIL throws an exception. In this case dtor is not invoked and socket is getting leaked.

As issue exists in original code, consider this non-blocking. We may need to fix it later.

}

// Validate expected bind error.
if (expected_bind_error != 0) {
if (_bind_succeeded) {
Expand Down
Loading