Skip to content

Commit

Permalink
Ensure DisposeAsync and Dispose are idempotent
Browse files Browse the repository at this point in the history
Fixes #1749
  • Loading branch information
lukebakken committed Dec 20, 2024
1 parent 377dbe0 commit 050173d
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions projects/RabbitMQ.Client/Impl/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ internal partial class Channel : IChannel, IRecoverable

internal readonly IConsumerDispatcher ConsumerDispatcher;

private bool _disposedValue = false;

public Channel(ISession session, CreateChannelOptions createChannelOptions)
{
ContinuationTimeout = createChannelOptions.ContinuationTimeout;
Expand Down Expand Up @@ -517,44 +519,63 @@ void IDisposable.Dispose()
Dispose(true);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (IsOpen)
{
this.AbortAsync().GetAwaiter().GetResult();
}

ConsumerDispatcher.Dispose();
_rpcSemaphore.Dispose();
_confirmSemaphore.Dispose();
_outstandingPublisherConfirmationsRateLimiter?.Dispose();
}
}

public async ValueTask DisposeAsync()
{
await DisposeAsyncCore()
await DisposeAsyncCore(true)
.ConfigureAwait(false);

Dispose(false);
}

protected virtual async ValueTask DisposeAsyncCore()
protected virtual void Dispose(bool disposing)
{
if (IsOpen)
if (disposing)
{
await this.AbortAsync().ConfigureAwait(false);
try
{
if (IsOpen)
{
this.AbortAsync().GetAwaiter().GetResult();
}

ConsumerDispatcher.Dispose();
_rpcSemaphore.Dispose();
_confirmSemaphore.Dispose();
_outstandingPublisherConfirmationsRateLimiter?.Dispose();
}
finally
{
_disposedValue = true;
}
}
}

ConsumerDispatcher.Dispose();
_rpcSemaphore.Dispose();
_confirmSemaphore.Dispose();
if (_outstandingPublisherConfirmationsRateLimiter is not null)
protected virtual async ValueTask DisposeAsyncCore(bool disposing)
{
if (disposing)
{
await _outstandingPublisherConfirmationsRateLimiter.DisposeAsync()
.ConfigureAwait(false);
try
{
if (IsOpen)
{
await this.AbortAsync()
.ConfigureAwait(false);
}

ConsumerDispatcher.Dispose();
_rpcSemaphore.Dispose();
_confirmSemaphore.Dispose();

if (_outstandingPublisherConfirmationsRateLimiter is not null)
{
await _outstandingPublisherConfirmationsRateLimiter.DisposeAsync()
.ConfigureAwait(false);
}
}
finally
{
_disposedValue = true;
}
}
}

Expand Down

0 comments on commit 050173d

Please sign in to comment.