Skip to content

Commit eaa7e33

Browse files
committed
Fix econnaborted seen in integration test runs
This error was introduced with commit 1fa0562 The main frame reading loop was cancelled too soon.
1 parent f664259 commit eaa7e33

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Task UpdateSecretAsync(string newSecret, string reason)
5050

5151
internal void NotifyReceivedCloseOk()
5252
{
53-
TerminateMainloop();
53+
MaybeTerminateMainloopAndStopHeartbeatTimers(cancelMainLoop: true);
5454
_closed = true;
5555
}
5656

@@ -112,7 +112,11 @@ await _frameHandler.SendProtocolHeaderAsync(cancellationToken)
112112
var serverVersion = new AmqpVersion(connectionStart.m_versionMajor, connectionStart.m_versionMinor);
113113
if (!serverVersion.Equals(Protocol.Version))
114114
{
115-
TerminateMainloop();
115+
/*
116+
* Note:
117+
* FinishCloseAsync will cancel the main loop
118+
*/
119+
MaybeTerminateMainloopAndStopHeartbeatTimers();
116120
await FinishCloseAsync(cancellationToken);
117121
throw new ProtocolVersionMismatchException(Protocol.MajorVersion, Protocol.MinorVersion, serverVersion.Major, serverVersion.Minor);
118122
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,13 @@ private async void HeartbeatReadTimerCallback(object? state)
116116

117117
if (shouldTerminate)
118118
{
119-
TerminateMainloop();
120-
await FinishCloseAsync(_mainLoopCts.Token)
119+
MaybeTerminateMainloopAndStopHeartbeatTimers();
120+
/*
121+
* Note: do NOT use the main loop cancellation token,
122+
* because FininshCloseAsync immediately cancels it
123+
*/
124+
using var cts = new CancellationTokenSource(InternalConstants.DefaultConnectionAbortTimeout);
125+
await FinishCloseAsync(cts.Token)
121126
.ConfigureAwait(false);
122127
}
123128
else

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,12 @@ private async Task ProcessFrameAsync(InboundFrame frame, CancellationToken cance
165165
///<remarks>
166166
/// May be called more than once. Should therefore be idempotent.
167167
///</remarks>
168-
private void TerminateMainloop()
168+
private void MaybeTerminateMainloopAndStopHeartbeatTimers(bool cancelMainLoop = false)
169169
{
170-
_mainLoopCts.Cancel();
170+
if (cancelMainLoop)
171+
{
172+
_mainLoopCts.Cancel();
173+
}
171174
MaybeStopHeartbeatTimers();
172175
}
173176

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ await _session0.TransmitAsync(method, cancellationToken)
362362
}
363363
finally
364364
{
365-
TerminateMainloop();
365+
/*
366+
* Note:
367+
* NotifyReceivedCloseOk will cancel the main loop
368+
*/
369+
MaybeTerminateMainloopAndStopHeartbeatTimers();
366370
}
367371
}
368372

@@ -402,7 +406,7 @@ internal void ClosedViaPeer(ShutdownEventArgs reason)
402406

403407
OnShutdown(reason);
404408
_session0.SetSessionClosing(true);
405-
TerminateMainloop();
409+
MaybeTerminateMainloopAndStopHeartbeatTimers(cancelMainLoop: true);
406410
}
407411

408412
// Only call at the end of the Mainloop or HeartbeatLoop

0 commit comments

Comments
 (0)