From 99b6d8134f65c3f11aeb0fa26c1264a5d9a6be62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=A1k?= Date: Fri, 16 Aug 2024 14:44:19 +0200 Subject: [PATCH] unix and tcp writer: set timeout for sending and receiving on socket to ensure that the io thread does not block when destroyed --- fstrm/tcp_writer.c | 19 +++++++++++++++++++ fstrm/unix_writer.c | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/fstrm/tcp_writer.c b/fstrm/tcp_writer.c index 4a9bc868c..0f7b85884 100644 --- a/fstrm/tcp_writer.c +++ b/fstrm/tcp_writer.c @@ -129,6 +129,25 @@ fstrm__tcp_writer_op_open(void *obj) } #endif +#if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO) + static const struct timeval timeout = { + .tv_sec = 1, + .tv_usec = 0 + }; +#if defined(SO_RCVTIMEO) + if (setsockopt(w->fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0) { + close(w->fd); + return fstrm_res_failure; + } +#endif +#if defined(SO_SNDTIMEO) + if (setsockopt(w->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) { + close(w->fd); + return fstrm_res_failure; + } +#endif +#endif + /* Connect the TCP socket. */ if (connect(w->fd, (struct sockaddr *) &w->ss, w->ss_len) < 0) { close(w->fd); diff --git a/fstrm/unix_writer.c b/fstrm/unix_writer.c index 72c91672f..12cd4a895 100644 --- a/fstrm/unix_writer.c +++ b/fstrm/unix_writer.c @@ -115,6 +115,25 @@ fstrm__unix_writer_op_open(void *obj) } #endif +#if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO) + static const struct timeval timeout = { + .tv_sec = 1, + .tv_usec = 0 + }; +#if defined(SO_RCVTIMEO) + if (setsockopt(w->fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0) { + close(w->fd); + return fstrm_res_failure; + } +#endif +#if defined(SO_SNDTIMEO) + if (setsockopt(w->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) { + close(w->fd); + return fstrm_res_failure; + } +#endif +#endif + /* Connect the AF_UNIX socket. */ if (connect(w->fd, (struct sockaddr *) &w->sa, sizeof(w->sa)) < 0) { close(w->fd);