fix(controller): scan standby_endpoints by capacity, not count (bug-1474)

Root cause of the "spurious duplicate reconnect" (angle a): zarray_del()
NULLs the slot and decrements count without compacting, so a live entry
can sit at any index < caps. Every standby_endpoints loop bounded by
`count` missed it. Live evidence (--config-upgrade-wal-trim soak,
2026-09-26): ctrl-2 won, promoted vote eps [ctrl-1 (dead), ctrl-3]; the
dead one's removal left [NULL, ctrl-3] with count 1, so the primary

- stopped heartbeating ctrl-3 (primary_heartbeat_fn's send loop),
- re-dialed it ("peer[2] not connected -- initiating outbound connect"
  9s later, for a peer that never dropped) and registered a second,
  "late-joiner" channel that burned a MAX_STANDBYS repl slot,
- could not evict the stale channel (on_standby_connected's dedup scan),
- and skipped it in config pushes and the ctrlhello broadcast, where a
  hole below count also handed endpoint_ref() a NULL.

The re-dial landed in the freed slot 0, which is what made ctrl-3 visible
again -- the "spurious" reconnect was masking the lost heartbeats.

All index scans of standby_endpoints (ha.c heartbeat, sweep, dedup, two
removals; cluster.c removal and ctrlhello; config_repl.c push collection)
now run to `caps` and skip NULL slots; the push collects exactly the
non-NULL entries. `count` stays where it means "how many connected".

New test_reconnect_dedups_channel_behind_deleted_slot (RED before: the
stale channel behind the hole was never evicted). Controller suite 1241/0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Rongsong Shen 2026-09-27 06:01:11 +00:00
parent 255b0faad1
commit c3a17b19ab
Signed by untrusted user: rshen
GPG key ID: 80533536B0EB651C
4 changed files with 67 additions and 9 deletions

View file

@ -1558,8 +1558,16 @@ int push_config_transport_only(cluster_t *c, const uint8_t *bytes,
" — skipping transport push", n);
return 0;
}
for (int i = 0; i < n; i++) {
ctxs[i].ep = (endpoint_t *)zarray_get(c->ha.standby_endpoints, i);
/* bug-1474: zarray_del leaves holes without compacting, so live entries
* can sit at any index < caps -- collect exactly the non-NULL ones (n,
* the count, is how many there are). */
int filled = 0;
for (int k = 0; k < c->ha.standby_endpoints->caps && filled < n; k++) {
endpoint_t *sep = (endpoint_t *)zarray_get(c->ha.standby_endpoints, k);
if (sep == NULL)
continue;
int i = filled++;
ctxs[i].ep = sep;
ctxs[i].from = c->myhost;
ctxs[i].new_tag = new_tag;
ctxs[i].bytes = bytes;
@ -1576,6 +1584,9 @@ int push_config_transport_only(cluster_t *c, const uint8_t *bytes,
endpoint_ref(ctxs[i].ep);
}
pthread_mutex_unlock(&c->ha.lock);
n = filled;
if (n_out)
*n_out = n;
pthread_t *threads = malloc((size_t)n * sizeof(pthread_t));
if (!threads) {