Skip to content

Commit 722aa36

Browse files
committed
* Change connection so it disposes its channel and session
1 parent be31dd6 commit 722aa36

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

projects/RabbitMQ.Client/client/impl/ChannelBase.cs

+1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ protected virtual void Dispose(bool disposing)
544544
this.AbortAsync().GetAwaiter().GetResult();
545545
}
546546
ConsumerDispatcher.Dispose();
547+
Session.Dispose();
547548
_rpcSemaphore.Dispose();
548549
_confirmSemaphore?.Dispose();
549550
}

projects/RabbitMQ.Client/client/impl/Connection.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal sealed partial class Connection : IConnection
5050
private volatile bool _closed;
5151

5252
private readonly ConnectionConfig _config;
53-
private readonly ChannelBase _channel0; // FUTURE Note: this is not disposed
53+
private readonly ChannelBase _channel0;
5454
private readonly MainSession _session0;
5555

5656
private Guid _id = Guid.NewGuid();
@@ -513,7 +513,8 @@ private void Dispose(bool disposing)
513513
{
514514
this.AbortAsync().GetAwaiter().GetResult();
515515
}
516-
_session0.Dispose();
516+
((IDisposable)_channel0).Dispose();
517+
((IDisposable)_session0).Dispose();
517518
_mainLoopCts.Dispose();
518519
_sessionManager.Dispose();
519520
}

projects/RabbitMQ.Client/client/impl/ISession.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace RabbitMQ.Client.Impl
3939
{
4040
internal delegate Task CommandReceivedAction(IncomingCommand cmd, CancellationToken cancellationToken);
4141

42-
internal interface ISession
42+
internal interface ISession : IDisposable
4343
{
4444
/// <summary>
4545
/// Gets the channel number.

projects/RabbitMQ.Client/client/impl/MainSession.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
namespace RabbitMQ.Client.Impl
4444
{
4545
///<summary>Small ISession implementation used only for channel 0.</summary>
46-
internal sealed class MainSession : Session, IDisposable
46+
internal sealed class MainSession : Session
4747
{
4848
private volatile bool _closeIsServerInitiated;
4949
private volatile bool _closing;
@@ -121,6 +121,17 @@ public override ValueTask TransmitAsync<T>(in T cmd, CancellationToken cancellat
121121
return base.TransmitAsync(in cmd, cancellationToken);
122122
}
123123

124-
public void Dispose() => ((IDisposable)_closingSemaphore).Dispose();
124+
protected override void Dispose(bool disposing)
125+
{
126+
if (disposing)
127+
{
128+
if (false == _disposedValue)
129+
{
130+
((IDisposable)_closingSemaphore).Dispose();
131+
}
132+
}
133+
134+
base.Dispose(disposing);
135+
}
125136
}
126137
}

projects/RabbitMQ.Client/client/impl/SessionBase.cs

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace RabbitMQ.Client.Impl
4444
{
4545
internal abstract class SessionBase : ISession
4646
{
47+
protected bool _disposedValue;
4748
private ShutdownEventArgs _closeReason;
4849
public ShutdownEventArgs CloseReason => Volatile.Read(ref _closeReason);
4950

@@ -169,5 +170,20 @@ public ValueTask TransmitAsync<TMethod, THeader>(in TMethod cmd, in THeader head
169170

170171
private void ThrowAlreadyClosedException()
171172
=> throw new AlreadyClosedException(CloseReason);
173+
174+
protected virtual void Dispose(bool disposing)
175+
{
176+
if (disposing)
177+
{
178+
_disposedValue = true;
179+
}
180+
}
181+
182+
public void Dispose()
183+
{
184+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
185+
Dispose(disposing: true);
186+
GC.SuppressFinalize(this);
187+
}
172188
}
173189
}

projects/RabbitMQ.Client/client/impl/SessionManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ public ISession Lookup(int number)
122122
}
123123
}
124124

125-
public void Dispose() => _sessionMapSemaphore.Dispose();
125+
public void Dispose() => ((IDisposable)_sessionMapSemaphore).Dispose();
126126
}
127127
}

0 commit comments

Comments
 (0)