Skip to content

Uncount dropped in-flight requests so clients don't stall after a reconnect - #556

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/reconnect-interval-dropped-requests
Oct 12, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/reconnect-interval-dropped-requests

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes the nightly Soak / Stress failure on master. S4 (soak/test_connection_churn.py) has failed every night since 2026-10-02.

Problem

When a connection drops, shard_connection::disconnect() deletes its in-flight requests, but they stay counted in the client's m_reqs_generated. They are never processed, so generated − processed stays above the real in-flight count from then on:

  • --reconnect-interval: hold_pipeline() holds once (processed % interval) + (generated − processed) reaches the interval. After a reconnect the client never sends again.
  • Cluster mode: the in-flight budget (pipeline × connections) fills with requests that will never complete.
  • --requests: generated reaches the target while processed never does, so the client stops short.

In all three cases the worker's event loop runs out of events and the run fails with client(s) stopped before reaching their stop condition. Before #546 the worker was silently re-run, which hid this.

Fix

  • Uncount dropped workload requests: those discarded without --retry-on-error, and those --retry-on-error cannot replay because they have no captured bytes (enqueue_retry() never takes those, so none was already counted as processed).
  • client::dec_reqs_generated() never takes generated below processed, because hold_pipeline() subtracts the two without a guard. On connections_manager it is pure virtual, like its siblings.
  • A new is_workload_request() (an exhaustive switch) classifies request types, for both the drain and push_req()'s routed-ops tally.

Testing

  • New regression test: test_dropped_in_flight_requests_do_not_stall_client. One client, --reconnect-interval=10 --reconnect-on-error, four CLIENT KILL waves. It fails on master and passes with the fix, 3/3 each way locally. Independent runs gave 14/14 failures without the fix and 16/16 passes with it, including TLS. It is standalone only: cluster mode rejects --reconnect-interval, and a separate, pre-existing end-of-run hang (below) would make a cluster variant flaky.
  • Full standalone suite and unit tests: 351/351 and 3/3.
  • S4 soak scenario, reproduced manually (-t 8 -c 50 --reconnect-interval=10 --reconnect-on-error, kill every 30 s, 300 s):
    • master: exit 1 at about 91 s, all 8 threads ended.
    • fix: exit 0 on 2 full runs, plus a 120 s run killing every 3 s (about 5,400 reconnects).
  • Cluster mode (3 masters, --reconnect-on-error, kills every 3–5 s):
    • master: exit 1 on every run. --requests stopped at 540k of 800k operations.
    • fix: exit 0 on every run. --requests processed exactly 800,000.
  • --requests with kills, plus pipeline 16 and several reconnect-interval values: master exit 1, fix exit 0 with an exact client-side count. The server may see slightly more commands than requested, at most about pipeline × number of drops, because a request whose reply was lost is generated again. That matches what --retry-on-error already does.
  • Sanitizers: ASAN/LSAN/UBSan and TSAN churn runs are clean. The only TSAN reports are the existing suppressed stats races.
  • Asserts: enabled in all builds. assert(m_pipeline->size() == 0) on the voluntary reconnect holds under kills every 20–30 ms, with AUTH/SELECT/HELLO setup in flight.

Not changed here (follow-ups)

  • --retry-on-error accounting. A request answered with a retryable error is counted as processed, then counted again when its replay completes, so processed can exceed generated. Requests that drain_replay_queue_after_reconnect() drops after --max-retries are not uncounted. replay_request() clears m_claimed_by_retry, so it isn't possible to tell which dropped replays were already counted. That needs its own fix. --retry-on-error --max-retries=0 with kills still fails, on master and with this fix.
  • Cluster mode end-of-run hang. With reconnects still pending at --test-time, memtier sometimes never exits. Seen on master and with this fix.

🤖 Generated with Claude Code


Note

Medium Risk
Changes generated-request accounting on disconnect and reconnect paths, which directly drives pipeline gating and stop conditions; regression-tested but behavior-sensitive for cluster and --requests runs.

Overview
Fixes clients that stop sending after a reconnect when in-flight workload requests were discarded but still counted in m_reqs_generated, which inflated generated − processed and could wedge hold_pipeline() (e.g. --reconnect-interval), cluster in-flight budgets, and --requests stop conditions.

On shard_connection::disconnect(), dropped workload requests (plain discard or --retry-on-error replays with no captured bytes) now call dec_reqs_generated(), which only reduces the in-flight portion and never lowers generated below processed. is_workload_request() centralizes workload vs connection-setup classification for that drain path and for push_req() routed-op counting. A standalone regression test kills connections during --reconnect-interval and asserts the run completes without "stopped before reaching their stop condition".

Reviewed by Cursor Bugbot for commit d1fad6e. Bugbot is set up for automated code reviews on this repo. Configure here.

…onnect

When a connection drops, disconnect() deletes its in-flight requests, but
they stay counted in the client's m_reqs_generated. Nothing processes them,
so generated stays ahead of processed for good:

- With --reconnect-interval, hold_pipeline() holds once
  (processed % interval) + (generated - processed) reaches the interval,
  so after a reconnect the client stops sending.
- In cluster mode, the in-flight budget (pipeline * connections) fills up
  with requests that will never complete.
- With --requests, generated reaches the target while processed never
  does, so the client stops short.

In each case the worker's event loop runs out of events. Since #546 the
run then fails with "client(s) stopped before reaching their stop
condition"; before #546 the worker was silently re-run, which hid this.
It is what has failed the nightly soak S4 (test_connection_churn) on
master every night since 2026-10-02.

Uncount the dropped workload requests: those discarded without
--retry-on-error, and those --retry-on-error cannot replay because they
have no captured bytes. Requests later dropped from the replay queue are
not uncounted here; they may already have been counted as processed when
their error reply was queued for retry.

dec_reqs_generated() never takes generated below processed, which
hold_pipeline() subtracts unguarded. A new is_workload_request() helper
classifies request types for both the drain and push_req()'s routed-ops
tally.

Add a regression test: one client, --reconnect-interval=10
--reconnect-on-error, four CLIENT KILL waves. It fails on master and
passes with the fix. It is standalone only: cluster mode rejects
--reconnect-interval, and a separate, pre-existing end-of-run hang with
reconnects in flight would make a cluster variant flaky.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this, and for the detailed write-up. I traced the accounting and the fix looks right to me. Requests still sitting in the pipeline at disconnect() have been counted as generated but never as processed, so uncounting the discarded workload ones is the correct correction. Clamping at m_reqs_processed also makes sense given the --retry-on-error over-count you describe in the follow-ups. Pulling the classification into is_workload_request() keeps the drain path and the routed_ops tally consistent, which is nice. I have two small questions, neither blocking:

  • cluster_client.cpp already has several hand-rolled m_reqs_generated -= (m_reqs_generated >= n) ? n : m_reqs_generated; decrements (the staged-queue and key-pool compensation paths). Those only clamp at 0, while the new helper clamps at processed. Did you consider routing them through dec_reqs_generated() too, so there's one place that decides how far generated can drop? I'd be fine with that as a follow-up rather than in this PR.
  • The new test covers the --reconnect-interval case. The --requests stall is the other one you reproduced, and it works standalone too, so I wonder if a small companion test is worth adding: --requests=N plus a couple of CLIENT KILL waves, asserting exit 0 and that the client-side count reaches N. That would pin the second symptom with a test that fails on master, rather than relying on the manual runs in the description.

The core change looks solid to me. The two points above are suggestions, not things I'd block the merge on.

(would file this as a comment, not a formal block)

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Answers in order:

  • rt_unknown in the retry branch: intentional. Nothing ever constructs an rt_unknown request; the enum value only appears in switch cases (shard_connection.cpp, cluster_client.cpp). So the retry branch can't see one, and if one ever existed it should not count as a workload request. is_workload_request() is an exhaustive switch, so a new request type has to be classified there explicitly.
  • The cluster_client.cpp clamps: those uncount requests that were generated but never sent (key-index pools, staged queues) when a shard is retired or reconnected. That is a different set from the dropped in-flight requests here, so I kept them as they are; nothing is subtracted twice. Moving them onto dec_reqs_generated() needs a separate look at whether clamping at processed is right for never-sent requests, so I'd rather do it as a follow-up.
  • Teardown at --test-time: invisible. The end-of-run disconnect_all() only runs once all_connections_idle() is true, so nothing is in flight to uncount. Also, m_reqs_generated doesn't feed any reported stats or JSON. Stats and finished() use processed and duration, and get_reqs_generated() is only read by a unit test.
  • Blank lines: fixed in d1fad6e (two blank lines). test_reconnections.py passes locally, 8/8.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Filed the follow-ups listed in the description: #557 (--retry-on-error accounting) and #558 (cluster-mode end-of-run hang with pending reconnects).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant