Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create cancellation token from timeout #1775

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions projects/RabbitMQ.Client/Impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ public Task CloseAsync(ushort reasonCode, string reasonText, TimeSpan timeout, b
///</remarks>
internal async Task CloseAsync(ShutdownEventArgs reason, bool abort, TimeSpan timeout, CancellationToken cancellationToken)
{
using var timeoutCts = new CancellationTokenSource(timeout);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);

if (false == SetCloseReason(reason))
{
// close reason is already set
Expand All @@ -330,11 +333,9 @@ internal async Task CloseAsync(ShutdownEventArgs reason, bool abort, TimeSpan ti
}
else
{
cancellationToken.ThrowIfCancellationRequested();

await OnShutdownAsync(reason)
.ConfigureAwait(false);
await _session0.SetSessionClosingAsync(false, cancellationToken)
await _session0.SetSessionClosingAsync(false, cts.Token)
.ConfigureAwait(false);

try
Expand All @@ -343,7 +344,7 @@ await _session0.SetSessionClosingAsync(false, cancellationToken)
if (false == _closed)
{
var method = new ConnectionClose(reason.ReplyCode, reason.ReplyText, 0, 0);
await _session0.TransmitAsync(method, cancellationToken)
await _session0.TransmitAsync(method, cts.Token)
.ConfigureAwait(false);
}
}
Expand Down Expand Up @@ -392,14 +393,14 @@ await _session0.TransmitAsync(method, cancellationToken)

try
{
await _mainLoopTask.WaitAsync(timeout, cancellationToken)
await _mainLoopTask.WaitAsync(timeout, cts.Token)
.ConfigureAwait(false);
}
catch
{
try
{
await _frameHandler.CloseAsync(cancellationToken)
await _frameHandler.CloseAsync(cts.Token)
.ConfigureAwait(false);
}
catch
Expand Down Expand Up @@ -518,7 +519,6 @@ await this.AbortAsync()
}

_session0.Dispose();
_mainLoopCts.Dispose();

await _channel0.DisposeAsync()
.ConfigureAwait(false);
Expand All @@ -529,6 +529,7 @@ await _channel0.DisposeAsync()
}
finally
{
_mainLoopCts.Dispose();
_disposed = true;
}
}
Expand Down
28 changes: 27 additions & 1 deletion projects/RabbitMQ.Client/Impl/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal sealed class MainSession : Session, IDisposable
private volatile bool _closeIsServerInitiated;
private volatile bool _closing;
private readonly SemaphoreSlim _closingSemaphore = new SemaphoreSlim(1, 1);
private bool _disposed = false;

public MainSession(Connection connection, uint maxBodyLength)
: base(connection, 0, maxBodyLength)
Expand Down Expand Up @@ -83,6 +84,13 @@ public override Task HandleFrameAsync(InboundFrame frame, CancellationToken canc

public async Task SetSessionClosingAsync(bool closeIsServerInitiated, CancellationToken cancellationToken)
{
if (_disposed)
{
_closing = true;
_closeIsServerInitiated = closeIsServerInitiated;
return;
}

if (await _closingSemaphore.WaitAsync(InternalConstants.DefaultConnectionAbortTimeout, cancellationToken)
.ConfigureAwait(false))
{
Expand Down Expand Up @@ -122,6 +130,24 @@ public override ValueTask TransmitAsync<T>(in T cmd, CancellationToken cancellat
return base.TransmitAsync(in cmd, cancellationToken);
}

public void Dispose() => ((IDisposable)_closingSemaphore).Dispose();
public void Dispose()
{
if (_disposed)
{
return;
}

try
{
_closingSemaphore.Dispose();
}
catch
{
}
finally
{
_disposed = true;
}
}
}
}
16 changes: 16 additions & 0 deletions projects/Test/Integration/GH/TestGitHubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,21 @@ public async Task TestHeartbeatTimeoutValue_GH1756()

Assert.True(_conn.Heartbeat != default);
}

[Fact]
public async Task DisposeWhileCatchingTimeoutDeadlocksRepro_GH1759()
{
_connFactory = new ConnectionFactory();
_conn = await _connFactory.CreateConnectionAsync();
try
{
await _conn.CloseAsync(TimeSpan.Zero);
}
catch (Exception)
{
}

await _conn.DisposeAsync();
}
}
}