Skip to content

Pub/sub: retry a failed subscribe from the heartbeat while its connection stays up - #3387

Merged
mgravell merged 3 commits into
mainfrom
marc/subscription-self-heal
Dec 2, 2026
Merged

mgravell merged 3 commits into
mainfrom
marc/subscription-self-heal

Conversation

@mgravell

@mgravell mgravell commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

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:

  • The server may have applied it. On a timeout, only the reply may have been lost.
  • The registration is what restores the subscription when the subscription connection is replaced.

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:

  • the subscription connection's handshake;
  • a non-initial reconfigure;
  • a MOVED/disconnect resubscribe;
  • a restore that deferred to an attempt which then failed.

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:

    • a timeout, a connection failure, or a command never written;
    • or a server refusal that reflects the server's own state at the time (BUSY, LOADING, TRYAGAIN, MASTERDOWN, CLUSTERDOWN, MOVED, ...). That's FaultContext.NotApplied, the same "the server didn't run it" test the retry policy uses.

    Anything else (NOPERM from 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.

    • Single-node retries go through the internal restore path, so they defer to any subscribe already in flight, and ack-time reconciliation still guards against duplicates.
    • Multi-node retries subscribe each node that is connected but not subscribed. A duplicate there (one merely slow to be acknowledged) is a no-op on the server.
  • 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 InternalError once, until a sweep runs clean, not on every heartbeat.

  • The caller still sees the original failure. The exception from Subscribe/SubscribeAsync is unchanged.

  • Docs. docs/Basics.md now says what a failed subscribe means:

    • which failures are retried in the background, and which aren't;
    • don't retry the call, because that registers the handler twice;
    • what 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: after NOPERM, 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 except AbandonedSubscribeIsNotRetried and PermanentlyRefusedSubscribeIsNotRetried. 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 PermanentlyRefusedSubscribeIsNotRetried and the classification theory. Sending multi-node fire-and-forget subscribes the old way fails DeclinedFireAndForgetMultiNodeSubscribeIsRetried.

Also:

  • all 1395 *UnitTests plus SanityChecks pass on net10.0;
  • FailedSubscribeUnitTests, FailedUnsubscribeUnitTests and SanityChecks pass on net481;
  • the pub/sub integration classes pass against the docker topology (149 passed, 58 skipped for keyspace-event config, long-running and Azure);
  • the library builds with 0 warnings across all target frameworks.

…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.
@mgravell
mgravell merged commit 514e348 into main Dec 2, 2026
7 checks passed
@mgravell
mgravell deleted the marc/subscription-self-heal branch December 2, 2026 16:44
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.
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