Skip to content

fix(cluster): defensive clamp on m_reqs_generated subtraction at three sites - #437

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-reqs-generated-clamp
Jun 2, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-reqs-generated-clamp

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Jun 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Three sites in cluster_client.cpp (connect_shard_connection L244, handle_cluster_slots retire-shard branch L504, handle_moved L1070) subtract empty_staged.size() from m_reqs_generated with no underflow guard.
  • PR fix(monitor-cluster): end-to-end backpressure for staged monitor replay #399 introduced a defensive clamp at hold_pipeline for the same reason; this PR matches that pattern at the three sibling sites.
  • Today the invariant (entries in empty_staged are popped before m_reqs_generated is decremented) prevents underflow, but it is undocumented. A future refactor could silently wrap m_reqs_generated to 2^64 and permanently wedge a --requests-bounded run.

Fix

Each bare subtraction is replaced with:

const size_t n = empty_staged.size();
m_reqs_generated -= (m_reqs_generated >= n) ? n : m_reqs_generated;

An inline comment at each site documents the invariant and references hold_pipeline as the canonical pattern.

Test plan

  • autoreconf -ivf && ./configure && make -j — clean build, no new warnings.
  • Run against a live Redis cluster with --monitor-input and trigger topology changes (MOVED, slot rebalance, shard reconnect) to confirm no hang with --requests.
  • Verify no behavioural change when empty_staged is empty (the common path).

Refs: Review 16 (analysis that the subtraction sites are safe today but unguarded).

🤖 Generated with Claude Code


Note

Low Risk
Small defensive accounting change in cluster client teardown paths; no auth, protocol, or routing logic changes.

Overview
When cluster topology or shard state clears staged monitor queues, the benchmark already subtracts those entries from m_reqs_generated so --requests runs do not wait for responses that will never arrive.

This change replaces three unchecked subtractions in cluster_client.cpp—in connect_shard_connection, the retire-shard path in handle_cluster_slots, and handle_moved—with the same defensive clamp used in hold_pipeline: subtract at most the current counter value so unsigned underflow cannot wrap m_reqs_generated and wedge a bounded run. Inline comments document the invariant and point to hold_pipeline as the canonical pattern.

Behavior on today's code paths should be unchanged; the guard is for future refactors that might double-compensate or skew the counter.

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

… sites

PR #399 introduced a >= clamp at hold_pipeline; the three sibling sites
(connect_shard_connection, handle_cluster_slots, handle_moved) subtract
empty_staged.size() with no such guard. Today the invariant -- entries
in empty_staged are pre-decrement -- holds, but a refactor could break
it silently. Matching the pattern makes the invariant explicit and
future-proofs against subtle underflow to 2^64.

Refs: 2.4 review #12 (Review 1 + Review 16).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@jit-ci

jit-ci Bot commented Jun 2, 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.

1 participant