Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/StackExchange.Redis/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
private Task SendRetiringUnsubscribe(RedisSubscriber subscriber, in RedisChannel channel, CommandFlags flags)
/// <param name="subscriber">The subscriber.</param>
/// <param name="channel">The channel.</param>
/// <param name="flags">The caller's flags.</param>
/// <param name="blocking">
/// 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 (<c>UnsubscribeAll</c> 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.
/// </param>
private Task SendRetiringUnsubscribe(RedisSubscriber subscriber, in RedisChannel channel, CommandFlags flags, bool blocking)
{
MarkRetired();
if (Volatile.Read(ref _currentServer) is { } server)
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -949,7 +959,7 @@ internal override Task<bool> UnsubscribeFromServerAsync(
object? asyncState,
bool internalCall)
{
return AsTrue(SendRetiringUnsubscribe(subscriber, channel, flags), asyncState);
return AsTrue(SendRetiringUnsubscribe(subscriber, channel, flags, blocking: false), asyncState);
}

private ServerEndPoint? _currentServer;
Expand Down
11 changes: 7 additions & 4 deletions tests/StackExchange.Redis.Tests/SubscriptionRestorationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<RedisTimeoutException>(() => pending.WithTimeout(5000));
Expand Down
Loading