Skip to content

Pub/sub: a publish that demands a primary does not follow a subscription onto a replica (#3357) - #3380

Merged
mgravell merged 2 commits into
mainfrom
marc/publish-demandmaster
Nov 27, 2026
Merged

mgravell merged 2 commits into
mainfrom
marc/publish-demandmaster

Conversation

@mgravell

Copy link
Copy Markdown
Member

Fixes #3357.

A publish to a subscribed channel goes to the server the channel is subscribed on (GetSubscribedServer), whatever the flags. A standalone subscription stays where it was made, even after a role change; it keeps working on a replica, so nothing moves it. After a promotion, a PublishAsync(..., CommandFlags.DemandMaster) to that channel is sent to the old primary, now a replica, and is refused locally with Command cannot be issued to a replica: PUBLISH. That continues indefinitely, even though the client knows the new primary and every other command routes to it.

Change

A new internal GetPublishServer(channel, flags) uses the subscribed server only if it satisfies the publish's primary/replica demand:

  • with DemandMaster, it must not be a replica;
  • with DemandReplica, it must be a replica.

Otherwise it returns null, and normal server selection applies. All four publish paths (ISubscriber and IDatabase, sync and async) use it. A publish without a demand is unchanged: it still uses the subscribed server.

No public API change. The subscription itself is not moved; that's a separate question.

Tests

PublishRoutingUnitTests, against the in-process server with a primary and a replica, run for both protocols:

  • DemandMasterPublishFollowsTheNewPrimary: subscribe, swap roles with no disconnect, then a DemandMaster publish reaches the new primary. Covers ISubscriber and IDatabase, sync and async.
  • DemandReplicaPublishDoesNotUseAPrimarySubscription.
  • UndemandingPublishStillUsesTheSubscribedServer: pins the unchanged default.

Without the fix, the first two fail with the error from the issue; the third passes either way, as intended. The library builds clean in Release across all TFMs with analyzers, and all 1232 *UnitTests pass on net10.0.

…ion onto a replica (#3357)

A publish to a subscribed channel is sent to the server the channel is
subscribed on. A subscription stays where it was made, even after a
role change, so after a promotion that server can be a replica; a
publish with DemandMaster was then refused locally ("Command cannot be
issued to a replica: PUBLISH"), indefinitely, even with the new primary
known and every other command routing to it.

The subscribed server is now only used when it satisfies the publish's
primary/replica demand (DemandMaster: not a replica; DemandReplica: a
replica); otherwise normal server selection applies. Without a demand,
behavior is unchanged.
…e role change

ConfigureAsync is a no-op while another reconfigure is in flight, so a single
call could leave the client never re-reading roles (seen on CI, both OSes).

Claude-Session: https://claude.ai/code/session_01DUpQ8dy8FLBwADei2VzLmZ
@mgravell
mgravell merged commit 97d17b7 into main Nov 27, 2026
5 checks passed
@mgravell
mgravell deleted the marc/publish-demandmaster branch November 27, 2026 09:33
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.

PublishAsync with DemandMaster keeps failing after a primary/replica role change

1 participant