Skip to content

Commit 0731895

Browse files
committed
Address ObjectDisposedException
Fixes #1802 * Start by modifying channel creation program to try and reproduce the issue.
1 parent 24a3772 commit 0731895

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

projects/Applications/CreateChannel/Program.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131

3232
using System;
3333
using System.Diagnostics;
34-
using System.Threading;
34+
using System.Globalization;
35+
using System.Runtime.ExceptionServices;
3536
using System.Threading.Tasks;
3637

3738
using RabbitMQ.Client;
39+
using RabbitMQ.Client.Exceptions;
3840

3941
namespace CreateChannel
4042
{
@@ -44,11 +46,11 @@ public static class Program
4446
private const int ChannelsToOpen = 50;
4547

4648
private static int channelsOpened;
47-
private static AutoResetEvent doneEvent;
49+
private readonly static TaskCompletionSource<bool> s_tcs = new();
4850

4951
public static async Task Main()
5052
{
51-
doneEvent = new AutoResetEvent(false);
53+
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
5254

5355
var connectionFactory = new ConnectionFactory { };
5456
await using IConnection connection = await connectionFactory.CreateConnectionAsync();
@@ -67,26 +69,48 @@ public static async Task Main()
6769

6870
for (int j = 0; j < channels.Length; j++)
6971
{
72+
if (j % 2 == 0)
73+
{
74+
try
75+
{
76+
await channels[j].QueueDeclarePassiveAsync(Guid.NewGuid().ToString());
77+
}
78+
catch (Exception)
79+
{
80+
}
81+
}
7082
await channels[j].DisposeAsync();
7183
}
7284
}
7385

74-
doneEvent.Set();
86+
s_tcs.SetResult(true);
7587
});
7688

7789
Console.WriteLine($"{Repeats} times opening {ChannelsToOpen} channels on a connection. => Total channel open/close: {Repeats * ChannelsToOpen}");
7890
Console.WriteLine();
7991
Console.WriteLine("Opened");
80-
while (!doneEvent.WaitOne(500))
92+
while (false == s_tcs.Task.IsCompleted)
8193
{
94+
await Task.Delay(500);
8295
Console.WriteLine($"{channelsOpened,5}");
8396
}
8497
watch.Stop();
8598
Console.WriteLine($"{channelsOpened,5}");
8699
Console.WriteLine();
87100
Console.WriteLine($"Took {watch.Elapsed.TotalMilliseconds} ms");
101+
}
102+
103+
private static string Now => DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture);
88104

89-
Console.ReadLine();
105+
private static void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
106+
{
107+
if (e.Exception is OperationInterruptedException)
108+
{
109+
}
110+
else
111+
{
112+
Console.Error.WriteLine("{0} [ERROR] {1}", Now, e.Exception);
113+
}
90114
}
91115
}
92116
}

0 commit comments

Comments
 (0)