Fix hanging API connections

There was a problem identified where an upstream API connection was found hanging waiting
for a TLS handshake to complete.  Seeingly the TCP connection was ESTABLISHED locally but
not cleanly terminated remotely.  The Socket events layer never triggered the TLS handshake
oddly.  This however enables TCP keep alive packets to detect broken connections, raising
EPOLLERR and breaking the deadlock condition so that the agent will attempt to reconnect
at a later time.

fixes #12003

Signed-off-by: Gunnar Beutner <gunnar.beutner@netways.de>
This commit is contained in:
Simon Murray 2016-06-21 15:46:53 +01:00 committed by Gunnar Beutner
parent ba24f7b912
commit e3645aa2f7
1 changed files with 11 additions and 0 deletions

View File

@ -177,6 +177,17 @@ void TcpSocket::Connect(const String& node, const String& service)
continue;
}
int optval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) != 0) {
#ifdef _WIN32
error = WSAGetLastError();
#else /* _WIN32 */
error = errno;
#endif /* _WIN32 */
Log(LogWarning, "TcpSocket")
<< "setsockopt() unable to enable TCP keep-alives with error code " << rc;
}
rc = connect(fd, info->ai_addr, info->ai_addrlen);
if (rc < 0) {