Correctly return errors when failing to connect

Triggered by investigating

    xmon: could not read message type: unexpected EOF, will reconnect
    xmon: connected to xmon REDACTED
    Undertaker: hard abort - running abort handlers
    Uncaught exception thrown: SyscallException(Xmon.cpp@186, 9/EBADF=Bad file descriptor in void Xmon::run()): setsockopt

which caused crashes in shards/CDC.
This commit is contained in:
Francesco Mazzoli
2023-07-26 12:38:16 +00:00
parent 3206d7a564
commit d918df0fcc
2 changed files with 15 additions and 3 deletions

View File

@@ -16,6 +16,10 @@ static std::string explicitGenerateErrString(const std::string& what, int err, c
return ss.str();
}
static std::string generateErrString(const std::string& what, int err) {
return explicitGenerateErrString(what, err, (std::string(translateErrno(err)) + "=" + safe_strerror(err)).c_str());
}
int connectToHost(
const std::string& host,
uint16_t port,
@@ -52,11 +56,15 @@ int connectToHost(
throw SYSCALL_EXCEPTION("socket");
}
if (synRetries >= 0) {
setsockopt(infoSock, IPPROTO_TCP, TCP_SYNCNT, &synRetries, sizeof(synRetries));
if (synRetries > 0) {
if (setsockopt(infoSock, IPPROTO_TCP, TCP_SYNCNT, &synRetries, sizeof(synRetries)) < 0) {
errString = generateErrString("setsockopt", errno);
continue;
}
}
if (connect(infoSock, info->ai_addr, info->ai_addrlen) < 0) {
errString = generateErrString("connect", errno);
close(infoSock);
} else {
fd = infoSock;
@@ -64,5 +72,9 @@ int connectToHost(
}
}
// we've managed to connect, clear earlier errors
if (fd != -1) {
errString = "";
}
return fd;
}

View File

@@ -41,7 +41,7 @@ static ShuckleSock shuckleSock(const std::string& host, uint16_t port, Duration
// We retry upstream anyway, and we want prompt termination of `connect`.
// If we don't do this, mistakenly trying to connect to an iceland shuckle (currently
// the default) will hang for a long time.
sock.fd = connectToHost(host, port, 0, errString);
sock.fd = connectToHost(host, port, 1, errString);
if (sock.fd >= 0) {
struct timeval tv;