Skip to content

Pub/Sub: retry a last unsubscribe the server declined, rather than leaving it subscribed - #3368

Open
aradalvand wants to merge 2 commits into
seredis:mainfrom
aradalvand:fix/failed-unsubscribe-leaves-server-subscribed
Open

aradalvand wants to merge 2 commits into
seredis:mainfrom
aradalvand:fix/failed-unsubscribe-leaves-server-subscribed

Conversation

@aradalvand

@aradalvand aradalvand commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #3367.

Builds on #3363. This branch includes its commit (bb0d88f), since it restructured the unsubscribe path this change hooks into. The change itself is the second commit, ebe2190. I'll rebase once #3363 is merged.

Unsubscribing a channel's last handler retires its subscription on the client whether or not the server-side unsubscribe succeeds. When the server declined to run it, for example with a BUSY reply while a script runs, it stays subscribed. It keeps pushing messages that nothing receives, and nothing is left on the client to unsubscribe, until the connection is replaced. This happens for awaited, synchronous and FireAndForget unsubscribes alike.

Changes

  • A new ConnectionMultiplexer.OnUnsubscribeNotApplied re-issues the retired subscription's unsubscribe, with backoff from 100ms up to 5s, at most 30 attempts, unless the channel has been subscribed to again by then, at which point client and server agree once more. It's called once per failure, including failures of the re-issued unsubscribes, from wherever that failure is observed:
    • TrackSubscriptionsProcessor.SetResult (now overridden) handles error replies. It checks whether an error reply to an UNSUBSCRIBE/SUNSUBSCRIBE/PUNSUBSCRIBE of a retired subscription is one where the server never ran the command (FaultContext.IsKnownNotApplied: BUSY, LOADING, MASTERDOWN, …). Reply processing sees every reply, so this works whether or not anyone awaits the command; FireAndForget included.
    • ObserveUnsubscribe (on the returned task) and the sync path's catch handle failures with no reply at all, where the command was never written (FaultContext.NotApplied). Server errors are left to the processor, so nothing is retried twice.
  • The caller still observes the original failure. Only the consequence is repaired.

Things worth a reviewer's eye

  • Ordering against a newer subscription. A re-issued unsubscribe must never undo a subscription made after the failed one; that's the same class of race as Pub/Sub: don't orphan a subscribe that races the last unsubscribe of the same channel #3363. So each re-issue goes through the new TryIssueWhileUnsubscribed. It holds the same lock as GetOrAddSubscription, and only issues the command if the channel still has no subscription. A subscription added afterwards therefore has its subscribe command issued after the unsubscribe. Like Pub/Sub: don't orphan a subscribe that races the last unsubscribe of the same channel #3363, this relies on commands to the same bridge being written in the order they're issued.
  • Reading IsRetired in the processor without RegistrationLock. A subscription is retired before its unsubscribe command is issued, so before that command's reply can be read. The flag also never goes back from true.
  • Which server it's re-issued to: the retired Subscription's own server(s), where the server-side subscription actually lives. An error reply doesn't clear _currentServer.
  • Timeouts. SE.Redis keeps a timed-out command queued so later replies still line up (OnBridgeHeartbeat), but Message.ComputeResult skips processing the late reply of a command whose result box has already faulted. So:
    • a FireAndForget unsubscribe's late error reply is handled (it has no box);
    • an unsubscribe that timed out before being written is handled by ObserveUnsubscribe (NotApplied, by its status);
    • one gap remains: an awaited unsubscribe that's written, times out, and then gets a "declined" error reply. That seems unlikely, since such errors are typically immediate. Closing it would mean letting processors observe late replies, which felt too broad for this PR.
  • A dropped connection isn't retried: the server forgets the subscription anyway. A retry that's pending when the connection is replaced is a harmless no-op on the new connection.
  • A background task for each scheduled retry. I didn't see an existing scheduler for this kind of deferred work; happy to move it if there's a better place.

Testing

  • New FailedUnsubscribeUnitTests, run for both protocols, against an in-process server that replies BUSY to UNSUBSCRIBE a given number of times:
    • DeclinedLastUnsubscribeIsRetried (async), DeclinedSyncLastUnsubscribeIsRetried and DeclinedFireAndForgetLastUnsubscribeIsRetried: the server ends up unsubscribed (PUBLISH reaches 0 clients). Without the fix, all three fail on both protocols.
    • RetriedUnsubscribeDoesNotUndoNewerSubscription: subscribing again right after the failure isn't undone by the retries.
  • All *UnitTests classes pass (1229, on net10.0).
  • Against a real Redis 8.10.2, with the repro from Pub/Sub: a failed unsubscribe of the last handler leaves the server subscribed while the client forgets it #3367, both awaited and FireAndForget:
    • 3.4.20: server-side subscribers stay at 1.
    • With this change: they drop to 0 within the backoff window.

Checklist

  • I fully and freely contribute this code in accordance with the project license (and am legally able to do so)
  • I take responsibility for this contribution's quality and correctness, including any portions produced with AI assistance (see CONTRIBUTING.md).

🤖 Generated with Claude Code

https://claude.ai/code/session_01XUNs4qcAQaGCRpkwmr8LnY

aradalvand and others added 2 commits November 19, 2026 21:14
…the same channel

Subscribing (looking up the channel's subscription, then adding the handler)
and unsubscribing the last handler (removing it, removing the now-empty
subscription from the multiplexer, then unsubscribing from the server) were
not atomic with respect to each other. Interleaved, the handler could be added
to a subscription that was being removed: nothing routes messages to it
anymore, and the server is told to stop sending them; yet the subscribe
completes successfully, so the handler silently never fires.

A per-subscription registration lock now makes adding a handler and retiring
the subscription mutually exclusive; a subscribe that finds the subscription
retired looks up a fresh one instead. The retiring side also issues the
unsubscribe command to the server (and only then removes the subscription from
the multiplexer) while holding the lock, so that the racing subscribe's command
is issued after it, rather than being able to overtake it and leave the server
unsubscribed while the client considers itself subscribed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XUNs4qcAQaGCRpkwmr8LnY
…aving it subscribed

Unsubscribing a channel's last handler retires its subscription on the client
whether or not the server-side unsubscribe succeeds. When the server declined
to run it (e.g. a BUSY reply while a script runs), the server stayed
subscribed, pushing messages that nothing receives, with nothing left on the
client to unsubscribe - until the connection was replaced.

Now such an unsubscribe is re-issued after a backoff, unless the channel has
been subscribed to again by then (at which point client and server agree once
more); each re-issue happens while holding the same lock that adding a
subscription takes, so it is ordered before any subscribe that follows and
can't undo a newer subscription.

The failure is noticed where it can always be seen, rather than through the
caller's task: TrackSubscriptionsProcessor sees every error reply - including
those of fire-and-forget and synchronous unsubscribes - and the returned task
is only observed for failures with no reply at all (the command never having
been written). The caller still observes the original failure.

Fixes seredis#3367.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XUNs4qcAQaGCRpkwmr8LnY
@aradalvand
aradalvand force-pushed the fix/failed-unsubscribe-leaves-server-subscribed branch from 903d730 to ebe2190 Compare November 20, 2026 19:37

This branch has not been deployed

No deployments
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.

Pub/Sub: a failed unsubscribe of the last handler leaves the server subscribed while the client forgets it

1 participant