From 2a27c0bbe4a73c191ebb7143e4d0d3797d82b7ef Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 12:49:04 +0300 Subject: [PATCH 1/2] client: Send the rest of a short io_uring send instead of disconnecting A sendmsg with MSG_WAITALL submitted through io_uring can still complete short: after many poll retries io_uring punts it to io-wq, where it runs with MSG_DONTWAIT. handle_send() took that for a socket error and disconnected a healthy peer. A first CQE with -EAGAIN or -EINTR kept send_list, but only a later queued op resent it, and the failed send's send_free range was never handed to the retry: after a copy send its entries stayed referenced until disconnect, holding every later reply, and after a zero-copy send its notification freed replies that send_list still pointed into. Handle both as a short send: eat the sent prefix, keep the rest in send_list and schedule the client through write_ready_clients, so the next try_send() sends the rest, without any EPOLLOUT switching. The IN/BUF entries of the short send move to the end of send_free and the next send's range starts at them, while the short send keeps REF-only entries in its own range. send_free is freed from the front, so a buffer is freed only when both sends are done and, if zero-copy, released. Add send path tests over a fake ring, so they run without io_uring. --- src/client/messenger.h | 2 + src/client/msgr_send.cpp | 39 +++- src/test/test_msgr_stop.cpp | 382 ++++++++++++++++++++++++++++++++++++ 3 files changed, 415 insertions(+), 8 deletions(-) diff --git a/src/client/messenger.h b/src/client/messenger.h index ee367b76..08a4dc98 100644 --- a/src/client/messenger.h +++ b/src/client/messenger.h @@ -162,6 +162,8 @@ struct osd_client_t std::vector send_list; size_t send_list_size = 0; uint64_t next_send_free = 0; + // Start of the send_free entries of the unsent rest of a short send + uint64_t send_free_rest = 0; std::deque send_free; op_aes_xts_encrypt_t *xts_enc_ctx = NULL; XXH3_state_t* write_csum_state = NULL; diff --git a/src/client/msgr_send.cpp b/src/client/msgr_send.cpp index fbc9ef10..18756987 100644 --- a/src/client/msgr_send.cpp +++ b/src/client/msgr_send.cpp @@ -527,7 +527,9 @@ restart: // Do not copy more data into it, just wait for EPOLLOUT return true; } - uint64_t send_free_from = cl->next_send_free + cl->send_free.size(); + // With io_uring, send_list is only non-empty here with the rest of a short send. + // The sync loop passes next_send_free to handle_send() and doesn't use this range + uint64_t send_free_from = cl->send_list.size() ? cl->send_free_rest : cl->next_send_free + cl->send_free.size(); if (cl->hs) { // Send handshake message @@ -771,6 +773,11 @@ bool osd_messenger_t::handle_send(int result, bool prev, bool more, osd_client_t stop_client(cl->client_id); return false; } + if (result < 0 && !prev) + { + // -EAGAIN or -EINTR: nothing is sent + result = 0; + } if (result >= 0) { if (prev) @@ -784,15 +791,31 @@ bool osd_messenger_t::handle_send(int result, bool prev, bool more, osd_client_t cl->send_list.clear(); cl->send_list_size = 0; } - else if (!use_sync_send_recv) - { - fprintf(stderr, "Client %ju socket write error: expected to send " - "%zu bytes with MSG_WAITALL but sent %u. Disconnecting client\n", cl->client_id, cl->send_list_size, result); - stop_client(cl->client_id); - return false; - } else { + if (!use_sync_send_recv) + { + // MSG_WAITALL doesn't hold when io_uring punts the send to io-wq + fprintf(stderr, "Client %ju: %ssend stopped short at %d of %zu bytes, sending the rest\n", + cl->client_id, more ? "zero-copy " : "", result, cl->send_list_size); + // The next send carries the rest, so it now frees this send's buffers. + // This send keeps only references, so the buffers live until both are released + cl->send_free_rest = cl->next_send_free + cl->send_free.size(); + for (uint64_t i = free_from; i < free_to; i++) + { + msgr_sendp_t sp = cl->send_free[i - cl->next_send_free]; + if (sp.flags & (MSGR_SENDP_IN|MSGR_SENDP_BUF)) + { + cl->send_free[i - cl->next_send_free].flags = MSGR_SENDP_REF; + cl->send_free.push_back(sp); + } + } + if (!more) + { + cl->unref_send_free(free_from, free_to); + } + wakeup_send(cl); + } // Partially sent eat_send_list(cl, result); cl->send_list_size -= result; diff --git a/src/test/test_msgr_stop.cpp b/src/test/test_msgr_stop.cpp index 869764cd..9051a0b2 100644 --- a/src/test/test_msgr_stop.cpp +++ b/src/test/test_msgr_stop.cpp @@ -4,6 +4,7 @@ // Stopping an OSD peer must cancel its operations at once, also while a send to the // peer is stuck on a full socket, as with a powered-off host. // Uses the real messenger over io_uring. Run: test_msgr_stop +// Also checks when the send path frees sent replies, with short and failed sends, over a fake ring. #include #include @@ -15,6 +16,9 @@ #include #include +#include +#include +#include #include #include @@ -260,8 +264,386 @@ static void test_invalid_osd_network() } } +// Send path tests over a fake ring: the test posts the CQEs itself, as ring_loop_t::loop() +// dispatches them, so they also run without io_uring + +class fake_ring_t: public ring_loop_i +{ +public: + io_uring_sqe sqe = {}; + ring_data_t *last = NULL; + void register_consumer(ring_consumer_t*) override {} + void unregister_consumer(ring_consumer_t*) override {} + int register_eventfd() override { return -1; } + io_uring_sqe* get_sqe() override + { + last = new ring_data_t(); + sqe = { .user_data = (uint64_t)last }; + return &sqe; + } + void set_immediate(const std::function & cb) override { cb(); } + int submit() override { return 0; } + int wait() override { return 0; } + unsigned space_left() override { return 1; } + bool has_work() override { return false; } + bool has_sendmsg_zc() override { return true; } + void loop() override {} + void wakeup() override {} + unsigned save() override { return 0; } + void restore(unsigned) override {} +}; + +// The same dispatch as in ring_loop_t::loop() +static void fake_cqe(ring_data_t *d, int res, bool f_more) +{ + if (f_more) + { + d->res = res; + d->more = true; + d->callback(d); + d->prev = true; + d->more = false; + } + else + { + ring_data_t dl; + dl.res = res; + dl.more = false; + dl.prev = d->prev; + dl.callback.swap(d->callback); + delete d; + dl.callback(&dl); + } +} + +struct send_msgr_t: public osd_messenger_t +{ + using osd_messenger_t::try_send; + using osd_messenger_t::min_zerocopy_send_size; + using osd_messenger_t::write_ready_clients; +}; + +struct test_send_t +{ + ring_data_t *d; + bool zc; + int len; +}; + +struct test_reply_t +{ + // Owned by the test, so that the data of a freed reply can't be reused + std::vector data; + std::weak_ptr alive; +}; + +static uint8_t request_data[4096]; + +struct send_env_t +{ + fake_ring_t ring; + timerfd_manager_t *tfd; + send_msgr_t *msgr; + osd_client_t *cl; + uint64_t client_id; + std::deque replies; + std::vector requests; + + send_env_t() + { + tfd = new timerfd_manager_t([](int, bool, std::function) {}); + msgr = new send_msgr_t(); + msgr->tfd = tfd; + msgr->ringloop = ˚ + msgr->has_sendmsg_zc = true; + cl = new osd_client_t(); + client_id = cl->client_id = msgr->next_client_id++; + // Never connected: the fake ring doesn't submit anything + cl->peer_fd = socket(AF_INET, SOCK_STREAM, 0); + assert(cl->peer_fd >= 0); + cl->peer_state = PEER_CONNECTED; + msgr->clients[client_id] = cl; + } + + ~send_env_t() + { + delete msgr; + delete tfd; + for (auto op: requests) + delete op; + } + + int reply(int n_iov, int iov_len = 16) + { + osd_op_t *op = new osd_op_t(); + op->op_type = OSD_OP_IN; + op->req.hdr.opcode = OSD_OP_READ; + op->reply.hdr.magic = SECONDARY_OSD_REPLY_MAGIC; + auto alive = std::make_shared(); + op->callback = [alive](osd_op_t*) {}; + replies.push_back({ .data = std::vector(n_iov*iov_len), .alive = alive }); + for (int i = 0; i < n_iov; i++) + op->iov.push_back(replies.back().data.data() + i*iov_len, iov_len); + cl->write_ops.push_back(op); + return replies.size()-1; + } + + void request() + { + osd_op_t *op = new osd_op_t(); + op->op_type = OSD_OP_OUT; + op->req.hdr.magic = SECONDARY_OSD_OP_MAGIC; + op->req.hdr.opcode = OSD_OP_SEC_WRITE_STABLE; + op->req.sec_rw.len = sizeof(request_data); + op->iov.push_back(request_data, sizeof(request_data)); + cl->write_ops.push_back(op); + requests.push_back(op); + } + + bool freed(int r) + { + return replies[r].alive.expired(); + } + + // Nothing may be freed while send_list still points into it + void check() + { + if (!msgr->clients.count(client_id)) + return; + for (auto & r: replies) + { + if (!r.alive.expired()) + continue; + for (auto & iov: cl->send_list) + assert((uint8_t*)iov.iov_base < r.data.data() || (uint8_t*)iov.iov_base >= r.data.data()+r.data.size()); + } + } + + test_send_t submitted(bool zc) + { + assert(ring.last); + assert((ring.sqe.opcode == IORING_OP_SENDMSG_ZC) == zc); + test_send_t s = { .d = ring.last, .zc = zc, .len = (int)cl->send_list_size }; + ring.last = NULL; + check(); + return s; + } + + test_send_t send(bool zc) + { + msgr->min_zerocopy_send_size = zc ? 0 : -1; + msgr->try_send(cl); + return submitted(zc); + } + + // The next send of a client scheduled for the normal write path + test_send_t send_scheduled(bool zc) + { + assert(msgr->write_ready_clients.size() == 1 && msgr->write_ready_clients[0] == client_id); + msgr->min_zerocopy_send_size = zc ? 0 : -1; + msgr->send_replies(); + return submitted(zc); + } + + // A zero-copy send gets its first CQE with F_MORE and a release notification later + void complete(test_send_t & s, int res, bool f_more) + { + fake_cqe(s.d, res, f_more); + check(); + } + + void complete(test_send_t & s) + { + complete(s, s.len, s.zc); + } + + void release(test_send_t & s) + { + fake_cqe(s.d, 0, false); + check(); + } + + // A later reply is freed at once and nothing stays in send_free + void check_next_send() + { + int r = reply(1); + auto s = send(false); + complete(s); + assert(freed(r)); + assert(!cl->send_free.size()); + assert(!msgr->write_ready_clients.size()); + } +}; + +// The reply must be freed only after the last step +static void run_steps(send_env_t & e, int r, const std::vector> & steps) +{ + for (auto & step: steps) + { + assert(!e.freed(r)); + step(); + } + assert(e.freed(r)); +} + +// A send that stopped short or failed with -EAGAIN/-EINTR doesn't disconnect the client: +// the rest is sent through the normal write path, and the reply is freed only when the send +// and the retry are both done and, if zero-copy, released +static void test_send_rest(bool zc, int res, bool notify, bool retry_zc, int release_pos) +{ + printf("test_send_rest(%s, %d, %s, retry %s, release at %d)\n", zc ? "zerocopy" : "copy", res, + notify ? "notify" : "no notify", retry_zc ? "zerocopy" : "copy", release_pos); + send_env_t e; + int r = e.reply(4, 1024); + auto s1 = e.send(zc); + e.complete(s1, res, notify); + assert(e.msgr->clients.count(e.client_id) && e.cl->peer_state == PEER_CONNECTED); + int rest = s1.len - (res > 0 ? res : 0); + assert(e.cl->send_list_size == rest); + auto s2 = e.send_scheduled(retry_zc); + assert(s2.len == rest); + std::vector> steps = { [&]() { e.complete(s2); } }; + if (retry_zc) + steps.push_back([&]() { e.release(s2); }); + if (notify) + steps.insert(steps.begin() + release_pos, [&]() { e.release(s1); }); + run_steps(e, r, steps); + e.check_next_send(); +} + +// The rest of a short send stops short again +static void test_send_rest_twice() +{ + printf("test_send_rest_twice\n"); + send_env_t e; + int r = e.reply(4, 1024); + auto s1 = e.send(true); + e.complete(s1, 64, true); + auto s2 = e.send_scheduled(true); + e.complete(s2, 1000, true); + auto s3 = e.send_scheduled(false); + assert(s3.len == s1.len-1064); + run_steps(e, r, { [&]() { e.release(s2); }, [&]() { e.complete(s3); }, [&]() { e.release(s1); } }); + e.check_next_send(); +} + +// stop_client() while releases of a short send are pending: releases after the stop are +// dropped, the last one destroys the client and frees the reply once +static void test_stop_with_rest(bool retried) +{ + printf("test_stop_with_rest(%s)\n", retried ? "retried" : "not retried"); + send_env_t e; + int r = e.reply(4, 1024); + auto s1 = e.send(true); + e.complete(s1, 64, true); + test_send_t s2 = {}; + std::vector> steps = { [&]() { e.release(s1); } }; + if (retried) + { + s2 = e.send_scheduled(true); + e.complete(s2); + steps.push_back([&]() { e.release(s2); }); + } + e.msgr->stop_client(e.client_id); + assert(e.msgr->clients.count(e.client_id)); + run_steps(e, r, steps); + assert(!e.msgr->clients.count(e.client_id)); + e.msgr->send_replies(); + assert(!e.ring.last); +} + +// Out-of-order zero-copy releases free nothing ahead of an unreleased send; +// request-only sends leave nothing in send_free +static void test_release_order() +{ + printf("test_release_order\n"); + send_env_t e; + int r[3]; + test_send_t s[3]; + for (int i = 0; i < 3; i++) + { + r[i] = e.reply(1); + s[i] = e.send(true); + e.complete(s[i]); + } + e.request(); + auto q = e.send(true); + e.complete(q); + e.release(s[1]); + e.release(q); + e.release(s[2]); + assert(!e.freed(r[0]) && !e.freed(r[1]) && !e.freed(r[2])); + e.release(s[0]); + assert(e.freed(r[0]) && e.freed(r[1]) && e.freed(r[2])); + assert(!e.cl->send_free.size()); + for (int i = 0; i < 10; i++) + { + e.request(); + auto t = e.send(true); + e.complete(t); + e.release(t); + assert(!e.cl->send_free.size()); + } + e.check_next_send(); +} + +// A reply split at IOV_MAX between two sends is freed only after both are done and released +static void test_iov_max_split(bool first_zc, bool second_zc, bool reverse) +{ + printf("test_iov_max_split(%s, %s%s)\n", first_zc ? "zerocopy" : "copy", + second_zc ? "zerocopy" : "copy", reverse ? ", reversed" : ""); + send_env_t e; + int r = e.reply(1500); + auto s1 = e.send(first_zc); + assert(e.cl->write_op); + e.complete(s1); + auto s2 = e.send_scheduled(second_zc); + assert(!e.cl->write_op); + std::vector> steps = { [&]() { e.complete(s2); } }; + if (first_zc) + steps.push_back([&]() { e.release(s1); }); + if (second_zc) + steps.insert(reverse ? steps.begin()+1 : steps.end(), [&]() { e.release(s2); }); + run_steps(e, r, steps); + e.check_next_send(); +} + +static void test_send_free() +{ + // -EAGAIN/-EINTR on a copy send + for (int err: { -EAGAIN, -EINTR }) + for (bool retry_zc: { false, true }) + test_send_rest(false, err, false, retry_zc, 0); + // -EAGAIN on a zero-copy send, without and with a release notification + for (bool retry_zc: { false, true }) + { + test_send_rest(true, -EAGAIN, false, retry_zc, 0); + for (int pos = 0; pos <= 1+retry_zc; pos++) + test_send_rest(true, -EAGAIN, true, retry_zc, pos); + } + // Short sends stopped inside the header and inside the data of a reply + for (int sent: { 64, OSD_PACKET_SIZE+1000 }) + { + for (bool retry_zc: { false, true }) + { + test_send_rest(false, sent, false, retry_zc, 0); + for (int pos = 0; pos <= 1+retry_zc; pos++) + test_send_rest(true, sent, true, retry_zc, pos); + } + } + test_send_rest_twice(); + test_stop_with_rest(false); + test_stop_with_rest(true); + test_release_order(); + for (bool first_zc: { false, true }) + for (bool second_zc: { false, true }) + test_iov_max_split(first_zc, second_zc, false); + test_iov_max_split(true, true, true); +} + int main(int narg, char *args[]) { + test_send_free(); test_invalid_osd_network(); test_keepalive_catch_up(); test_keepalive_after_stall(); -- 2.47.3 From 17793798f44efbb4c827c9bf37256ed7cee0fb1c Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 22:48:28 +0300 Subject: [PATCH 2/2] client: Drop the log line for a short io_uring send, it is a normal case now --- src/client/msgr_send.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/client/msgr_send.cpp b/src/client/msgr_send.cpp index 18756987..4bc193f0 100644 --- a/src/client/msgr_send.cpp +++ b/src/client/msgr_send.cpp @@ -796,8 +796,6 @@ bool osd_messenger_t::handle_send(int result, bool prev, bool more, osd_client_t if (!use_sync_send_recv) { // MSG_WAITALL doesn't hold when io_uring punts the send to io-wq - fprintf(stderr, "Client %ju: %ssend stopped short at %d of %zu bytes, sending the rest\n", - cl->client_id, more ? "zero-copy " : "", result, cl->send_list_size); // The next send carries the rest, so it now frees this send's buffers. // This send keeps only references, so the buffers live until both are released cl->send_free_rest = cl->next_send_free + cl->send_free.size(); -- 2.47.3