Skip to content

fix(reconnect,retry): cap exponential backoff at 60s - #433

Merged
fcostaoliveira merged 4 commits into
masterfrom
fix/backoff-cap-60s
Jun 2, 2026
Merged

fcostaoliveira merged 4 commits into
masterfrom
fix/backoff-cap-60s

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Jun 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

shard_connection.cpp had two exponential-backoff multiplications with no upper bound:

  1. Reconnect backoff (m_current_backoff_delay *= reconnect_backoff_factor) — triggered on every connection error when --reconnect-on-error is set.
  2. Retry backoff (m_current_retry_backoff_ms *= retry_backoff_factor) — triggered on each retry drain schedule.

With a factor of 2.0 and --max-reconnect-attempts=0 (unlimited), the delay doubles on every attempt. After ~30 doublings, the next scheduled reconnect or retry is approximately 34 years in the future. event_add() silently absorbs the absurd timeval without error, and the benchmark goes effectively dark — no reconnects, no retries, no visible failure — instead of surfacing the underlying connectivity problem.

Fix

Adds two file-scope constants near the top of shard_connection.cpp:

// Maximum backoff delay enforced after every exponential-backoff multiplication.
// Without a cap, a factor of 2.0 and --max-reconnect-attempts=0 (unlimited)
// causes the delay to double on every attempt: after ~30 doublings the next
// scheduled reconnect or retry fires in ~34 years. event_add() silently absorbs
// that value, making the benchmark go effectively dark instead of surfacing the
// underlying failure.  60 s is a practical upper bound: tight enough to remain
// responsive, large enough to avoid thundering-herd reconnect storms.
static const double MEMTIER_BACKOFF_CAP_SEC = 60.0;
static const double MEMTIER_BACKOFF_CAP_MS  = 60000.0;

After each *= factor multiplication:

  • Reconnect delay is clamped to MEMTIER_BACKOFF_CAP_SEC (60 s)
  • Retry delay is clamped to MEMTIER_BACKOFF_CAP_MS (60 000 ms)

No parser changes, no flag defaults changed, no semantic changes outside the runtime multiplication sites.

Test

Adds test_reconnect_backoff_cap_60s to tests/test_reconnections.py. It targets a guaranteed-closed port with --reconnect-backoff-factor=4.0 --max-reconnect-attempts=0 for 10 s, then:

  • Asserts the process does not crash with SIGABRT.
  • Parses every "attempting reconnection … in X.XX seconds" line from stderr and asserts max(delays) <= 60.0 s.

Refs: 2.4 review finding #13


Note

Low Risk
Runtime-only clamps on reconnect/retry timer delays with no CLI or auth changes; regression test covers the reconnect logging path.

Overview
Caps unbounded exponential backoff on reconnect and retry-on-error paths in shard_connection.cpp so scheduled libevent delays cannot grow without limit (which could leave the benchmark idle for years while errors keep accumulating).

Introduces MEMTIER_BACKOFF_CAP_SEC (60 s) and MEMTIER_BACKOFF_CAP_MS (60 000 ms) and clamps m_current_backoff_delay and m_current_retry_backoff_ms immediately after each *= backoff_factor multiplication in attempt_reconnect, failed reconnect retries, and enqueue_retry.

Adds RLTest test_reconnect_backoff_cap_60s against a closed port with aggressive reconnect backoff: asserts no SIGABRT, at least two reconnect log lines, and parsed “in X.XX seconds” delays never exceed 60 s (regex fixed so the “(unlimited)” log variant is matched).

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

shard_connection.cpp had two exponential-backoff multiplications with
no upper bound. With factor=2.0 and --max-reconnect-attempts=0 the
timer doubles indefinitely; after ~30 attempts the next scheduled
reconnect is ~34 years out. event_add() silently absorbs the value
and the benchmark goes effectively dark instead of surfacing the
underlying failure.

Adds MEMTIER_BACKOFF_CAP_SEC = 60s and clamps after every factor
multiplication (both reconnect_backoff_delay and retry_backoff_ms).

Refs: 2.4 review finding #13

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

Comment thread tests/test_reconnections.py Resolved
Comment thread shard_connection.cpp Resolved
Bugbot flagged two real issues in PR #433:

1. handle_reconnect_timer_event() had a second
   m_current_backoff_delay *= reconnect_backoff_factor without the
   MEMTIER_BACKOFF_CAP_SEC clamp. When a scheduled reconnect itself
   failed, the delay still grew unboundedly. Now capped at both
   sites.

2. test_reconnect_backoff_cap_60s regex used a [^i]+ negated class
   that could never match the "unlimited" log variant, so under
   --max-reconnect-attempts=0 the test ran the cap assertion
   against an empty match list (vacuous pass). Regex rewritten to
   match the actual log format.

Refs: bugbot HIGH x2 on PR #433

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

Copy link
Copy Markdown
Collaborator Author

Both bugbot HIGH findings addressed in eb4ea29: cap applied at second multiplication site in handle_reconnect_timer_event; test regex rewritten to match unlimited-mode log lines so the cap assertion is actually exercised.

Comment thread tests/test_reconnections.py Resolved
…keep memtier alive

Bugbot caught that the cap-regression test used --requests=forever, which
is not a valid memtier option. strtoull("forever") returns 0, triggering
immediate exit with "requests must be greater than zero" -- the test ran
memtier for ~0ms, observed zero reconnect attempts, found no delays in
the log, hit the warning branch, and passed vacuously.

Switched to --test-time=20 (bounded) which keeps memtier alive long
enough for the exponential backoff to fire the cap multiple times. Test
now actually exercises the production code path.

Also strengthens assertions: the "skip with warning" branch is replaced
by a hard assertGreaterEqual(len(delays), 2) that fails if memtier exits
immediately, plus a documented note that the libevent non-blocking connect
resets the backoff counter on each cycle so end-to-end cap-value
observation is not feasible in this test design.

Refs: bugbot HIGH on PR #433 (follow-up).

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

Copy link
Copy Markdown
Collaborator Author

Bugbot HIGH (vacuous test) addressed in e717a98: swapped --requests=forever for --test-time=20 so memtier actually stays alive through multiple reconnect attempts.

@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 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 e717a98. Configure here.

Comment thread tests/test_reconnections.py Resolved
RLTest assertions are soft - assertGreaterEqual records failure but
doesn't halt execution. Without an explicit guard, a vacuous-pass
pattern recurs: assertion records failure, max([]) raises ValueError,
the real failure reason is masked by the traceback.

Adds `if not delays: return` after the soft assertion so subsequent
max() / sequence checks only run when delays were observed.

Refs: bugbot LOW on PR #433 follow-up.

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

Copy link
Copy Markdown
Collaborator Author

Bugbot LOW (max on empty list) addressed in 43fcaaa: explicit guard after the soft assertion so the test fails cleanly with the assertion message instead of a confusing ValueError traceback.

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