Skip to content

Close three dispose/connect/topology windows behind intermittent CI failures - #3386

Merged
mgravell merged 3 commits into
mainfrom
marc/publish-bridge-before-connect
Dec 2, 2026
Merged

mgravell merged 3 commits into
mainfrom
marc/publish-bridge-before-connect

Conversation

@mgravell

@mgravell mgravell commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Fixes the intermittent Windows CI failures on main in MaintenanceOptInClientTests. A different test in that class has failed on every push since this morning: EnabledFailsAgainstAServerThatRefuses, AutoIsOffWhenTheServerDowngradesToResp2 and EnabledFailsWhenResp2WasOurOwnChoice (RESP3, mostly net481).

Cause

GetOrCreateBridge created the bridge, which starts connecting straight away, and only then published it to the interactive/subscription field. OnFullyEstablished identifies a connection by comparing its bridge with those fields. Against a fast server (the in-process test server has no socket latency), or with the creating thread descheduled on a busy runner, the handshake can complete inside that gap. The interactive connection then isn't recognised as interactive and skips its per-connection setup, which includes ReconcileMaintenanceNotifications. That accounts for both failure shapes:

  • a refused Enabled opt-in is never acted on, so the connection stays usable ("the connection should not have stayed usable");
  • a RESP2 downgrade never clears the accepted flag, so MaintenanceNotificationsActive reads true.

The race dates from #3264. Nothing about it changed recently; something has presumably shifted CI timing enough to expose it.

Change

Publish first, and connect only the bridge that won the publish. A bridge that loses the race is disposed without ever having connected, rather than opening a connection only to close it again. Before it connects, a published bridge is in the same "connecting" state it would be in just after starting to connect, so callers that find it early see nothing new.

Verification

I widened the gap locally with a temporary 200ms delay after TryConnect:

MaintenanceOptInClientTests (42)
main + delay 7 fail, every run: exactly the set seen in CI
this PR + the same delay 0 fail

Without the delay, all *UnitTests plus the maintenance classes pass on net10.0 (1392) and net481 (1375), and the library builds with 0 warnings across all TFMs. I haven't added a deterministic regression test, because that would need a timing hook in production code. The existing class covers it once the window can't open.

Also: QuitAllServers sized its array outside the lock

The second intermittent Windows failure on main (UnroutableRedirectUnitTests.UnroutableRedirectDoesNotInventAnEndpoint, IndexOutOfRangeException from QuitAllServers during DisposeAsync) is the same kind of window. The task array was sized from servers.Count before taking lock (servers) and filled inside it, so a server added in between (here, a redirect still being resolved during dispose) overran it. It's now sized inside the lock, which is the lock that additions take. The code is old; recent changes have only made the window easier to hit. UnroutableRedirectUnitTests passes 3/3 locally, and all 1313 *UnitTests pass on net10.0.

Also: the test server's CLUSTER SLOTS could reply with a nil element

A third intermittent failure, this time in the in-process test server rather than the library (EndpointPruningUnitTests.NodeCarryingSubscriptionsIsNotPruned on Ubuntu, from this PR's own CI run). The server answers CLUSTER SLOTS without taking its lock. It counted every node's slot ranges, rented an array of that size, and then filled it from a second read. A Migrate from the test that merged two ranges back into one, landing while the client's own reconfigure was asking, left a trailing nil element. The writer then threw Array element cannot be nil, and the client's connection faulted. The server now snapshots each node's ranges once and sizes the reply from that. All 1313 *UnitTests pass twice in a row on net10.0.

…gnises it

GetOrCreateBridge created the bridge - which starts connecting - and only then
published it to the interactive/subscription field. Against a fast server, or
with the creating thread descheduled, the handshake could complete in that gap,
and OnFullyEstablished identifies the connection by comparing its bridge with
those fields: an interactive connection not yet in `interactive` skipped its
per-connection setup, including the maintenance-notification reconcile. That
left a refused `Enabled` connection usable, and a RESP2 downgrade reported as
having notifications - the intermittent MaintenanceOptInClientTests failures on
the Windows CI job.

Publish first, and connect only the bridge that won the publish; a lost race
now never opens a connection only to close it again.

Verified by widening the gap with a 200ms delay after TryConnect: before, 7 of
the 42 MaintenanceOptInClientTests fail every run (exactly the set seen in CI);
after, none do.
@mgravell

mgravell commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Another intermittent Windows failure on main, which I'll fold into this PR. It isn't the bridge race, but it's the same kind of window. 5cb8fc9f, net10.0:

UnroutableRedirectUnitTests.UnroutableRedirectDoesNotInventAnEndpoint
System.IndexOutOfRangeException : Index was outside the bounds of the array.
  at ConnectionMultiplexer.QuitAllServers() in ConnectionMultiplexer.cs:3105
  at ConnectionMultiplexer.CloseAsync(Boolean allowCommandsToComplete)
  at ConnectionMultiplexer.DisposeAsync()

QuitAllServers sizes its array before taking the lock that guards servers:

var quits = new Task[2 * servers.Count];   // read outside the lock
lock (servers)
{
    // ... quits[index++] = server.Close(...), twice per server
}

A server added between that read and the lock (here, presumably while the redirect is still being resolved during dispose) overruns the array, and dispose throws. The fix is to size the array inside the lock. The code is old, so nothing recent introduced it; recent changes have only made the window easier to hit.

The task array was sized from servers.Count before taking lock(servers), and
filled inside it. A server added in between - e.g. a redirect still being
resolved while the multiplexer disposes - overran the array, and dispose threw
IndexOutOfRangeException (seen intermittently in
UnroutableRedirectUnitTests.UnroutableRedirectDoesNotInventAnEndpoint on the
Windows CI job). Read the count inside the lock instead.
@mgravell mgravell changed the title Publish a bridge before it connects, so the handshake completion recognises it Close two dispose/connect windows behind intermittent Windows CI failures Dec 2, 2026
CLUSTER SLOTS is lock-free in the in-process server: it counted every node's
ranges, rented an array of that size, then filled it from a second read. When
the topology changed in between - a test's Migrate merging two ranges back into
one while the client's own reconfigure was asking - the array kept a trailing
nil element, the writer threw "Array element cannot be nil", and the client's
connection faulted (EndpointPruningUnitTests.NodeCarryingSubscriptionsIsNotPruned
on the Ubuntu CI job). Snapshot each node's ranges once and size from that.
@mgravell mgravell changed the title Close two dispose/connect windows behind intermittent Windows CI failures Close three dispose/connect/topology windows behind intermittent CI failures Dec 2, 2026
@mgravell
mgravell merged commit a4cea63 into main Dec 2, 2026
5 checks passed
@mgravell
mgravell deleted the marc/publish-bridge-before-connect branch December 2, 2026 15:28
mgravell added a commit that referenced this pull request Dec 3, 2026
* Docs: why WithRetry has no retrying batch, and what to use instead (#3388)

A batch's one guarantee is an uninterrupted run, which a retry would break:
a batch cannot be replayed as a unit (some of it may have applied), and
re-issuing just the failed element would reorder it. Document that, and the
alternatives: pipelined commands on the retrying database for independent
work (each retried by its own category), a retrying transaction for
all-or-nothing, and a plain batch where the uninterrupted run is what matters.

Closes #3370.

(cherry picked from commit 5e2e8e4)

* Stabilize two racy tests: keyspace notification counting, and the drain-cap MOVING test (#3393)

PubSubKeyNotificationTests counted an event in the total before counting its key. Multi-node
notifications arrive on several reader threads, so the last event could complete the run while
another, already in the total, had not yet counted its key: the per-key check came up one short
(expected 51, actual 50). Count the key first.

MovingWaitsForSlowCommandsThatFitInsideTheDrainCap needs exactly one 3s command in flight, but
SetLatency slowed every command, and the in-process server answers one at a time: a background
INFO ahead of the PING pushed the PING's reply to ~6s, past the 5s drain cap, so the recycle
failed it. Add a SetLatency overload that holds a single command, and slow only the PING.

(cherry picked from commit f5bf0c3)

* Resp3DowngradeTests: keep publishing until the restored subscription receives (#3396)

The test published once, straight after the interactive connection came back as RESP2. The subscription is
restored separately (with a dedicated connection, on a connection of its own), and a message published before
that SUBSCRIBE lands is not delivered, so on a slow runner the test waited 10s for a message that was never coming.
It now publishes every 250ms until one arrives, still within 10s, so a subscription that is never restored still
fails it.

(cherry picked from commit e56835e)

* Port main's small 3.5.0 fixes and test hardening to v4

- #3374 (+#3376): v4 already had the inverted-check fix; its regression test comes over
  (as #3374 shipped it - #3376's TryAdoptEndpoint rename belongs with #3353)
- #3361: v4 already disposes socketless tunnel streams; its regression test comes over
- #3386: the toy server's CLUSTER SLOTS snapshot fix; the other two windows are in
  code v4 replaced (QuitAllServers, PhysicalBridge creation; v4 executors connect lazily)
- #3395: already the v4 behaviour (GetConnectionId does not dial)
- #3394: not applicable; the test it fixes went with the old core
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