diff --git a/projects/RabbitMQ.Client/Impl/AutorecoveringChannel.cs b/projects/RabbitMQ.Client/Impl/AutorecoveringChannel.cs index b057af115..8cfb9553e 100644 --- a/projects/RabbitMQ.Client/Impl/AutorecoveringChannel.cs +++ b/projects/RabbitMQ.Client/Impl/AutorecoveringChannel.cs @@ -254,10 +254,23 @@ await _connection.DeleteRecordedChannelAsync(this, public override string ToString() => InnerChannel.ToString(); - public void Dispose() => DisposeAsync().AsTask().GetAwaiter().GetResult(); + public void Dispose() + { + if (_disposed) + { + return; + } + + DisposeAsync().AsTask().GetAwaiter().GetResult(); + } public async ValueTask DisposeAsync() { + if (_disposed) + { + return; + } + if (IsDisposing) { return; diff --git a/projects/RabbitMQ.Client/Impl/AutorecoveringConnection.cs b/projects/RabbitMQ.Client/Impl/AutorecoveringConnection.cs index 879ed249a..b3a967a05 100644 --- a/projects/RabbitMQ.Client/Impl/AutorecoveringConnection.cs +++ b/projects/RabbitMQ.Client/Impl/AutorecoveringConnection.cs @@ -270,10 +270,23 @@ await RecordChannelAsync(autorecoveringChannel, channelsSemaphoreHeld: false, ca return autorecoveringChannel; } - public void Dispose() => DisposeAsync().AsTask().GetAwaiter().GetResult(); + public void Dispose() + { + if (_disposed) + { + return; + } + + DisposeAsync().AsTask().GetAwaiter().GetResult(); + } public async ValueTask DisposeAsync() { + if (_disposed) + { + return; + } + if (IsDisposing) { return; diff --git a/projects/RabbitMQ.Client/Impl/Channel.cs b/projects/RabbitMQ.Client/Impl/Channel.cs index 7293f584c..20b4175d5 100644 --- a/projects/RabbitMQ.Client/Impl/Channel.cs +++ b/projects/RabbitMQ.Client/Impl/Channel.cs @@ -529,6 +529,11 @@ void IDisposable.Dispose() protected virtual void Dispose(bool disposing) { + if (_disposed) + { + return; + } + if (IsDisposing) { return; @@ -559,6 +564,11 @@ protected virtual void Dispose(bool disposing) public async ValueTask DisposeAsync() { + if (_disposed) + { + return; + } + await DisposeAsyncCore() .ConfigureAwait(false); @@ -567,6 +577,11 @@ await DisposeAsyncCore() protected virtual async ValueTask DisposeAsyncCore() { + if (_disposed) + { + return; + } + if (IsDisposing) { return; @@ -576,7 +591,8 @@ protected virtual async ValueTask DisposeAsyncCore() { if (IsOpen) { - await this.AbortAsync().ConfigureAwait(false); + await this.AbortAsync() + .ConfigureAwait(false); } if (_serverOriginatedChannelCloseTcs is not null) diff --git a/projects/RabbitMQ.Client/Impl/Connection.cs b/projects/RabbitMQ.Client/Impl/Connection.cs index fd88b5b5b..247543a90 100644 --- a/projects/RabbitMQ.Client/Impl/Connection.cs +++ b/projects/RabbitMQ.Client/Impl/Connection.cs @@ -487,10 +487,23 @@ internal ValueTask WriteAsync(RentedMemory frames, CancellationToken cancellatio return _frameHandler.WriteAsync(frames, cancellationToken); } - public void Dispose() => DisposeAsync().AsTask().GetAwaiter().GetResult(); + public void Dispose() + { + if (_disposed) + { + return; + } + + DisposeAsync().AsTask().GetAwaiter().GetResult(); + } public async ValueTask DisposeAsync() { + if (_disposed) + { + return; + } + if (IsDisposing) { return; diff --git a/projects/Test/Integration/TestQueueDeclare.cs b/projects/Test/Integration/TestQueueDeclare.cs index 91510029f..9c161d8d7 100644 --- a/projects/Test/Integration/TestQueueDeclare.cs +++ b/projects/Test/Integration/TestQueueDeclare.cs @@ -65,9 +65,9 @@ public async Task TestPassiveQueueDeclareException_GH1749() { await _channel.QueueDeclarePassiveAsync(q); } - catch (Exception ex) + catch (Exception) { - _output.WriteLine("{0} ex: {1}", _testDisplayName, ex); + // _output.WriteLine("{0} ex: {1}", _testDisplayName, ex); await _channel.DisposeAsync(); _channel = null; } diff --git a/projects/Test/Integration/TestToxiproxy.cs b/projects/Test/Integration/TestToxiproxy.cs index 4aeb219ec..75e4af1e7 100644 --- a/projects/Test/Integration/TestToxiproxy.cs +++ b/projects/Test/Integration/TestToxiproxy.cs @@ -61,14 +61,25 @@ public override Task InitializeAsync() Assert.Null(_conn); Assert.Null(_channel); - _toxiproxyManager = new ToxiproxyManager(_testDisplayName, IsRunningInCI, IsWindows); - _proxyPort = ToxiproxyManager.ProxyPort; - return _toxiproxyManager.InitializeAsync(); + if (AreToxiproxyTestsEnabled) + { + _toxiproxyManager = new ToxiproxyManager(_testDisplayName, IsRunningInCI, IsWindows); + _proxyPort = ToxiproxyManager.ProxyPort; + return _toxiproxyManager.InitializeAsync(); + } + else + { + return Task.CompletedTask; + } } public override async Task DisposeAsync() { - await _toxiproxyManager.DisposeAsync(); + if (AreToxiproxyTestsEnabled) + { + await _toxiproxyManager.DisposeAsync(); + } + await base.DisposeAsync(); }