Skip to content

Fix SignalR sharded subscriptions racing Redis Cluster startup - #2830

Merged
thomhurst merged 1 commit into
mainfrom
issue-2824-signalr-cluster-connect
Dec 5, 2026
Merged

thomhurst merged 1 commit into
mainfrom
issue-2824-signalr-cluster-connect

Conversation

@thomhurst

@thomhurst thomhurst commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

Fix

A SignalR host can start its background Redis PING while the first long-polling client is connecting. The Cluster router published the seed before its initial CLUSTER SLOTS query finished. A sharded subscription could then use the seed while the slot map was empty. Redirects and topology updates during backplane startup could start subscription recovery, which rejected the next subscription from OnConnectedAsync. The client had completed the handshake and saw HTTP 500 when invoking Identity.

Publish the seed after the initial slot query completes. When Redis supplies a map, concurrent subscribers go straight to the slot primary. A server that declines the topology query can still serve as the seed. Cluster test hosts now log server errors so a failed poll carries diagnostics in the test output.

Reproduction

The original smoke job recorded only the client stack. In a local run using the same TUnit version and Redis image, a temporary 40 ms delay before the initial slot query reproduced 89 HTTP 500 responses in 512 sharded MessagePack cases. The server logged RespireConnectionException: Sharded pub/sub recovery is in progress. Subscribe again after recovery completes. One additional case lost a client-result heartbeat under load.

The regression test holds the first slot reply while starting a sharded subscription. Before the fix, both RESP2 and RESP3 sent SSUBSCRIBE foo to the seed even though the second primary owns its slot.

Scope

#2807 changes test parallelism. #2816 adds subscriber queries through #2825. This fix addresses Cluster startup ordering independently of both changes.

Validation

  • Both RESP2 and RESP3 regression cases failed against the original router. All 191 focused Cluster tests and all 93 SignalR tests passed on both net10.0 and net11.0 with the fix. The four Release builds completed with zero warnings and errors.
  • With the same 40 ms delay and the fix, all 512 sharded MessagePack cases connected, and 511 completed the entire scenario. The other case timed out waiting for the first remote broadcast after both clients connected. Remote SignalR client times out waiting for the first sharded cluster broadcast under load #2829 tracks that delivery failure separately.
  • Curb 0.16.0 formatted and checked all three changed C# files. The temporary delay, repetition count and TUnit changes were local to the experiment.

Closes #2824

@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 23 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f0076c22-d0fa-4ef9-8a47-167457b23e2b
Commits

Reviewing files that changed from the base of the PR and between 8b660ba and 8fd3d45.

Files selected for processing: 3

  • src/Respire/Internal/ClusterRouter.cs
  • tests/Respire.SignalR.Tests/BackplaneIntegrationTests.cs
  • tests/Respire.Tests/Networking/ClusterShardedPubSubTests.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@thomhurst

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T14:33:53.842701Z 8fd3d45 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: fbae31169b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge.

Summary

The router now finishes its first CLUSTER SLOTS query before publishing the seed. Concurrent sharded subscriptions can then select the advertised primary.

  • Sharded subscriptions use the slot owner during Redis Cluster startup.

Diagram

sequenceDiagram
    participant Startup
    participant Router
    participant Redis
    participant Subscriber
    Startup->>Router: PING starts discovery
    Router->>Redis: CLUSTER SLOTS
    Subscriber->>Router: SubscribeShardedAsync
    Note over Subscriber,Router: Wait for initial discovery
    Redis-->>Router: Slot owners
    Router->>Router: Publish topology, then seed
    Router-->>Subscriber: Select slot primary
Loading

Reviews (2) · Last reviewed commit: "Publish the cluster seed after initial s..." · Reviewed by Greptile

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Review of #2830

I found no bugs or blocking design problems at head fbae311. I read the diff and the surrounding ClusterRouter seed and slot-load code. I did not build or run anything. The validation figures come from the PR description.

What changed

EnsureConnectedAsync now awaits TryLoadSlotsAsync(node) before it calls SetSeed(node). Previously it published the seed first. The fast path at ClusterRouter.cs:408 and the re-check at :421 read _seed. A concurrent caller, such as a sharded subscribe arriving during startup PING, could skip the _seedGate wait and use the seed while the routing snapshot was still empty. That is the race described in the PR.

Why I think it's correct

  • Seed gate: The whole sequence runs under _seedGate, so other callers wait on the gate until the first slot query completes. After that they either see the published seed and a populated snapshot, or they re-enter discovery.
  • Failed slot query: TryLoadSlotsAsync returns Loaded = false when the server declines CLUSTER SLOTS. The result is deliberately discarded, so a server that rejects the topology query can still act as the seed, as the PR says.
  • Thrown failures: If the slot query throws a retryable connection failure, the existing catch handles it and moves to the next seed. This is slightly better than before, because a seed whose topology query failed is no longer published first.
  • Worker start: SetSeed is also what starts the topology refresh worker. That now happens after the first slot load rather than before it. No refresh can race the initial load, so this looks harmless and slightly safer.

Tests

  • ShardedSubscribeDuringInitialDiscoveryUsesTheSlotOwner covers RESP2 and RESP3. It gates the first CLUSTER SLOTS reply, starts a sharded subscribe, then releases the gate. It checks that SSUBSCRIBE foo reaches the owning node and never the seed. That is a deterministic reproduction of the race, with no sleeps.
  • The SignalR host change logs errors only for cluster test hosts. That is enough to put the hub exception in the test output for a failed long-poll.

Reviews and open items

Nothing needs action.

@thomhurst

thomhurst commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner Author

#2837 cleared the earlier blocker from #2821. Claude and Codex both cleared fbae311, and all review threads are resolved. This PR is ready for review with squash auto-merge armed. GitHub will merge when the required CI Passed check succeeds. CI run

@thomhurst
thomhurst marked this pull request as draft December 5, 2026 14:20
auto-merge was automatically disabled December 5, 2026 14:20

Pull request was converted to draft

@thomhurst
thomhurst force-pushed the issue-2824-signalr-cluster-connect branch from fbae311 to 8fd3d45 Compare December 5, 2026 14:22
@thomhurst

Copy link
Copy Markdown
Owner Author

@codex review

Rebased onto main to pick up the CI gate fix from #2837. The previous CI run passed every build and test, then failed on the automatic Claude review startup. The rebase preserved the patch. All 191 focused Cluster tests and 93 SignalR tests passed on each of .NET 10 and .NET 11. All four Release builds finished with zero warnings and errors, and all 12 CI gate tests passed.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

No blocking issues. I read the diff and the surrounding router code, but I did not build or run the tests.

The fix (ClusterRouter.cs:449-453): SetSeed(node) now runs after TryLoadSlotsAsync instead of before it.

  • The fast paths in EnsureConnectedAsync (lines 408 and 421) return as soon as _seed is connected. Setting the seed first therefore let a concurrent caller skip _seedGate while the slot map was still empty. A sharded subscription could then route to the seed rather than the slot owner.
  • With the new order, concurrent callers wait on _seedGate until the first topology reply arrives.
  • If the server declines CLUSTER SLOTS, TryLoadSlotsAsync returns (false, …) without throwing. The seed is still published, so a server that can't answer the topology query still works as the seed.
  • If the slot query throws a retryable connection error, the seed is no longer published. The loop moves to the next candidate seed, and the error is accounted for the same way as before. That is arguably better than keeping a seed whose first topology query failed. Cancellation and timeouts propagate as before.
  • TryLoadSlotsAsync takes the seed as an argument and doesn't read _seed. Its internal SetSeed(master) calls apply the topology, so they don't depend on the ordering.

Regression test (ShardedSubscribeDuringInitialDiscoveryUsesTheSlotOwner):

  • It gates CLUSTER SLOTS on the first node and starts the subscription while the gate is held. It then asserts that SSUBSCRIBE foo reached the second node and not the first.
  • It runs under both RESP2 and RESP3.
  • It is deterministic, because it uses a TaskCompletionSource gate rather than sleeps, and it waits with Deadline. The PR description says it failed against the original router.

SignalR test host: error-level console logging is enabled only for cluster hosts. A long-polling HTTP 500 will now come with the server-side cause in the test output.

Optional, not blocking: the comment at line 449 explains why SetSeed comes after the slot load, but it is easy to reorder by accident later. A comment on the _seed field saying "published only after the initial slot query completes" would help. The new test already guards the behaviour.

The PR description notes one remaining message-delivery timeout, tracked in #2829. It is outside this change.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 8fd3d453c3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@thomhurst

Copy link
Copy Markdown
Owner Author

The comment beside TryLoadSlotsAsync and SetSeed explains why their order matters. The RESP2 and RESP3 regression cases check that the subscription reaches the slot owner during initial discovery. I’m keeping that explanation at the call site and declining the optional duplicate field comment. Claude review

@thomhurst
thomhurst marked this pull request as ready for review December 5, 2026 14:32
@thomhurst
thomhurst enabled auto-merge (squash) December 5, 2026 14:33
@thomhurst
thomhurst merged commit 7eb512e into main Dec 5, 2026
62 checks passed

This branch was previously deployed

1 inactive deployment
Pull Requests — 8fd3d453 Deployed Dec 5, 2026 by thomhurst via modularpipeline #6979
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.

SignalR cluster smoke test intermittently returns HTTP 500 on long-polling connect

1 participant