Skip to content

v4: absorb main 3.5.0, part 3 - pub/sub: overlapping restores cannot register on two servers - #3407

Merged
mgravell merged 1 commit into
v4from
marc/v4-absorb-3.5-b2
Dec 3, 2026
Merged

mgravell merged 1 commit into
v4from
marc/v4-absorb-3.5-b2

Conversation

@mgravell

@mgravell mgravell commented Dec 3, 2026

Copy link
Copy Markdown
Member

Third slice of absorbing main's 3.5.0 into v4 (after #3403 and #3406): main's fix for overlapping subscription restores registering on two servers. #3368 and #3387 follow separately.

Covered

main What happens here
#3353 Main's fix has two layers. Claim before send: v4 already had the equivalent, HasSendInFlight. Reconcile at the ack: v4 lacked this. It recorded every acknowledgement unconditionally, so a restore acknowledged by a second server after the in-flight window (which is bounded by time) replaced the recorded server while the first registration stayed live, delivering every message twice (#3336).
#3376 The test rename to TryAdoptEndpoint applies as-is.

Changes (Subscription.cs)

  • TryAdoptEndpoint: a single-node subscription refuses an ack from a different server while its recorded one is live. The settle path then unsubscribes the stray, fire-and-forget and pinned to that server. Multi-node subscriptions adopt every ack. OnSubscribed keeps the unconditional AddEndpoint.
  • Only internal restores defer to an attempt in flight, and they count as a pending change (1, not 0). A user subscribe sends its own, and returns once that is acknowledged or throws, as in main's review follow-up.
  • Clearing a stale recorded server is a compare-and-swap, so a live server recorded by a concurrent ack is never wiped. This applies in EnsureSubscribedToServer[Async], RemoveIncorrectRouting and RemoveDisconnectedEndpoints.

Tests

Main's SubscriptionRestoreRaceTests come across with the old-core sends replaced by v4's: a ping on the second server's subscription socket as the barrier, and Subscription.SendAsync for the stray. All 16 failed on v4 before the change; they pass now.

One expectation differs from main. In the fire-and-forget overlap case the deferred restore returns 0, not 1, because v4 records a fire-and-forget subscribe's server when it is sent, not when it is acknowledged (so that a flush finds the right socket). The second restore therefore reads it as already subscribed. The assertions that matter (no SUBSCRIBE reaches the second server, and the first server is the one recorded) are unchanged.

The pub/sub, failover, endpoint-pruning, retirement and SanityChecks suites pass on net10.0 (and, before the rebase onto #3406, on net481).

…vers (port of main #3353, #3376)

v4 already keeps overlapping restores apart while one is in flight (HasSendInFlight, standing in for
main's claim), but recorded every acknowledgement unconditionally. A restore acknowledged by a second
server after the in-flight window (it is bounded by time) replaced the recorded server while the first
registration stayed live, delivering every message twice (#3336).

- TryAdoptEndpoint: a single-node subscription refuses an ack from a different server while its
  recorded one is live; the settle path then unsubscribes the stray (fire-and-forget, pinned to that
  server). Multi-node subscriptions adopt every ack.
- Only an internal restore defers to the attempt in flight, and counts as a pending change (1, not 0);
  a user subscribe sends its own and returns once that is acknowledged, as main's review follow-up.
- Clearing a stale recorded server is a compare-and-swap (EnsureSubscribedToServer[Async],
  RemoveIncorrectRouting, RemoveDisconnectedEndpoints), so a live server recorded by a concurrent ack
  is not wiped.

Main's SubscriptionRestoreRaceTests come across with the old-core sends replaced by v4's; all 16
failed on v4 before this. The fire-and-forget overlap case expects 0 rather than 1 from the deferred
restore, because v4 records a fire-and-forget subscribe's server when it is sent. Main's
MultiNodeSubscriptionTrackingTests (#3376's rename) applies as-is.
@mgravell
mgravell merged commit 2c9136c into v4 Dec 3, 2026
4 of 5 checks passed
@mgravell
mgravell deleted the marc/v4-absorb-3.5-b2 branch December 3, 2026 17:20
mgravell added a commit that referenced this pull request Dec 3, 2026
… restore and the heartbeat (port of main #3368) (#3408)

Unsubscribing a channel's last handler retires its subscription on the client whether or not the
server-side unsubscribe succeeds. v4 had main's gap: a declined (BUSY, LOADING, MASTERDOWN) or
timed-out UNSUBSCRIBE, or one sent while the SUBSCRIBE was still unacknowledged, left the server
subscribed with nothing left to clean it up until the connection was replaced.

- Subscription: a retiring state readable without the registration lock, plus a retry-needed flag
  consumed before re-issuing, so a slow reply is not re-sent on every heartbeat. Restore paths skip
  retired subscriptions.
- The UNSUBSCRIBE is never sent as fire-and-forget, though a caller who asked for that still does
  not wait, so a declined one is always observed. An incomplete one records the server and is
  tracked by the multiplexer, which re-issues it from the heartbeat and from EnsureSubscriptions
  unless a newer subscription owns, or may yet claim, that server.
- An unsubscribe while the subscribe is in flight goes out behind it on the same connection, so
  the caller does not return with the server still subscribed. An ack for a retired subscription,
  or a stray for a live one, is unsubscribed as a tracked retirement.
- Self-correction: an older UNSUBSCRIBE that lands after a newer subscription for the channel was
  restored onto the same server re-subscribes that one.
- The synchronous Unsubscribe surfaces the server's exception rather than an AggregateException.

Also from main's SubscriptionRestorationTests, which #3353 created and #3407 missed:
- a timed-out subscribe's server is tracked as a possible stray, since its late ack reaches nobody
  in v4;
- an internal restore that deferred to an in-flight attempt is re-driven when that attempt fails;
- UnsubscribeAsync's task carries the subscriber's async state (an existing v4 bug).

Main's FailedUnsubscribeUnitTests (14 of 18 failed on v4) and SubscriptionRestorationTests come
across with old-core sends replaced by v4's; the bridge-identity test keeps its intent (restored
on the new connection, and delivers) without the PhysicalBridge assertions.
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