Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 1807e95

Browse files
authored
Merge pull request #1934 from TarasKovalenko/disposeAsync
feat: changing the Dispose with potential deadlocks to the DisposeAsync
2 parents ce50bb8 + 94c300b commit 1807e95

File tree

8 files changed

+31
-42
lines changed

8 files changed

+31
-42
lines changed

src/BuildingBlocks/EventBus/EventBus/Extensions/GenericTypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public static class GenericTypeExtensions
44
{
55
public static string GetGenericTypeName(this Type type)
66
{
7-
var typeName = string.Empty;
7+
string typeName;
88

99
if (type.IsGenericType)
1010
{

src/BuildingBlocks/EventBus/EventBusRabbitMQ/DefaultRabbitMQPersistentConnection.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ public class DefaultRabbitMQPersistentConnection
66
private readonly IConnectionFactory _connectionFactory;
77
private readonly ILogger<DefaultRabbitMQPersistentConnection> _logger;
88
private readonly int _retryCount;
9-
IConnection _connection;
10-
bool _disposed;
9+
private IConnection _connection;
10+
public bool Disposed;
1111

12-
object sync_root = new object();
12+
readonly object _syncRoot = new();
1313

1414
public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory, ILogger<DefaultRabbitMQPersistentConnection> logger, int retryCount = 5)
1515
{
@@ -18,13 +18,7 @@ public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory,
1818
_retryCount = retryCount;
1919
}
2020

21-
public bool IsConnected
22-
{
23-
get
24-
{
25-
return _connection != null && _connection.IsOpen && !_disposed;
26-
}
27-
}
21+
public bool IsConnected => _connection is { IsOpen: true } && !Disposed;
2822

2923
public IModel CreateModel()
3024
{
@@ -38,9 +32,9 @@ public IModel CreateModel()
3832

3933
public void Dispose()
4034
{
41-
if (_disposed) return;
35+
if (Disposed) return;
4236

43-
_disposed = true;
37+
Disposed = true;
4438

4539
try
4640
{
@@ -59,7 +53,7 @@ public bool TryConnect()
5953
{
6054
_logger.LogInformation("RabbitMQ Client is trying to connect");
6155

62-
lock (sync_root)
56+
lock (_syncRoot)
6357
{
6458
var policy = RetryPolicy.Handle<SocketException>()
6559
.Or<BrokerUnreachableException>()
@@ -96,7 +90,7 @@ public bool TryConnect()
9690

9791
private void OnConnectionBlocked(object sender, ConnectionBlockedEventArgs e)
9892
{
99-
if (_disposed) return;
93+
if (Disposed) return;
10094

10195
_logger.LogWarning("A RabbitMQ connection is shutdown. Trying to re-connect...");
10296

@@ -105,7 +99,7 @@ private void OnConnectionBlocked(object sender, ConnectionBlockedEventArgs e)
10599

106100
void OnCallbackException(object sender, CallbackExceptionEventArgs e)
107101
{
108-
if (_disposed) return;
102+
if (Disposed) return;
109103

110104
_logger.LogWarning("A RabbitMQ connection throw exception. Trying to re-connect...");
111105

@@ -114,7 +108,7 @@ void OnCallbackException(object sender, CallbackExceptionEventArgs e)
114108

115109
void OnConnectionShutdown(object sender, ShutdownEventArgs reason)
116110
{
117-
if (_disposed) return;
111+
if (Disposed) return;
118112

119113
_logger.LogWarning("A RabbitMQ connection is on shutdown. Trying to re-connect...");
120114

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private async Task ProcessEvent(string eventName, string message)
240240

241241
if (_subsManager.HasSubscriptionsForEvent(eventName))
242242
{
243-
using var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME);
243+
await using var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME);
244244
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
245245
foreach (var subscription in subscriptions)
246246
{

src/BuildingBlocks/EventBus/EventBusServiceBus/DefaultServiceBusPersisterConnection.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ public ServiceBusClient TopicClient
2727
}
2828
}
2929

30-
public ServiceBusAdministrationClient AdministrationClient
31-
{
32-
get
33-
{
34-
return _subscriptionClient;
35-
}
36-
}
30+
public ServiceBusAdministrationClient AdministrationClient =>
31+
_subscriptionClient;
3732

3833
public ServiceBusClient CreateModel()
3934
{
@@ -45,11 +40,11 @@ public ServiceBusClient CreateModel()
4540
return _topicClient;
4641
}
4742

48-
public void Dispose()
43+
public async ValueTask DisposeAsync()
4944
{
5045
if (_disposed) return;
5146

5247
_disposed = true;
53-
_topicClient.DisposeAsync().GetAwaiter().GetResult();
48+
await _topicClient.DisposeAsync();
5449
}
5550
}

src/BuildingBlocks/EventBus/EventBusServiceBus/EventBusServiceBus.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
22

3-
public class EventBusServiceBus : IEventBus, IDisposable
3+
public class EventBusServiceBus : IEventBus, IAsyncDisposable
44
{
55
private readonly IServiceBusPersisterConnection _serviceBusPersisterConnection;
66
private readonly ILogger<EventBusServiceBus> _logger;
77
private readonly IEventBusSubscriptionsManager _subsManager;
88
private readonly ILifetimeScope _autofac;
99
private readonly string _topicName = "eshop_event_bus";
1010
private readonly string _subscriptionName;
11-
private ServiceBusSender _sender;
12-
private ServiceBusProcessor _processor;
11+
private readonly ServiceBusSender _sender;
12+
private readonly ServiceBusProcessor _processor;
1313
private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
1414
private const string INTEGRATION_EVENT_SUFFIX = "IntegrationEvent";
1515

@@ -134,12 +134,6 @@ private async Task RegisterSubscriptionClientMessageHandlerAsync()
134134
await _processor.StartProcessingAsync();
135135
}
136136

137-
public void Dispose()
138-
{
139-
_subsManager.Clear();
140-
_processor.CloseAsync().GetAwaiter().GetResult();
141-
}
142-
143137
private Task ErrorHandler(ProcessErrorEventArgs args)
144138
{
145139
var ex = args.Exception;
@@ -173,7 +167,7 @@ private async Task<bool> ProcessEvent(string eventName, string message)
173167
var eventType = _subsManager.GetEventTypeByName(eventName);
174168
var integrationEvent = JsonSerializer.Deserialize(message, eventType);
175169
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
176-
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
170+
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new[] { integrationEvent });
177171
}
178172
}
179173
}
@@ -196,4 +190,10 @@ private void RemoveDefaultRule()
196190
_logger.LogWarning("The messaging entity {DefaultRuleName} Could not be found.", RuleProperties.DefaultRuleName);
197191
}
198192
}
193+
194+
public async ValueTask DisposeAsync()
195+
{
196+
_subsManager.Clear();
197+
await _processor.CloseAsync();
198+
}
199199
}

src/BuildingBlocks/EventBus/EventBusServiceBus/IServiceBusPersisterConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
22

3-
public interface IServiceBusPersisterConnection : IDisposable
3+
public interface IServiceBusPersisterConnection : IAsyncDisposable
44
{
55
ServiceBusClient TopicClient { get; }
66
ServiceBusAdministrationClient AdministrationClient { get; }

src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task<IEnumerable<IntegrationEventLogEntry>> RetrieveEventLogsPendin
2828
var result = await _integrationEventLogContext.IntegrationEventLogs
2929
.Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished).ToListAsync();
3030

31-
if (result != null && result.Any())
31+
if (result.Any())
3232
{
3333
return result.OrderBy(o => o.CreationTime)
3434
.Select(e => e.DeserializeJsonContent(_eventTypes.Find(t => t.Name == e.EventTypeShortName)));

src/BuildingBlocks/EventBus/IntegrationEventLogEF/Utilities/ResilientTransaction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class ResilientTransaction
44
{
5-
private DbContext _context;
5+
private readonly DbContext _context;
66
private ResilientTransaction(DbContext context) =>
77
_context = context ?? throw new ArgumentNullException(nameof(context));
88

@@ -15,9 +15,9 @@ public async Task ExecuteAsync(Func<Task> action)
1515
var strategy = _context.Database.CreateExecutionStrategy();
1616
await strategy.ExecuteAsync(async () =>
1717
{
18-
using var transaction = _context.Database.BeginTransaction();
18+
await using var transaction = await _context.Database.BeginTransactionAsync();
1919
await action();
20-
transaction.Commit();
20+
await transaction.CommitAsync();
2121
});
2222
}
2323
}

0 commit comments

Comments
 (0)