Skip to content

Commit

Permalink
Fix BasicRabbitService disconnection
Browse files Browse the repository at this point in the history
- Disposing channel and connection objects is not enough. See https://www.rabbitmq.com/dotnet-api-guide.html#disconnecting
- It's generally not a good practice to lock on 'this' because code from other classes can potentially acquire a lock on the same object leading to deadlocks. Refactored to new object like what is used elsewhere in this project
  • Loading branch information
jkamleh committed Nov 13, 2023
1 parent 5669e4d commit eb9f6af
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions SimpleRabbit.NetCore/Service/BasicRabbitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IBasicRabbitService : IDisposable

public abstract class BasicRabbitService : IBasicRabbitService
{
private readonly object _lock = new object();
private const ushort DefaultRequestedHeartBeat = 5;
private const int DefaultNetworkRecoveryInterval = 10;

Expand Down Expand Up @@ -79,27 +80,29 @@ protected BasicRabbitService(RabbitConfiguration config)

public IBasicProperties GetBasicProperties()
{
lock(this)
lock (_lock)
{
return Channel.CreateBasicProperties();
}
}

public void ClearConnection()
{
lock (this)
lock (_lock)
{
if (_disposed)
{
return;
}
try
{
_channel?.Close();
_channel?.Dispose();
_channel = null;
}
finally
{
_connection?.Close();
_connection?.Dispose();
_connection = null;
}
Expand All @@ -109,7 +112,7 @@ public void ClearConnection()

public void Close()
{
lock (this)
lock (_lock)
{
if (_disposed)
{
Expand Down

0 comments on commit eb9f6af

Please sign in to comment.