From 25469e8f1a5d65f38847316098386e2193b44570 Mon Sep 17 00:00:00 2001 From: Himbeer Date: Mon, 24 Mar 2025 11:45:56 +0100 Subject: Set read/write timeouts on TcpStreams This prevents indefinite blocking and ensures that timeout conditions get detected. --- src/main.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main.rs b/src/main.rs index e122e74..b6b98c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -145,6 +145,9 @@ fn handle_conn_check( raddr: SocketAddr, inbound_healthy: &mut bool, ) -> io::Result<()> { + conn.set_read_timeout(Some(TCP_TIMEOUT))?; + conn.set_write_timeout(Some(TCP_TIMEOUT))?; + let mut buf = [0; 4]; loop { match conn.read_exact(&mut buf) { @@ -167,6 +170,9 @@ fn handle_conn_check( } fn handle_conn_monitor(mut conn: TcpStream, raddr: SocketAddr) -> io::Result<()> { + conn.set_read_timeout(Some(TCP_TIMEOUT))?; + conn.set_write_timeout(Some(TCP_TIMEOUT))?; + let mut buf = [0; 4]; loop { match conn.read_exact(&mut buf) { @@ -205,6 +211,8 @@ fn check_connectivity( if let Some(mut conn4) = conn4 { conn4.write_all(b"GET / HTTP/1.1")?; + conn4.set_read_timeout(Some(TCP_TIMEOUT))?; + conn4.set_write_timeout(Some(TCP_TIMEOUT))?; loop { match conn4.read(&mut buf) { @@ -239,6 +247,8 @@ fn check_connectivity( if let Some(mut conn6) = conn6 { conn6.write_all(b"GET / HTTP/1.1")?; + conn6.set_read_timeout(Some(TCP_TIMEOUT))?; + conn6.set_write_timeout(Some(TCP_TIMEOUT))?; loop { match conn6.read(&mut buf) { -- cgit v1.2.3