client: Send the rest of a short io_uring send instead of disconnecting #174

Open
koolkhel wants to merge 2 commits from koolkhel/vitastor:zc-short-send into master
Contributor

Description

Two related problems in handle_send() with io_uring, fixed without touching EPOLLOUT:

  1. A short send disconnected a healthy peer. A sendmsg / sendmsg_zc with MSG_WAITALL submitted through io_uring can still complete short: after APOLL_MAX_RETRY (128) poll retries io_uring punts it to io-wq, where it runs with MSG_DONTWAIT and returns what it managed to send. Observed on kernel 6.8 with CPU-starved OSDs and large replies (the punt came after exactly 127 poll re-arms); handle_send() took it for a socket error and disconnected the peer, which re-peered its PGs.
  2. A first CQE with -EAGAIN/-EINTR (the same punt when nothing was sent) 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 on the connection), and after a zero-copy send its notification freed replies that send_list still pointed into.

Fix:

  • -EAGAIN/-EINTR on the first CQE is a zero-byte short send.
  • A short send no longer disconnects: it logs one line, eats the sent prefix (eat_send_list, as the sync path does), keeps the rest in send_list and calls wakeup_send(), so the next try_send() from send_replies() sends the rest. No EPOLLOUT switching: io_uring polls the new SQE itself.
  • send_free: a REF bit can't express two holders, so the short send's IN/BUF entries move to the end of send_free (the next send's range starts at them, via the new osd_client_t::send_free_rest), while the short send keeps REF-only entries in its own range — the same form as the existing IOV_MAX-split placeholder. Since send_free is freed from the front, a buffer is freed only after both sends are done and, if zero-copy, released. Full sends are unchanged.

Tested: new tests in test_msgr_stop over a fake ring, so they run without io_uring: -EAGAIN on copy and zero-copy sends (with and without a notification, at every position), short sends stopping inside a reply's header and inside its data with the rest sent as copy and as zero-copy and the releases in every order, a retry that stops short again, stop_client() with releases pending, plus ordering tests for out-of-order releases and the IOV_MAX split. Without the fix they fail (disconnect, use-after-free via the notification, entry referenced forever, no reschedule); with it the whole test_msgr_stop and osd_test pass, also under ASan.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description Two related problems in `handle_send()` with io_uring, fixed without touching EPOLLOUT: 1. **A short send disconnected a healthy peer.** A `sendmsg` / `sendmsg_zc` with `MSG_WAITALL` submitted through io_uring can still complete short: after `APOLL_MAX_RETRY` (128) poll retries io_uring punts it to io-wq, where it runs with `MSG_DONTWAIT` and returns what it managed to send. Observed on kernel 6.8 with CPU-starved OSDs and large replies (the punt came after exactly 127 poll re-arms); `handle_send()` took it for a socket error and disconnected the peer, which re-peered its PGs. 2. **A first CQE with -EAGAIN/-EINTR** (the same punt when nothing was sent) 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 on the connection), and after a zero-copy send its notification freed replies that `send_list` still pointed into. Fix: - -EAGAIN/-EINTR on the first CQE is a zero-byte short send. - A short send no longer disconnects: it logs one line, eats the sent prefix (`eat_send_list`, as the sync path does), keeps the rest in `send_list` and calls `wakeup_send()`, so the next `try_send()` from `send_replies()` sends the rest. No EPOLLOUT switching: io_uring polls the new SQE itself. - `send_free`: a REF bit can't express two holders, so the short send's IN/BUF entries move to the end of `send_free` (the next send's range starts at them, via the new `osd_client_t::send_free_rest`), while the short send keeps REF-only entries in its own range — the same form as the existing IOV_MAX-split placeholder. Since `send_free` is freed from the front, a buffer is freed only after both sends are done and, if zero-copy, released. Full sends are unchanged. Tested: new tests in `test_msgr_stop` over a fake ring, so they run without io_uring: -EAGAIN on copy and zero-copy sends (with and without a notification, at every position), short sends stopping inside a reply's header and inside its data with the rest sent as copy and as zero-copy and the releases in every order, a retry that stops short again, `stop_client()` with releases pending, plus ordering tests for out-of-order releases and the IOV_MAX split. Without the fix they fail (disconnect, use-after-free via the notification, entry referenced forever, no reschedule); with it the whole `test_msgr_stop` and `osd_test` pass, also under ASan. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-24 12:53:48 +03:00
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.
vitalif reviewed 2026-11-25 20:55:13 +03:00
@@ -796,0 +796,4 @@
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",
Owner

print можно убрать, если это теперь штатная ситуация

print можно убрать, если это теперь штатная ситуация
vitalif marked this conversation as resolved
koolkhel added 1 commit 2026-11-25 22:48:31 +03:00
Author
Contributor

print можно убрать, если это теперь штатная ситуация (msgr_send.cpp:799)

Убрал: 17793798f4

> print можно убрать, если это теперь штатная ситуация (msgr_send.cpp:799) Убрал: https://git.yourcmc.ru/koolkhel/vitastor/commit/17793798f44efbb4c827c9bf37256ed7cee0fb1c
vitalif requested changes 2026-11-27 18:51:48 +03:00
vitalif left a comment
Owner

Ну фикс лучше чем у китайца, но на самом деле есть ещё вопрос, почему требуется такой сложный фикс, причём срабатывающий при любом send, не только при частичном, когда по идее в коде уже есть путь обработки частичного send (// Partially sent), почему нельзя просто включить его и всё? (ну м.б ещё какие-то микро правки доделать, если надо условно состояние отправки разбудить или что-то подобное)

Ну фикс лучше чем у китайца, но на самом деле есть ещё вопрос, почему требуется такой сложный фикс, причём срабатывающий при любом send, не только при частичном, когда по идее в коде уже есть путь обработки частичного send (// Partially sent), почему нельзя просто включить его и всё? (ну м.б ещё какие-то микро правки доделать, если надо условно состояние отправки разбудить или что-то подобное)
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u zc-short-send:koolkhel-zc-short-send
git checkout koolkhel-zc-short-send
Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#174
No description provided.