Skip to content

Commit 1ad4e1a

Browse files
committed
DRAFT - Use TCP KEEPALIVE to force closing socket with offline remotes.
Close issue #4729.
1 parent a23330d commit 1ad4e1a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/tcp_listener.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,33 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
246246
if (options.priority != 0)
247247
set_socket_priority (sock, options.priority);
248248

249+
// OK. Let's talk about this lazy implementation.
250+
//
251+
// On Linux, after ::accept(), TCP_KEEPALIVE is not set by default.
252+
// Moreover, there is no way for an application to retreive this socket
253+
// and then, no way to close() or use setsockopt() on it.
254+
// So, the only possibility to avoid socket leakage is TCP_KEEPALIVE.
255+
//
256+
// Regarding the values, the default TCP ones are far too huge (> 2h).
257+
// The values below seem reasonnable for a ZYRE application :
258+
// 120 + 10 * 10 = 220s (3mn 40s)
259+
//
260+
// Note:
261+
// A ZYRE client application will reset its side after about 30s, so
262+
// no worry about such applications.
263+
// What about other applications ?
264+
//
265+
int rc = tune_tcp_keepalives (sock, 1, 10, 120, 10);
266+
if (rc != 0) {
267+
#ifdef ZMQ_HAVE_WINDOWS
268+
const int rc = closesocket (sock);
269+
wsa_assert (rc != SOCKET_ERROR);
270+
#else
271+
rc = ::close (sock);
272+
errno_assert (rc == 0);
273+
#endif
274+
return retired_fd;
275+
}
276+
249277
return sock;
250278
}

0 commit comments

Comments
 (0)