diff --git a/projects/RabbitMQ.Client/client/impl/ChannelBase.cs b/projects/RabbitMQ.Client/client/impl/ChannelBase.cs index f5095e4a3c..95c1bf03c8 100644 --- a/projects/RabbitMQ.Client/client/impl/ChannelBase.cs +++ b/projects/RabbitMQ.Client/client/impl/ChannelBase.cs @@ -544,6 +544,7 @@ protected virtual void Dispose(bool disposing) this.AbortAsync().GetAwaiter().GetResult(); } ConsumerDispatcher.Dispose(); + Session.Dispose(); _rpcSemaphore.Dispose(); _confirmSemaphore?.Dispose(); } diff --git a/projects/RabbitMQ.Client/client/impl/Connection.cs b/projects/RabbitMQ.Client/client/impl/Connection.cs index e80026d889..2eaa80468f 100644 --- a/projects/RabbitMQ.Client/client/impl/Connection.cs +++ b/projects/RabbitMQ.Client/client/impl/Connection.cs @@ -50,7 +50,7 @@ internal sealed partial class Connection : IConnection private volatile bool _closed; private readonly ConnectionConfig _config; - private readonly ChannelBase _channel0; // FUTURE Note: this is not disposed + private readonly ChannelBase _channel0; private readonly MainSession _session0; private Guid _id = Guid.NewGuid(); @@ -513,7 +513,8 @@ private void Dispose(bool disposing) { this.AbortAsync().GetAwaiter().GetResult(); } - _session0.Dispose(); + ((IDisposable)_channel0).Dispose(); + ((IDisposable)_session0).Dispose(); _mainLoopCts.Dispose(); _sessionManager.Dispose(); } diff --git a/projects/RabbitMQ.Client/client/impl/ISession.cs b/projects/RabbitMQ.Client/client/impl/ISession.cs index 1aa5d7342b..06eea02945 100644 --- a/projects/RabbitMQ.Client/client/impl/ISession.cs +++ b/projects/RabbitMQ.Client/client/impl/ISession.cs @@ -39,7 +39,7 @@ namespace RabbitMQ.Client.Impl { internal delegate Task CommandReceivedAction(IncomingCommand cmd, CancellationToken cancellationToken); - internal interface ISession + internal interface ISession : IDisposable { /// /// Gets the channel number. diff --git a/projects/RabbitMQ.Client/client/impl/MainSession.cs b/projects/RabbitMQ.Client/client/impl/MainSession.cs index 68b08a2297..8a677e5557 100644 --- a/projects/RabbitMQ.Client/client/impl/MainSession.cs +++ b/projects/RabbitMQ.Client/client/impl/MainSession.cs @@ -43,7 +43,7 @@ namespace RabbitMQ.Client.Impl { ///Small ISession implementation used only for channel 0. - internal sealed class MainSession : Session, IDisposable + internal sealed class MainSession : Session { private volatile bool _closeIsServerInitiated; private volatile bool _closing; @@ -121,6 +121,17 @@ public override ValueTask TransmitAsync(in T cmd, CancellationToken cancellat return base.TransmitAsync(in cmd, cancellationToken); } - public void Dispose() => ((IDisposable)_closingSemaphore).Dispose(); + protected override void Dispose(bool disposing) + { + if (disposing) + { + if (false == _disposedValue) + { + ((IDisposable)_closingSemaphore).Dispose(); + } + } + + base.Dispose(disposing); + } } } diff --git a/projects/RabbitMQ.Client/client/impl/SessionBase.cs b/projects/RabbitMQ.Client/client/impl/SessionBase.cs index 2f35291722..74dc5f433f 100644 --- a/projects/RabbitMQ.Client/client/impl/SessionBase.cs +++ b/projects/RabbitMQ.Client/client/impl/SessionBase.cs @@ -44,6 +44,7 @@ namespace RabbitMQ.Client.Impl { internal abstract class SessionBase : ISession { + protected bool _disposedValue; private ShutdownEventArgs _closeReason; public ShutdownEventArgs CloseReason => Volatile.Read(ref _closeReason); @@ -169,5 +170,20 @@ public ValueTask TransmitAsync(in TMethod cmd, in THeader head private void ThrowAlreadyClosedException() => throw new AlreadyClosedException(CloseReason); + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + _disposedValue = true; + } + } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } } diff --git a/projects/RabbitMQ.Client/client/impl/SessionManager.cs b/projects/RabbitMQ.Client/client/impl/SessionManager.cs index 8eccd6aad1..903feea5d7 100644 --- a/projects/RabbitMQ.Client/client/impl/SessionManager.cs +++ b/projects/RabbitMQ.Client/client/impl/SessionManager.cs @@ -122,6 +122,6 @@ public ISession Lookup(int number) } } - public void Dispose() => _sessionMapSemaphore.Dispose(); + public void Dispose() => ((IDisposable)_sessionMapSemaphore).Dispose(); } }