From e3645aa2f7a36355e92eb2d4e6c6b5001c085fa4 Mon Sep 17 00:00:00 2001 From: Simon Murray Date: Tue, 21 Jun 2016 15:46:53 +0100 Subject: [PATCH] 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 --- lib/base/tcpsocket.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/base/tcpsocket.cpp b/lib/base/tcpsocket.cpp index b438dcd73..02b105441 100644 --- a/lib/base/tcpsocket.cpp +++ b/lib/base/tcpsocket.cpp @@ -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) {