Skip to content

v4: absorb main 3.5.0, part 2 - pub/sub: channel hash slot, publish routing, subscribe/unsubscribe race - #3406

Merged
mgravell merged 4 commits into
v4from
marc/v4-absorb-3.5-b
Dec 3, 2026
Merged

mgravell merged 4 commits into
v4from
marc/v4-absorb-3.5-b

Conversation

@mgravell

@mgravell mgravell commented Dec 3, 2026

Copy link
Copy Markdown
Member

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

main What happens here
#3366 IConnectionMultiplexer.HashSlot(RedisChannel): cherry-picked; the only conflict was in PublicAPI.Unshipped.txt. Returning NoSlot for 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.
#3380 (#3357) v4 had the bug: a publish went to the server the channel is subscribed on, even after a promotion made it a replica, so a DemandMaster publish failed with the new primary known. ResolveForChannel now carries the publish's flags, and the subscribed server is used only if it meets the primary/replica demand (GetPublishServer, as main).
#3363 v4 had the race: a subscribe racing the last unsubscribe of the same channel could land on the retiring subscription and never fire. Main's registration lock applied to v4's RedisSubscriber as-is. In Subscription, the multi-node UnsubscribeFromServerAsync now issues every UNSUBSCRIBE before 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 plus QueuedResultTests, TransactionTests and SanityChecks pass on net10.0: 606 tests, 28 skipped.

aradalvand and others added 4 commits December 3, 2026 15:52
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)
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.

2 participants