Skip to content

Commit

Permalink
DRAFT - Use TCP KEEPALIVE to force closing socket with offline remotes.
Browse files Browse the repository at this point in the history
Close issue zeromq#4729.
  • Loading branch information
stephan57160 committed Dec 20, 2024
1 parent a23330d commit 1ad4e1a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,33 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
if (options.priority != 0)
set_socket_priority (sock, options.priority);

// OK. Let's talk about this lazy implementation.
//
// On Linux, after ::accept(), TCP_KEEPALIVE is not set by default.
// Moreover, there is no way for an application to retreive this socket
// and then, no way to close() or use setsockopt() on it.
// So, the only possibility to avoid socket leakage is TCP_KEEPALIVE.
//
// Regarding the values, the default TCP ones are far too huge (> 2h).
// The values below seem reasonnable for a ZYRE application :
// 120 + 10 * 10 = 220s (3mn 40s)
//
// Note:
// A ZYRE client application will reset its side after about 30s, so
// no worry about such applications.
// What about other applications ?
//
int rc = tune_tcp_keepalives (sock, 1, 10, 120, 10);
if (rc != 0) {
#ifdef ZMQ_HAVE_WINDOWS
const int rc = closesocket (sock);
wsa_assert (rc != SOCKET_ERROR);
#else
rc = ::close (sock);
errno_assert (rc == 0);
#endif
return retired_fd;
}

return sock;
}

0 comments on commit 1ad4e1a

Please sign in to comment.