diff --git a/src/StackExchange.Redis/Subscription.cs b/src/StackExchange.Redis/Subscription.cs index 4dbd6499a..b98c61d7b 100644 --- a/src/StackExchange.Redis/Subscription.cs +++ b/src/StackExchange.Redis/Subscription.cs @@ -896,7 +896,17 @@ internal override bool TryConsumePendingUnsubscribe(ServerEndPoint server) /// acknowledgement instead would return to the caller with the server still subscribed, and a subscribe that /// times out never acknowledges at all. /// - private Task SendRetiringUnsubscribe(RedisSubscriber subscriber, in RedisChannel channel, CommandFlags flags) + /// The subscriber. + /// The channel. + /// The caller's flags. + /// + /// Whether the caller is synchronous. One that is does not wait for an unsubscribe queued behind an in-flight + /// subscribe: that reply cannot arrive before the subscribe's, so the caller (UnsubscribeAll among them) + /// would block for the subscribe's whole wait, and then - depending on which of its own wait and the command's + /// timeout ran out first - either return or throw. Not waiting does not mean not observing: the unsubscribe is + /// still sent as an ordinary command, so a declined or timed-out one is retried. An async caller awaits it. + /// + private Task SendRetiringUnsubscribe(RedisSubscriber subscriber, in RedisChannel channel, CommandFlags flags, bool blocking) { MarkRetired(); if (Volatile.Read(ref _currentServer) is { } server) @@ -907,7 +917,7 @@ private Task SendRetiringUnsubscribe(RedisSubscriber subscriber, in RedisChannel if (SendingVia is { } via) { Volatile.Write(ref _pendingUnsubscribeVia, via); - return SendAsync(subscriber, channel, SubscriptionAction.Unsubscribe, flags, via); + return SendAsync(subscriber, channel, SubscriptionAction.Unsubscribe, blocking ? flags | CommandFlags.FireAndForget : flags, via); } return Task.CompletedTask; @@ -938,7 +948,7 @@ internal override bool UnsubscribeFromServer( CommandFlags flags, bool internalCall) { - WaitUnlessDeclined(subscriber, SendRetiringUnsubscribe(subscriber, channel, flags), flags); + WaitUnlessDeclined(subscriber, SendRetiringUnsubscribe(subscriber, channel, flags, blocking: true), flags); return true; } @@ -949,7 +959,7 @@ internal override Task UnsubscribeFromServerAsync( object? asyncState, bool internalCall) { - return AsTrue(SendRetiringUnsubscribe(subscriber, channel, flags), asyncState); + return AsTrue(SendRetiringUnsubscribe(subscriber, channel, flags, blocking: false), asyncState); } private ServerEndPoint? _currentServer; diff --git a/tests/StackExchange.Redis.Tests/SubscriptionRestorationTests.cs b/tests/StackExchange.Redis.Tests/SubscriptionRestorationTests.cs index 57cc917f2..e43c310c2 100644 --- a/tests/StackExchange.Redis.Tests/SubscriptionRestorationTests.cs +++ b/tests/StackExchange.Redis.Tests/SubscriptionRestorationTests.cs @@ -250,7 +250,9 @@ public async Task SyncUnsubscribeDuringInFlightSubscribeThatTimesOutStillUnsubsc { using var server = new PausedSubscribeServer(output); var config = server.GetClientConfig(); - config.SyncTimeout = 500; + // a sync wait far longer than the commands' own timeout: a caller that waited behind the subscribe would see + // its UNSUBSCRIBE time out, and throw, every time - not only when a slow runner tipped the race + config.SyncTimeout = 10_000; config.AsyncTimeout = 500; await using var connection = await ConnectionMultiplexer.ConnectAsync(config); var subscriber = connection.GetSubscriber(); @@ -264,9 +266,10 @@ public async Task SyncUnsubscribeDuringInFlightSubscribeThatTimesOutStillUnsubsc { await server.FirstSubscribe.Task.WithTimeout(5000); - // Sync unsubscribe while the subscribe is in flight queues a fire-and-forget UNSUBSCRIBE - // behind it on the pending server, so even if the subscribe then times out (and Message.ComputeResult - // drops its late ack), the server still ends up unsubscribed. + // Sync unsubscribe while the subscribe is in flight queues the UNSUBSCRIBE behind it on the pending + // server, and does not wait for it: its reply cannot arrive before the subscribe's, so waiting would + // block for the whole timeout and then return or throw by whichever timer ran out first. Even if the + // subscribe then times out (and its late ack is dropped), the server still ends up unsubscribed. subscriber.Unsubscribe(channel); Assert.False(connection.TryGetSubscription(channel, out _)); await Assert.ThrowsAsync(() => pending.WithTimeout(5000));