From 17ba884e884fb9a9f285c89b6602171ba20d04f6 Mon Sep 17 00:00:00 2001 From: Perdix of Mars Date: Tue, 8 Sep 2026 13:10:17 +0000 Subject: [PATCH] Add retry handler to handle EINTR signals when writing to the console. --- src/console.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 196815a..158280a 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -1,5 +1,6 @@ #include "consolepp/console.hpp" +#include #include #include #include @@ -67,6 +68,35 @@ extent get_console_size(int descriptor) return {window_size.ws_col, window_size.ws_row}; } +// ========================================================================== +// WRITE_ALL +// ========================================================================== +void write_all(boost::asio::posix::stream_descriptor &stream, bytes const &data) +{ + auto const *position = data.data(); + auto remaining = data.size(); + + while (remaining != 0) + { + auto error = boost::system::error_code{}; + auto const bytes_written = stream.write_some( + boost::asio::const_buffer{position, remaining}, error); + + if (error == boost::asio::error::interrupted) + { + continue; + } + + if (error) + { + throw boost::system::system_error{error, "write_some"}; + } + + position += bytes_written; + remaining -= bytes_written; + } +} + } // namespace // ========================================================================== @@ -150,8 +180,7 @@ void console::write(bytes data) { if (!data.empty()) { - stream_.write_some( - boost::asio::const_buffer{&*data.begin(), data.size()}); + write_all(stream_, data); } }