Skip to content

Reconnect only the dropped cluster connection, and only once - #560

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-end-of-run-hang
Oct 12, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-end-of-run-hang

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #558.

Problem

In cluster mode, a live connection could be connected again. That frees its bufferevent and opens a fresh socket, while its in-flight requests stay in the pipeline and in the pending count. Their bytes are lost, so:

  • later replies on that connection are matched to the wrong requests;
  • the connection never becomes idle, so at the end of the run disconnect_all() is never reached and memtier never exits.

Two paths did this:

  • Every shard connection's reconnect timer called the client-wide connect(), which always reconnects the main connection, whatever its state.
  • A CLUSTER SLOTS refresh reconnects every disconnected shard connection, including one whose own reconnect timer is still pending. When that timer fired, it connected the now-live connection again.

Fix

  • New connections_manager::reconnect(conn_id). Standalone clients keep reconnecting their only connection.
  • cluster_client::reconnect() reconnects the dropped connection to its own endpoint and requests a slot map refresh over the main connection (which the old path got as a side effect). It does nothing for a connection that is no longer disconnected.
  • shard_connection::connect() cancels a pending reconnect timer.

How it was found

From CI, with temporary diagnostics (since removed): on a hang, the test dumped every thread's backtrace and, via gdb, each client's counters and each connection's state, buffers, pipeline and key pools, plus ss for both TCP ends.

  • The worker sat in epoll_wait with no timers.
  • A connection had 4 requests in its pipeline, an empty output buffer, and a socket that had never sent a byte. First on the main connection (the first path), then, after fixing that, on shard connections (the second path).

Testing

  • New test_cluster_run_exits_with_reconnects_pending (cluster only): kills client connections on all masters around the --test-time deadline, 10 rounds with varied timing, and fails if memtier doesn't exit.
  • Before: a dedicated CI cell hung in every run (rounds 0–7 of 10).
  • After:
    • 3 dedicated 40-round CI runs (2 plain, 1 TLS): no hangs;
    • full CI and the sanitizer cluster cells green.
  • Skipped over TLS: a separate, rarer TLS-only stall (one hang in about 60 TLS rounds, under ASAN) remains and is tracked in TLS cluster: worker stalls after reconnects (unsent output, handshake never completes) #562.

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster reconnection and CLUSTER SLOTS refresh coordination during failovers; incorrect behavior could still strand pending requests or delay topology recovery, but scope is limited to --reconnect-on-error cluster paths with new regression coverage.

Overview
Fixes cluster-mode hangs (#558) where reconnecting a live socket left in-flight requests counted as pending on a new connection that never sent them, so the benchmark never reached shutdown.

Reconnect path: Adds per-connection connections_manager::reconnect(conn_id). Shard timers call that instead of client-wide connect(), which used to always reopen the main connection. For a dropped shard, cluster_client::reconnect() avoids redialing the main (and losing its pipeline); it wakes the main connection to refresh topology via CLUSTER SLOTS when appropriate. Successful connect() clears a pending reconnect timer; CLUSTER SLOTS skips immediate redial when is_reconnect_pending() so backoff and max-attempt limits are honored.

Failover spin guard: When routing is wedged, hold_pipeline treats a primary as “live” only if is_ready_for_reads(), not merely TCP-connected, so the event loop can process CLUSTER SLOTS during failover.

Test: New cluster integration test kills client connections near --test-time and asserts memtier still exits (skipped on TLS; #562).

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

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

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

Thanks for this. The write-up of how the hang was found makes the two paths easy to follow. I like that the new test is shown hanging on master (rounds 0–7 of 10) and passing with the fix. That's the bar this kind of fix should meet. A few questions:

  • Who retries when the handoff fails synchronously? When reconnect() returns 1, the timer path logs and stops. From then on the refresh owns the reconnect. But handle_cluster_slots() ignores what connect_shard_connection() returns, and that call can fail before any timer is armed: a getaddrinfo failure, setup_socket failing, or bufferevent_socket_connect returning -1. In those cases I think the shard stays conn_disconnected with no timer and no further attempts, and --max-reconnect-attempts never counts the failure. I may be wrong, but the old path already ignored this result too (main reconnected, then the refresh redialed). Now that reconnect() explicitly takes on this job, though, should a failed refresh-side connect re-arm the shard's own timer? The same question applies if the CLUSTER SLOTS reply never arrives or comes back as an error.

  • Main drops out of read routing on every shard reconnect. Each deferred shard reconnect calls set_cluster_slots() on main, which sets main's m_cluster_slots back to setup_none. That takes main's own shard group out of is_ready_for_reads() for one round-trip. With several shards dropping at once, I guess that's one refresh, since the setup_done guard keeps it to one. Still, did you consider whether this matters with --read-preference set to something other than primary?

  • Spin guard change. The hold_pipeline() change from conn_connected to is_ready_for_reads() is described in the commit message but not in the PR body. Is it covered by the new test, i.e. does the test still hang without that hunk? The long comment near select_target_conn explains why writes deliberately avoid the stricter predicate because of exactly these transient setup_none windows. Yielding more often there is probably harmless, since the counter resets, but it would be good to confirm it's intended.

  • shard_connection::connect() now clears a pending reconnect timer. That looks right, and doing it only after socket setup succeeds is a nice touch. Just checking: should m_reconnect_attempts and the backoff also be reset here, or is that still left to the connected handler on purpose?

  • Nitpicking: the reflowed comment in hold_pipeline() has one line well over the 120-column limit. make format-check will probably catch it anyway.

The core fix looks right to me, and the != conn_disconnected early return in cluster_client::reconnect() is the guard that closes the double-connect. This is cluster reconnect lifecycle code, so it may be worth a second look from whoever owns cluster routing. I'd mainly like the first question answered before this merges.

@fcostaoliveira
fcostaoliveira force-pushed the fix/cluster-end-of-run-hang branch from c9f4ebc to 3702b7a Compare October 12, 2026 16:05
@fcostaoliveira fcostaoliveira changed the title Fix cluster run not exiting when reconnects are pending at --test-time Reconnect only the dropped cluster connection, and only once Oct 12, 2026
@fcostaoliveira
fcostaoliveira marked this pull request as ready for review October 12, 2026 16:05

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3702b7a. Configure here.

Comment thread cluster_client.cpp Resolved
@fcostaoliveira
fcostaoliveira force-pushed the fix/cluster-end-of-run-hang branch 3 times, most recently from dab76b5 to 8c374d1 Compare October 12, 2026 22:22
A shard connection's reconnect timer called connect(), which in cluster
mode reconnects the main connection. That discarded main's in-flight
requests, which stayed counted as pending, so the run never ended
(#558). A CLUSTER SLOTS refresh could also reconnect a shard connection
while its own reconnect timer was pending, and the timer then
reconnected the live connection, discarding its requests the same way.

Add connections_manager::reconnect(conn_id). Its default reconnects the
main connection as before. In cluster mode it leaves a dropped shard
connection to a CLUSTER SLOTS refresh over the main connection, which
it wakes if idle: the refresh reconnects the shard with its current
role, or not at all if the node left the cluster. A refresh leaves a
connection whose reconnect timer is pending to that timer, so the
backoff and --max-reconnect-attempts still apply during a failover, and
reconnect() does nothing to a connection that is no longer
disconnected.

The no-route spin guard now yields unless a primary is ready to serve
requests, not just connected: during a failover the live primaries can
all be waiting for a CLUSTER SLOTS reply, which only the event loop
reads, so the worker spun forever instead.

Fixes #558.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

Cluster mode: run sometimes never exits when reconnects are pending at --test-time

1 participant