Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport(tcp): retry bind operations #428

Merged
merged 1 commit into from
Apr 20, 2024
Merged
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
23 changes: 21 additions & 2 deletions src/transport/tcp/RecvSocket.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <faabric/transport/tcp/RecvSocket.h>
#include <faabric/transport/tcp/SocketOptions.h>
#include <faabric/util/logging.h>
#include <faabric/util/macros.h>

#ifdef FAABRIC_USE_SPINLOCK
#include <emmintrin.h>
Expand Down Expand Up @@ -31,9 +32,27 @@
setNonBlocking(connFd);
}

// Attempt to bind a number of times to account for situations where
// another messge is being migrated and is vacating the port
int numRetries = 5;
int pollPeriodMs = 200;
int numTry = 0;

SPDLOG_TRACE("Binding TCP socket to {}:{} (fd: {})", host, port, connFd);
int ret = ::bind(connFd, addr.get(), sizeof(sockaddr_in));
if (ret) {
while ((ret != 0) && (numTry < numRetries)) {
numTry++;
SPDLOG_TRACE("Retrying binding to {}:{} (attempt: {}/{}, ret: {})",
host,
port,
numTry,
numRetries,
ret);
SLEEP_MS(pollPeriodMs);
ret = ::bind(connFd, addr.get(), sizeof(sockaddr_in));

Check warning on line 52 in src/transport/tcp/RecvSocket.cpp

View check run for this annotation

Codecov / codecov/patch

src/transport/tcp/RecvSocket.cpp#L44-L52

Added lines #L44 - L52 were not covered by tests
}

if (ret != 0) {
SPDLOG_ERROR("Error binding to {}:{} (fd: {}): {} (ret: {})",
host,
port,
Expand Down Expand Up @@ -70,7 +89,7 @@
struct pollfd polledFds[1];
polledFds[0].fd = connFd;
polledFds[0].events = POLLIN;
int pollTimeoutMs = 2000;
int pollTimeoutMs = SocketTimeoutMs;
int numReady = ::poll(polledFds, 1, pollTimeoutMs);
if (numReady < 1) {
SPDLOG_ERROR(
Expand Down