Skip to content

Commit f9a29d8

Browse files
Prevent concurrent recoveries and extra connections after recovery
1 parent 312263d commit f9a29d8

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

projects/client/RabbitMQ.Client/src/client/impl/AutorecoveringConnection.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,28 @@ public void init()
9292
var self = this;
9393
ConnectionShutdownEventHandler recoveryListener = (_, args) =>
9494
{
95-
if(ShouldTriggerConnectionRecovery(args))
95+
lock(self)
9696
{
97-
try
97+
if(ShouldTriggerConnectionRecovery(args))
9898
{
99-
self.BeginAutomaticRecovery();
100-
} catch (Exception e)
101-
{
102-
// TODO: logging
103-
Console.WriteLine("BeginAutomaticRecovery() failed: {0}", e);
104-
}
99+
try
100+
{
101+
self.BeginAutomaticRecovery();
102+
} catch (Exception e)
103+
{
104+
// TODO: logging
105+
Console.WriteLine("BeginAutomaticRecovery() failed: {0}", e);
106+
}
107+
}
105108
}
106109
};
107110
lock(this.m_eventLock)
108111
{
109112
this.ConnectionShutdown += recoveryListener;
110-
this.m_recordedShutdownEventHandlers.Add(recoveryListener);
113+
if(!this.m_recordedShutdownEventHandlers.Contains(recoveryListener))
114+
{
115+
this.m_recordedShutdownEventHandlers.Add(recoveryListener);
116+
}
111117
}
112118
}
113119

@@ -837,7 +843,7 @@ public void DeleteRecordedBinding(RecordedBinding rb)
837843
}
838844
}
839845

840-
public void RecordConsumer(string name, RecordedConsumer c)
846+
public void RecordConsumer(string name, RecordedConsumer c)
841847
{
842848
lock(this.m_recordedEntitiesLock)
843849
{

projects/client/Unit/src/unit/TestConnectionRecovery.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ public void TestConsumerRecoveryWithManyConsumers()
518518
}
519519

520520
[Test]
521+
[Category("Focus")]
521522
public void TestConsumerRecoveryOnClientNamedQueueWithOneRecovery()
522523
{
523524
var c = CreateAutorecoveringConnection();
@@ -535,6 +536,10 @@ public void TestConsumerRecoveryOnClientNamedQueueWithOneRecovery()
535536
latestName = current;
536537
};
537538

539+
CloseAndWaitForRecovery(c);
540+
AssertConsumerCount(m, latestName, 1);
541+
CloseAndWaitForRecovery(c);
542+
AssertConsumerCount(m, latestName, 1);
538543
CloseAndWaitForRecovery(c);
539544
AssertConsumerCount(m, latestName, 1);
540545

0 commit comments

Comments
 (0)