Repository navigation
v4: absorb main 3.5.0, part 2 - pub/sub: channel hash slot, publish routing, subscribe/unsubscribe race - #3406
Merged
Merged
Conversation
Fixes #3365. There was no public way to ask which hash slot a `RedisChannel` is routed by. The usual workaround is `HashSlot(new RedisKey(channel))`, which hashes the channel's name as a key. It gets two cases wrong: a configured `ChannelPrefix`, and single-key keyspace channels, which are routed by their key. This exposes the existing `ServerSelectionStrategy.HashSlot(in RedisChannel)`, which is what routing actually uses and handles both. - **New `IConnectionMultiplexer.HashSlot(RedisChannel)`**, implemented by `ConnectionMultiplexer` (delegating to `ServerSelectionStrategy.HashSlot(in RedisChannel)`). Also forwarded by `MultiGroupMultiplexer` and the test fixture's `NonDisposingConnection`. - **`ServerSelectionStrategy.HashSlot(in RedisChannel)` now returns `NoSlot` (`-1`) for multi-node channels** (`IsMultiNode`, e.g. `KeySpacePattern`/`KeySpacePrefix`), which are subscribed on every node rather than routed by slot. Internally, slot-based routing is unaffected: the maintenance migration check only considers sharded channels, and multi-node subscriptions never select a server by slot. - **`[OverloadResolutionPriority(1)]` on the existing `HashSlot(RedisKey)`**, on both the interface and `ConnectionMultiplexer`. `string` and `byte[]` convert implicitly to both `RedisKey` and `RedisChannel`; the channel conversions are `[Obsolete]`, but that doesn't remove them from overload resolution. Without the attribute, `HashSlot("key")` becomes ambiguous (CS0121). I checked this: removing it breaks existing calls in `ClusterTests` and `HotKeysTests`. This follows the precedent of `RedisChannel.KeySpacePrefix`, including the framework shim for older TFMs. - **`PublicAPI.Unshipped.txt`** entries. - **A new interface member.** This adds a member to `IConnectionMultiplexer`, which is a break for anyone implementing it outside the library. Happy to change the shape (e.g. an extension method) if you'd rather avoid that. - **The priority attribute on an existing member.** Adding it doesn't change any signature, so it isn't a binary break. Compilers that don't understand the attribute would still see the ambiguity for `string`/`byte[]` arguments. - **Plain literal and pattern channels.** These return the slot of the (prefixed) name: that's the slot this client routes them by, which decides the node it subscribes on, rather than a server-side slot constraint. Multi-node channels return `-1`. As with `HashSlot(RedisKey)`, it returns `-1` when not connected to a cluster. - **New `ChannelHashSlotUnitTests`**, against the in-process server in cluster mode: - a sharded channel has the same slot as the same-named key; - with a `ChannelPrefix`, its slot is the prefixed name's; - a `KeySpaceSingleKey` channel has its key's slot; - a plain literal channel on a cluster has its name's slot; - multi-node channels (`KeySpacePattern`, `KeySpacePrefix`) return `-1`; - standalone returns `-1`; - `string` and `byte[]` arguments still bind to the key overload, both through `ConnectionMultiplexer` and through the interface. - **Builds** for all of `StackExchange.Redis`'s TFMs with no warnings. - **Existing tests:** all `*UnitTests` classes pass on `net10.0`, and CI is green on Ubuntu and Windows. (cherry picked from commit f80bdd4)
…a replica (port of main #3380 / #3357) v4's publish used the server the channel is subscribed on whenever there was one (ResolveForChannel). After a promotion that server can be a replica, and a DemandMaster publish then failed with the new primary known. ResolveForChannel now carries the publish's flags, and the subscribed server is used only when it satisfies the primary/replica demand (GetPublishServer, as main). Main's PublishRoutingUnitTests come over unchanged: 6 of 8 fail without this change. (cherry picked from commit 97d17b7, by intent)
…the same channel (port of main #3363) v4 had main's race: adding a handler and retiring the channel's last one were not atomic, so a handler could land on a subscription being removed and never fire. SubscriptionRegistrationRaceUnitTests failed on v4 before this; it passes now. The patch applied to v4's RedisSubscriber as-is. In Subscription, the multi-node UnsubscribeFromServerAsync now issues every UNSUBSCRIBE through v4's SendAsync before awaiting any of them, so all are written while the registration lock is held, as main's version does. (cherry picked from commit d278bf6, by intent)
This was referenced Dec 3, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Second slice of absorbing main's 3.5.0 into v4 (after #3403): the pub/sub fixes that port cleanly. The rest of the pub/sub batch (#3353, #3368, #3387, and #3376's rename) follows separately: their tests are written against the old core, so each needs a v4-shaped test.
Covered
IConnectionMultiplexer.HashSlot(RedisChannel): cherry-picked; the only conflict was inPublicAPI.Unshipped.txt. ReturningNoSlotfor multi-node channels is safe in v4 too. The one routing caller where it matters (RespConnectionManager.CanServe) now answers "unknown" instead of naming one node, for a channel subscribed on every node.DemandMasterpublish failed with the new primary known.ResolveForChannelnow carries the publish's flags, and the subscribed server is used only if it meets the primary/replica demand (GetPublishServer, as main).RedisSubscriberas-is. InSubscription, the multi-nodeUnsubscribeFromServerAsyncnow issues everyUNSUBSCRIBEbefore awaiting any of them, so all are written while the lock is held.Tests
Main's tests come over unchanged:
ChannelHashSlotUnitTests: 7 pass.PublishRoutingUnitTests: 6 of 8 fail on v4 without the change; all pass with it.SubscriptionRegistrationRaceUnitTests: failed on v4 (orphaned subscribe); now 300 of 300 attempts receive.The pub/sub suites (
PubSubTests,PubSubMultiserverTests,ClusterShardedTests,PubSubKeyNotificationTests,Resp3DowngradeTests, and the above) pass on net10.0 and net481. After merging current v4, those plusQueuedResultTests,TransactionTestsandSanityCheckspass on net10.0: 606 tests, 28 skipped.