From 011f8c48388cec0e90f5c0709356943f2ef56a0a Mon Sep 17 00:00:00 2001 From: waffle2k Date: Mon, 15 Jun 2026 16:43:06 -0700 Subject: [PATCH] Stop logging normal client disconnects as exceptions Clients that connect and close without sending a request -- health checks (nc ... < /dev/null), port scanners, reset connections -- made async_read_some throw eof, which the catch block logged as "echo exception: End of file [asio.misc:2 ...]", spamming the logs. Read with as_tuple so the error comes back as an error_code instead of an exception: on any read error just return quietly. Writes likewise use as_tuple and ignore errors (best-effort reply). The try/catch remains only as a backstop for genuinely unexpected exceptions. --- main.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 94082b4..d6e1c89 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -41,8 +42,14 @@ awaitable echo(tcp::socket socket, std::string client_addr, bool trackable } char data[1024]; - auto bytes_read = - co_await socket.async_read_some(boost::asio::buffer(data), deferred); + auto [read_ec, bytes_read] = co_await socket.async_read_some( + boost::asio::buffer(data), boost::asio::as_tuple(deferred)); + if (read_ec) { + // Client hung up before sending a request: health checks (which connect + // and immediately close), port scanners, and reset connections all land + // here. This is normal -- don't log it as an exception. + co_return; + } std::string username(data, bytes_read); // Remove trailing \r\n characters while (!username.empty() && @@ -70,12 +77,15 @@ awaitable echo(tcp::socket socket, std::string client_addr, bool trackable std::printf("finger miss from %s for '%s' (not tracked)\n", client_addr.c_str(), username.c_str()); } - co_await async_write( - socket, boost::asio::buffer(std::string("No plan found\r\n")), - deferred); + // Best-effort reply; ignore write errors (the client may have already + // gone away). + co_await async_write(socket, + boost::asio::buffer(std::string("No plan found\r\n")), + boost::asio::as_tuple(deferred)); co_return; } - co_await async_write(socket, boost::asio::buffer(response), deferred); + co_await async_write(socket, boost::asio::buffer(response), + boost::asio::as_tuple(deferred)); co_return; } catch (std::exception &e) { std::printf("echo exception: %s\n", e.what());