From 0ab99862cd60d90deab80fafceda3371bb943d4f Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Mon, 2 Nov 2026 09:57:17 +0000 Subject: [PATCH 1/3] fix(net): preserve errno status for receive failures --- docs/architecture/networking.md | 5 +- include/bycorf/net/tcp_stream.h | 4 ++ src/io/io_uring_backend.cpp | 6 ++- tests/tcp_buffered_read_test.cpp | 83 ++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) diff --git a/docs/architecture/networking.md b/docs/architecture/networking.md index 22f1dd4..86ac846 100644 --- a/docs/architecture/networking.md +++ b/docs/architecture/networking.md @@ -52,7 +52,10 @@ Linux owns the TCP state and NIC receive processing. An accepted Linux socket may be assigned to another worker before its session starts. The service uses round-robin assignment within the service's selected worker set, then retains that worker for the session. io_uring -completions make received byte ranges available to the stream. +completions make received byte ranges available to the stream. Plaintext reads +expose orderly EOF as a successful zero-byte result after queued data drains; +receive failures retain errno-derived status codes and diagnostics, with +connection reset/abort reported as unavailable. Connection closure cancels an armed receive by its operation identity before closing the descriptor. A receive may otherwise retain the kernel socket after `close`, delaying peer EOF and session replacement. Cancellation preserves a diff --git a/include/bycorf/net/tcp_stream.h b/include/bycorf/net/tcp_stream.h index 12a495e..89e6832 100644 --- a/include/bycorf/net/tcp_stream.h +++ b/include/bycorf/net/tcp_stream.h @@ -200,6 +200,10 @@ class TcpStream { // with their own bounded ingress queues. absl::Status SetReadAhead(bool enabled) noexcept; + // Read available bytes. For a nonempty destination, orderly peer EOF + // returns zero after queued bytes drain. Plaintext socket resets/aborts + // return kUnavailable; kernel receive failures retain errno-derived status + // codes and diagnostic messages rather than becoming generic kUnknown. Task> ReadSome(std::span buffer); // Wait for initial data, then copy currently queued receive buffers up to // buffer.size(). Never wait to fill the destination; its size bounds work diff --git a/src/io/io_uring_backend.cpp b/src/io/io_uring_backend.cpp index 4479947..51e245c 100644 --- a/src/io/io_uring_backend.cpp +++ b/src/io/io_uring_backend.cpp @@ -818,8 +818,10 @@ void IoUringBackend::HandleMultishotRecv(Connection* connection, connection->recv_eof_ = true; notify_reader = true; } else if (cqe->res < 0 && cqe->res != -ECANCELED && cqe->res != -ENOBUFS) { - connection->last_error_ = - absl::Status(absl::StatusCode::kUnknown, "recv multishot failed"); + // Preserve errno classification and diagnostics for both receive modes. + // In particular, a reset/aborted connection is unavailable to its reader, + // not an unclassified protocol or application failure. + connection->last_error_ = absl::ErrnoToStatus(-cqe->res, "recv failed"); notify_reader = true; } else if (cqe->res > 0) { connection->last_error_ = absl::Status( diff --git a/tests/tcp_buffered_read_test.cpp b/tests/tcp_buffered_read_test.cpp index a3f7a89..f829808 100644 --- a/tests/tcp_buffered_read_test.cpp +++ b/tests/tcp_buffered_read_test.cpp @@ -14,10 +14,12 @@ * limitations under the License. */ +#include #include #include #include +#include #include #include #include @@ -25,6 +27,7 @@ #include #include #include +#include #include "absl/cleanup/cleanup.h" #include "bycorf/io/storage.h" @@ -251,6 +254,86 @@ absl::Status CheckCloseDoesNotShrink(bycorf::Worker& worker) { "close discards queued buffers without shrinking storage"); } +// Use real loopback TCP: AF_UNIX socket pairs do not model TCP RST. +bycorf::Task CheckPeerClose(bycorf::Worker& worker, bool reset, + bool buffered) { + const int listener = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (listener < 0) co_return absl::ErrnoToStatus(errno, "socket listener"); + absl::Cleanup close_listener = [&] { close(listener); }; + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (bind(listener, reinterpret_cast(&address), sizeof(address)) != + 0 || + listen(listener, 1) != 0) + co_return absl::ErrnoToStatus(errno, "listen"); + socklen_t length = sizeof(address); + if (getsockname(listener, reinterpret_cast(&address), &length) != + 0) + co_return absl::ErrnoToStatus(errno, "getsockname"); + int peer = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (peer < 0) co_return absl::ErrnoToStatus(errno, "socket peer"); + absl::Cleanup close_peer = [&] { + if (peer >= 0) close(peer); + }; + if (connect(peer, reinterpret_cast(&address), sizeof(address)) != + 0) + co_return absl::ErrnoToStatus(errno, "connect"); + const int accepted = + accept4(listener, nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC); + if (accepted < 0) co_return absl::ErrnoToStatus(errno, "accept"); + bycorf::Connection initial; + initial.worker_ = &worker; + initial.file_.fd_ = accepted; + initial.closed_ = false; + auto* connection = worker.AddConnection(std::move(initial)); + bycorf::TcpStream stream(connection); + auto storage = stream.BorrowStorage(); + absl::Cleanup close_stream = [&] { (void)stream.Close(); }; + std::array output; + // Establish a live receive before closing; drain the payload so no unread + // bytes can change a nominal FIN into a reset. + auto status = Send(peer, "hello"); + if (!status.ok()) co_return status; + auto read = co_await stream.ReadSome(output); + if (!read.ok()) co_return read.status(); + if (*read != 5) co_return absl::InternalError("unexpected greeting size"); + if (reset) { + linger option{1, 0}; + if (setsockopt(peer, SOL_SOCKET, SO_LINGER, &option, sizeof(option)) != 0) + co_return absl::ErrnoToStatus(errno, "SO_LINGER"); + } + close(std::exchange(peer, -1)); + read = buffered ? co_await stream.ReadSomeBuffered(output) + : co_await stream.ReadSome(output); + if (!reset) + co_return Check(read.ok() && *read == 0, "FIN returns successful EOF"); + status = Check(absl::IsUnavailable(read.status()), + "RST returns unavailable instead of unknown"); + if (!status.ok()) co_return status; + co_return Check(read.status().message() == + absl::ErrnoToStatus(ECONNRESET, "recv failed").message(), + "RST preserves the kernel error diagnostic"); +} + +bycorf::Task CheckPeerCloses(bycorf::Worker& worker) { + for (bool buffered : {false, true}) { + for (bool reset : {false, true}) { + auto status = co_await CheckPeerClose(worker, reset, buffered); + if (!status.ok()) co_return status; + } + } + co_return absl::OkStatus(); +} + +TEST(TcpBufferedRead, PeerCloseStatus) { + bycorf::test::RunOnWorker(CheckPeerCloses); +} + +TEST(TcpBufferedRead, OneShotPeerCloseStatus) { + bycorf::test::RunOnWorker(CheckPeerCloses, {.recv_buffer_count_ = 0}); +} + TEST(TcpBufferedRead, QueuedAndLiveReads) { bycorf::test::RunOnWorker(CheckReads); } From 597e8ed089aba53e60201a2774573b99a2b44fb4 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Mon, 2 Nov 2026 10:04:41 +0000 Subject: [PATCH 2/3] refactor(net): keep receive error fix minimal --- docs/architecture/networking.md | 5 +---- include/bycorf/net/tcp_stream.h | 4 ---- src/io/io_uring_backend.cpp | 3 --- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/docs/architecture/networking.md b/docs/architecture/networking.md index 86ac846..22f1dd4 100644 --- a/docs/architecture/networking.md +++ b/docs/architecture/networking.md @@ -52,10 +52,7 @@ Linux owns the TCP state and NIC receive processing. An accepted Linux socket may be assigned to another worker before its session starts. The service uses round-robin assignment within the service's selected worker set, then retains that worker for the session. io_uring -completions make received byte ranges available to the stream. Plaintext reads -expose orderly EOF as a successful zero-byte result after queued data drains; -receive failures retain errno-derived status codes and diagnostics, with -connection reset/abort reported as unavailable. +completions make received byte ranges available to the stream. Connection closure cancels an armed receive by its operation identity before closing the descriptor. A receive may otherwise retain the kernel socket after `close`, delaying peer EOF and session replacement. Cancellation preserves a diff --git a/include/bycorf/net/tcp_stream.h b/include/bycorf/net/tcp_stream.h index 89e6832..12a495e 100644 --- a/include/bycorf/net/tcp_stream.h +++ b/include/bycorf/net/tcp_stream.h @@ -200,10 +200,6 @@ class TcpStream { // with their own bounded ingress queues. absl::Status SetReadAhead(bool enabled) noexcept; - // Read available bytes. For a nonempty destination, orderly peer EOF - // returns zero after queued bytes drain. Plaintext socket resets/aborts - // return kUnavailable; kernel receive failures retain errno-derived status - // codes and diagnostic messages rather than becoming generic kUnknown. Task> ReadSome(std::span buffer); // Wait for initial data, then copy currently queued receive buffers up to // buffer.size(). Never wait to fill the destination; its size bounds work diff --git a/src/io/io_uring_backend.cpp b/src/io/io_uring_backend.cpp index 51e245c..f4dbbb0 100644 --- a/src/io/io_uring_backend.cpp +++ b/src/io/io_uring_backend.cpp @@ -818,9 +818,6 @@ void IoUringBackend::HandleMultishotRecv(Connection* connection, connection->recv_eof_ = true; notify_reader = true; } else if (cqe->res < 0 && cqe->res != -ECANCELED && cqe->res != -ENOBUFS) { - // Preserve errno classification and diagnostics for both receive modes. - // In particular, a reset/aborted connection is unavailable to its reader, - // not an unclassified protocol or application failure. connection->last_error_ = absl::ErrnoToStatus(-cqe->res, "recv failed"); notify_reader = true; } else if (cqe->res > 0) { From c659ecfb84c32b1aba8b33c108d62860c5ebfa23 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Mon, 2 Nov 2026 10:12:10 +0000 Subject: [PATCH 3/3] test(net): separate coroutine reads for GCC compatibility --- tests/tcp_buffered_read_test.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/tcp_buffered_read_test.cpp b/tests/tcp_buffered_read_test.cpp index f829808..0c502c9 100644 --- a/tests/tcp_buffered_read_test.cpp +++ b/tests/tcp_buffered_read_test.cpp @@ -304,12 +304,16 @@ bycorf::Task CheckPeerClose(bycorf::Worker& worker, bool reset, co_return absl::ErrnoToStatus(errno, "SO_LINGER"); } close(std::exchange(peer, -1)); - read = buffered ? co_await stream.ReadSomeBuffered(output) - : co_await stream.ReadSome(output); + if (buffered) { + read = co_await stream.ReadSomeBuffered(output); + } else { + read = co_await stream.ReadSome(output); + } if (!reset) co_return Check(read.ok() && *read == 0, "FIN returns successful EOF"); - status = Check(absl::IsUnavailable(read.status()), - "RST returns unavailable instead of unknown"); + status = Check( + absl::IsUnavailable(read.status()), + "RST returns unavailable; actual status: " + read.status().ToString()); if (!status.ok()) co_return status; co_return Check(read.status().message() == absl::ErrnoToStatus(ECONNRESET, "recv failed").message(),