From bdaf9a2907df0a009865667c95afaa2ee13a78e2 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 8 Jul 2024 16:07:13 +0800 Subject: [PATCH] handle pipe signal --- include/cinatra/ylt/coro_io/coro_io.hpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/cinatra/ylt/coro_io/coro_io.hpp b/include/cinatra/ylt/coro_io/coro_io.hpp index bf5bd7e6..08a3cb67 100644 --- a/include/cinatra/ylt/coro_io/coro_io.hpp +++ b/include/cinatra/ylt/coro_io/coro_io.hpp @@ -518,6 +518,11 @@ inline std::error_code connect(executor_t &executor, } #ifdef __linux__ +// this help us ignore SIGPIPE when send data to a unexpected closed socket. +inline auto pipe_signal_handler = [] { + std::signal(SIGPIPE, SIG_IGN); + return 0; +}(); inline async_simple::coro::Lazy> async_sendfile(asio::ip::tcp::socket &socket, int fd, off_t offset, @@ -534,8 +539,8 @@ async_sendfile(asio::ip::tcp::socket &socket, int fd, off_t offset, while (true) { // Try the system call. errno = 0; - int n = ::sendfile(socket.native_handle(), fd, &offset, - std::min(std::size_t{65536}, least_bytes)); + ssize_t n = ::sendfile(socket.native_handle(), fd, &offset, + std::min(std::size_t{65536}, least_bytes)); ec = asio::error_code(n < 0 ? errno : 0, asio::error::get_system_category()); least_bytes -= ec ? 0 : n;