Repository navigation
Pub/sub: retry a failed subscribe from the heartbeat while its connection stays up - #3387
Merged
Merged
Conversation
…tion stays up A subscribe that fails (declined with BUSY, timed out, or never written) keeps its handler registered, so the subscription is restored when the subscription connection is replaced. Until then nothing retried it, so with the connection up the caller stayed deaf indefinitely (#3371). A failed subscribe attempt now records that a retry is owed; the heartbeat consumes that and re-drives, through the existing internal-restore path, each subscription that is wanted but has no server, on a server whose subscription connection is up. A retry that fails records another failure, so retries continue at most once per heartbeat while the server declines, and stop once the handler is unsubscribed.
The library must not reference System.ValueTuple (SanityChecks.ValueTupleNotReferenced); use a closure for the continuation state instead.
…et subscribes From review: - Only a timeout, a connection failure, or a server refusal that reflects its own state (BUSY, LOADING, TRYAGAIN and so on: FaultContext.NotApplied) schedules a retry; NOPERM and the like would fail the same way forever. - A multi-node subscribe asked for fire-and-forget (including the restore after a reconnect) was sent as a fire-and-forget command, so a declined one went unseen; it is now sent normally and observed, as single-node subscribes already were. - A subscription skipped because its server's subscription connection is down asks for another look at the next heartbeat, in case that server never comes back. - An exception in the retry sweep is reported once until the sweep runs clean, not on every heartbeat. - AbandonedSubscribeIsNotRetried lets a retry already under way land before taking its count. - The docs say what SubscribedEndpoint really returns: the server that last confirmed the subscription.
This was referenced Dec 3, 2026
Merged
mgravell
added a commit
that referenced
this pull request
Dec 3, 2026
…tion stays up (port of main #3387) (#3409) A subscribe that fails (declined with BUSY, timed out, or never written) keeps its handler registered, so the subscription is restored when the subscription connection is replaced. Until then nothing retried it, so with the connection up the caller stayed deaf indefinitely (#3371). v4 had the same gap. - A subscribe is never sent as fire-and-forget on the wire. A caller who asked for that, including the restore after a reconnect, still does not wait, but a declined subscribe is now observed rather than completing when written. - A failure that can pass (a timeout, a connection failure, or a refusal reflecting the server's own state: FaultContext.NotApplied) records a retry owed (IsTransientSubscribeFault, as main). - The heartbeat consumes that and re-drives each wanted subscription that has no server, on a server whose subscription connection is up; one that is down asks for another look next time. An exception in the sweep is reported once until it runs clean. - docs/Basics.md: what happens when subscribing fails, and what SubscribedEndpoint returns. A fire-and-forget subscribe now records its server on the acknowledgement rather than on send, so the overlapping-restore test's fire-and-forget case goes back to main's expectation (the deferred restore counts as a pending change). Main's FailedSubscribeUnitTests (44 cases) apply as-is; with the retry stubbed out, 14 fail.
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.
Part of #3371: the self-healing part from my comment there. Retry policy for the subscribe call itself isn't in scope here.
Problem
A subscribe that fails keeps its handler registered, which is deliberate:
Until that happens, though, nothing tried again. A subscribe declined with
BUSY, timed out, or never written, on a connection that stays up, left the caller deaf indefinitely. The only things that re-drive a subscription are:MOVED/disconnect resubscribe;None of them fires in this case.
Change
A failure that can pass is recorded. A failed subscribe sets a flag on the multiplexer, from any path: sync, async, fire-and-forget, an internal restore, single-node or multi-node. "Can pass" means:
BUSY,LOADING,TRYAGAIN,MASTERDOWN,CLUSTERDOWN,MOVED, ...). That'sFaultContext.NotApplied, the same "the server didn't run it" test the retry policy uses.Anything else (
NOPERMfrom an ACL, say) would fail the same way every time, so it isn't retried. The handler stays registered as before.The heartbeat retries, only when needed. It consumes the flag and walks the subscriptions, re-driving each that is wanted, not retired and has no server. Any other heartbeat costs one
Interlocked.Exchange.Only where the subscription connection is up. A target whose subscription connection is down is left to restore itself when that connection reconnects, as today. A single-node subscription skipped this way asks for another look at the next heartbeat, in case that server never comes back and another is selectable.
Retries reuse the existing restore paths.
Multi-node fire-and-forget subscribes are now observed. Before, a multi-node subscribe asked for fire-and-forget (including the restore after a reconnect) went out as a fire-and-forget command. That completes when written, so a declined one went unseen. It's now sent as a normal command and watched without being awaited, as single-node subscribes already were.
Retries stop on their own. A failed retry (of a kind that can pass) records another failure, so retries continue at most once per heartbeat for as long as the server declines. They stop once the handler is unsubscribed (the subscription is then retired).
No error flood. An exception thrown by the retry sweep is reported through
InternalErroronce, until a sweep runs clean, not on every heartbeat.The caller still sees the original failure. The exception from
Subscribe/SubscribeAsyncis unchanged.Docs.
docs/Basics.mdnow says what a failed subscribe means:SubscribedEndpoint(channel)returns: the server that last confirmed the subscription.Tests
FailedSubscribeUnitTests(RESP2 and RESP3), against the in-process server set to refuse (or delay) subscribes:DeclinedSubscribeIsRetried,DeclinedSyncSubscribeIsRetried,DeclinedPatternSubscribeIsRetried,DeclinedFireAndForgetSubscribeIsRetried: the call fails (where the caller can see it), the connection never drops, and the subscription comes up and delivers.DeclinedFireAndForgetMultiNodeSubscribeIsRetried: a keyspace subscription across two nodes, one of which declines. Both end up subscribed.TimedOutSubscribeIsRetried: the call times out and the server then declines it anyway.PersistentlyDeclinedSubscribeIsRetriedAtTheHeartbeat: 3–4 retries in 4s, and the subscription comes up once the server accepts.PermanentlyRefusedSubscribeIsNotRetried: afterNOPERM, the caller gets the error, and nothing is sent again.AbandonedSubscribeIsNotRetried: after the caller unsubscribes the handler, nothing is retried.TransientSubscribeFaultsAreRecognised: the classification, for timeouts, connection failures, transient and permanent server errors, wrapped exceptions, and others.On
main, every scenario test fails exceptAbandonedSubscribeIsNotRetriedandPermanentlyRefusedSubscribeIsNotRetried. Before each test refuses anything, it warms up the subscription connection. Otherwise the refusal can be absorbed by the connection's own restore sweep at handshake, which hid the problem in my first attempt.I also checked the tests catch what they're meant to. Treating every server error as transient fails
PermanentlyRefusedSubscribeIsNotRetriedand the classification theory. Sending multi-node fire-and-forget subscribes the old way failsDeclinedFireAndForgetMultiNodeSubscribeIsRetried.Also:
*UnitTestsplusSanityCheckspass on net10.0;FailedSubscribeUnitTests,FailedUnsubscribeUnitTestsandSanityCheckspass on net481;