Skip to content

Fix very rare deadlock #1771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Impl;
using RabbitMQ.Client.Logging;

namespace RabbitMQ.Client.ConsumerDispatching
{
Expand Down Expand Up @@ -71,6 +72,16 @@ await _channel.OnCallbackExceptionAsync(CallbackExceptionEventArgs.Build(e, work
throw;
}
}
finally
{
while (_reader.TryRead(out WorkStruct work))
{
using (work)
{
ESLog.Warn($"discarding consumer work: {work.WorkType}");
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal abstract class ConsumerDispatcherChannelBase : ConsumerDispatcherBase,
private readonly System.Threading.Channels.ChannelWriter<WorkStruct> _writer;
private readonly Task _worker;
private readonly ushort _concurrency;
private bool _quiesce = false;
private long _isQuiescing;
private bool _disposed;

internal ConsumerDispatcherChannelBase(Impl.Channel channel, ushort concurrency)
Expand Down Expand Up @@ -79,15 +79,15 @@ internal ConsumerDispatcherChannelBase(Impl.Channel channel, ushort concurrency)
}
}

public bool IsShutdown => _quiesce;
public bool IsShutdown => IsQuiescing;

public ushort Concurrency => _concurrency;

public async ValueTask HandleBasicConsumeOkAsync(IAsyncBasicConsumer consumer, string consumerTag, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

if (false == _disposed && false == _quiesce)
if (false == _disposed && false == IsQuiescing)
{
try
{
Expand All @@ -110,7 +110,7 @@ public async ValueTask HandleBasicDeliverAsync(string consumerTag, ulong deliver
{
cancellationToken.ThrowIfCancellationRequested();

if (false == _disposed && false == _quiesce)
if (false == _disposed && false == IsQuiescing)
{
IAsyncBasicConsumer consumer = GetConsumerOrDefault(consumerTag);
var work = WorkStruct.CreateDeliver(consumer, consumerTag, deliveryTag, redelivered, exchange, routingKey, basicProperties, body);
Expand All @@ -123,7 +123,7 @@ public async ValueTask HandleBasicCancelOkAsync(string consumerTag, Cancellation
{
cancellationToken.ThrowIfCancellationRequested();

if (false == _disposed && false == _quiesce)
if (false == _disposed && false == IsQuiescing)
{
IAsyncBasicConsumer consumer = GetAndRemoveConsumer(consumerTag);
WorkStruct work = WorkStruct.CreateCancelOk(consumer, consumerTag);
Expand All @@ -136,7 +136,7 @@ public async ValueTask HandleBasicCancelAsync(string consumerTag, CancellationTo
{
cancellationToken.ThrowIfCancellationRequested();

if (false == _disposed && false == _quiesce)
if (false == _disposed && false == IsQuiescing)
{
IAsyncBasicConsumer consumer = GetAndRemoveConsumer(consumerTag);
WorkStruct work = WorkStruct.CreateCancel(consumer, consumerTag);
Expand All @@ -147,7 +147,7 @@ await _writer.WriteAsync(work, cancellationToken)

public void Quiesce()
{
_quiesce = true;
Interlocked.Exchange(ref _isQuiescing, 1);
}

public async Task WaitForShutdownAsync()
Expand All @@ -157,7 +157,7 @@ public async Task WaitForShutdownAsync()
return;
}

if (_quiesce)
if (IsQuiescing)
{
try
{
Expand Down Expand Up @@ -193,6 +193,19 @@ await _worker
}
}

protected bool IsQuiescing
{
get
{
if (Interlocked.Read(ref _isQuiescing) == 1)
{
return true;
}

return false;
}
}

protected sealed override void ShutdownConsumer(IAsyncBasicConsumer consumer, ShutdownEventArgs reason)
{
_writer.TryWrite(WorkStruct.CreateShutdown(consumer, reason));
Expand Down
32 changes: 18 additions & 14 deletions projects/RabbitMQ.Client/Impl/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,11 +744,13 @@ protected async Task<bool> HandleChannelCloseOkAsync(IncomingCommand cmd, Cancel
await FinishCloseAsync(cancellationToken)
.ConfigureAwait(false);

if (_continuationQueue.TryPeek<ChannelCloseAsyncRpcContinuation>(out ChannelCloseAsyncRpcContinuation? k))
if (_continuationQueue.TryPeek(out ChannelCloseAsyncRpcContinuation? k))
{
_continuationQueue.Next();
await k.HandleCommandAsync(cmd)
.ConfigureAwait(false);
using (IRpcContinuation c = _continuationQueue.Next())
{
await k.HandleCommandAsync(cmd)
.ConfigureAwait(false);
}
}

return true;
Expand Down Expand Up @@ -818,10 +820,12 @@ await ModelSendAsync(in replyMethod, cancellationToken)

protected async Task<bool> HandleConnectionSecureAsync(IncomingCommand cmd, CancellationToken cancellationToken)
{
var k = (ConnectionSecureOrTuneAsyncRpcContinuation)_continuationQueue.Next();
await k.HandleCommandAsync(new IncomingCommand())
.ConfigureAwait(false); // release the continuation.
return true;
using (var k = (ConnectionSecureOrTuneAsyncRpcContinuation)_continuationQueue.Next())
{
await k.HandleCommandAsync(new IncomingCommand())
.ConfigureAwait(false); // release the continuation.
return true;
}
}

protected async Task<bool> HandleConnectionStartAsync(IncomingCommand cmd, CancellationToken cancellationToken)
Expand All @@ -848,12 +852,12 @@ await Session.Connection.CloseAsync(reason, false,

protected async Task<bool> HandleConnectionTuneAsync(IncomingCommand cmd, CancellationToken cancellationToken)
{
// Note: `using` here to ensure instance is disposed
using var k = (ConnectionSecureOrTuneAsyncRpcContinuation)_continuationQueue.Next();

// Note: releases the continuation and returns the buffers
await k.HandleCommandAsync(cmd)
.ConfigureAwait(false);
using (var k = (ConnectionSecureOrTuneAsyncRpcContinuation)_continuationQueue.Next())
{
// Note: releases the continuation and returns the buffers
await k.HandleCommandAsync(cmd)
.ConfigureAwait(false);
}

return true;
}
Expand Down
5 changes: 4 additions & 1 deletion projects/RabbitMQ.Client/Impl/RpcContinuationQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public void Enqueue(IRpcContinuation k)
///</remarks>
public void HandleChannelShutdown(ShutdownEventArgs reason)
{
Next().HandleChannelShutdown(reason);
using (IRpcContinuation c = Next())
{
c.HandleChannelShutdown(reason);
}
}

///<summary>Retrieve the next waiting continuation.</summary>
Expand Down