Skip to content

fix(cluster): prevent assertion abort on reconnect with --cluster-mode --reconnect-on-error (#377) - #378

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-reconnect-assertion-377
May 14, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-reconnect-assertion-377

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Make cluster_client::connect() idempotent so the reconnect path doesn't grow m_key_index_pools out of sync with m_connections and fire Assertion \m_connections.size() == m_key_index_pools.size()', which previously SIGABRT'd the whole memtier_benchmarkprocess the first time any worker thread had to reconnect under--cluster-mode --reconnect-on-error`.
  • Adds a regression test (tests/test_reconnections.py::test_reconnect_cluster_mode_no_assertion) that runs the bug-triggering workload, kills connections mid-run via CLIENT KILL TYPE normal, and asserts memtier never aborts.

Root cause

cluster_client::connect() (cluster_client.cpp:109) unconditionally did:

key_index_pool *key_idx_pool = new key_index_pool;
m_key_index_pools.push_back(key_idx_pool);
assert(m_connections.size() == m_key_index_pools.size());

On the first call this matched the single main connection in m_connections (assertion holds). But shard_connection::handle_reconnect_timer_event re-enters connect() on every reconnect attempt without clearing either vector — so m_key_index_pools grew by one each time while m_connections stayed the same, and the next reconnect tripped the assertion.

Fix

Only push the main connection's key_index_pool on the first connect; reuse it on subsequent reconnects. Per-shard pools added later via create_shard_connection() are unchanged.

Verification

Repro (before fix):

./memtier_benchmark -s 127.0.0.1 -p 7000 --cluster-mode \
  -t 4 -c 2 --ratio=1:0 --key-pattern=P:P --random-data \
  --data-size=512 --reconnect-on-error --test-time=20
# in another shell: redis-cli -p 7000 CLIENT KILL TYPE normal
# -> memtier_benchmark: cluster_client.cpp:121: virtual int cluster_client::connect(): Assertion \`m_connections.size() == m_key_index_pools.size()' failed.
# -> SIGABRT

After fix: same workload + repeated kills runs to completion, all worker threads reconnect, no aborts.

Regression test: stashed the fix, rebuilt, ran the new test → fails with exit_code == -6 (SIGABRT). Restored the fix, rebuilt, ran the new test → passes.

Test plan

  • Reproduced original assertion failure on master
  • Verified fix eliminates the assertion under repeated CLIENT KILL TYPE normal
  • New test test_reconnect_cluster_mode_no_assertion passes against fixed binary
  • New test fails against unfixed binary (regression coverage confirmed)
  • clang-format --dry-run --Werror cluster_client.cpp clean

Fixes #377

🤖 Generated with Claude Code


Note

Medium Risk
Touches core cluster reconnect bookkeeping; while small, mistakes could cause crashes or incorrect request routing during reconnect scenarios.

Overview
Prevents memtier_benchmark from aborting under --cluster-mode --reconnect-on-error by making cluster_client::connect() only create/push the main connection’s key_index_pool on the first connect, keeping m_key_index_pools aligned with m_connections across reconnects.

Adds a cluster-only regression test (test_reconnect_cluster_mode_no_assertion) that runs memtier with reconnect enabled, forcibly drops connections via CLIENT KILL TYPE normal, and asserts the process exits without the m_connections.size() == m_key_index_pools.size() assertion or SIGABRT.

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

…377)

cluster_client::connect() unconditionally pushed a new key_index_pool every
time it was called. On the initial call that matched the single main
connection in m_connections and the invariant held, but the reconnect path
in shard_connection::handle_reconnect_timer_event re-enters connect()
without clearing m_connections or m_key_index_pools — so the next call
grew the pools vector by one and triggered:

  Assertion `m_connections.size() == m_key_index_pools.size()' failed.

aborting the whole memtier_benchmark process the first time any worker
thread had to reconnect under --cluster-mode --reconnect-on-error.

Only push the main connection's key_index_pool on the first connect.
Subsequent reconnects reuse the existing pool, keeping the parallel
vectors aligned.

Adds tests/test_reconnections.py::test_reconnect_cluster_mode_no_assertion
that runs the cluster-mode workload, kills connections mid-run, and
asserts memtier never aborts and no assertion line appears in stderr.

Fixes #377

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@jit-ci

jit-ci Bot commented May 14, 2026

Copy link
Copy Markdown

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

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.

Assertion m_connections.size() == m_key_index_pools.size() fires on first reconnect when --cluster-mode and --reconnect-on-error are combined

2 participants