Repository navigation
Pub/Sub: retry a last unsubscribe the server declined, rather than leaving it subscribed - #3368
Open
aradalvand wants to merge 2 commits into
Open
aradalvand wants to merge 2 commits into
aradalvand wants to merge 2 commits into
Conversation
…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
force-pushed
the
fix/failed-unsubscribe-leaves-server-subscribed
branch
from
November 20, 2026 19:37
903d730 to
ebe2190
Compare
This branch has not been deployed
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.
Fixes #3367.
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
BUSYreply 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 andFireAndForgetunsubscribes alike.Changes
ConnectionMultiplexer.OnUnsubscribeNotAppliedre-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 anUNSUBSCRIBE/SUNSUBSCRIBE/PUNSUBSCRIBEof 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;FireAndForgetincluded.ObserveUnsubscribe(on the returned task) and the sync path'scatchhandle 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.Things worth a reviewer's eye
TryIssueWhileUnsubscribed. It holds the same lock asGetOrAddSubscription, 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.IsRetiredin the processor withoutRegistrationLock. 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 fromtrue.Subscription's own server(s), where the server-side subscription actually lives. An error reply doesn't clear_currentServer.OnBridgeHeartbeat), butMessage.ComputeResultskips processing the late reply of a command whose result box has already faulted. So:FireAndForgetunsubscribe's late error reply is handled (it has no box);ObserveUnsubscribe(NotApplied, by its status);Testing
FailedUnsubscribeUnitTests, run for both protocols, against an in-process server that repliesBUSYtoUNSUBSCRIBEa given number of times:DeclinedLastUnsubscribeIsRetried(async),DeclinedSyncLastUnsubscribeIsRetriedandDeclinedFireAndForgetLastUnsubscribeIsRetried: the server ends up unsubscribed (PUBLISHreaches 0 clients). Without the fix, all three fail on both protocols.RetriedUnsubscribeDoesNotUndoNewerSubscription: subscribing again right after the failure isn't undone by the retries.*UnitTestsclasses pass (1229, onnet10.0).FireAndForget:Checklist
🤖 Generated with Claude Code
https://claude.ai/code/session_01XUNs4qcAQaGCRpkwmr8LnY