Skip to content

fix ConnectAsync not respecting the connection timeout #1502

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

Merged
merged 1 commit into from
Sep 22, 2024
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
12 changes: 11 additions & 1 deletion src/Renci.SshNet/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
DisposeSession(session);
}

Session = await CreateAndConnectSessionAsync(cancellationToken).ConfigureAwait(false);
using var timeoutCancellationTokenSource = new CancellationTokenSource(ConnectionInfo.Timeout);
using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationTokenSource.Token);

try
{
Session = await CreateAndConnectSessionAsync(linkedCancellationTokenSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException ex) when (timeoutCancellationTokenSource.IsCancellationRequested)
{
throw new SshOperationTimeoutException("Connection has timed out.", ex);
}
}

try
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;

#if !NET8_0_OR_GREATER
using Renci.SshNet.Abstractions;
#endif
using Renci.SshNet.Common;
using Renci.SshNet.Connection;

namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public class BaseClientTest_ConnectAsync_Timeout
{
private BaseClient _client;

[TestInitialize]
public void Init()
{
var sessionMock = new Mock<ISession>();
var serviceFactoryMock = new Mock<IServiceFactory>();
var socketFactoryMock = new Mock<ISocketFactory>();

sessionMock.Setup(p => p.ConnectAsync(It.IsAny<CancellationToken>()))
.Returns<CancellationToken>(c => Task.Delay(Timeout.Infinite, c));

serviceFactoryMock.Setup(p => p.CreateSocketFactory())
.Returns(socketFactoryMock.Object);

var connectionInfo = new ConnectionInfo("host", "user", new PasswordAuthenticationMethod("user", "pwd"))
{
Timeout = TimeSpan.FromSeconds(1)
};

serviceFactoryMock.Setup(p => p.CreateSession(connectionInfo, socketFactoryMock.Object))
.Returns(sessionMock.Object);

_client = new MyClient(connectionInfo, false, serviceFactoryMock.Object);
}

[TestMethod]
public async Task ConnectAsyncWithTimeoutThrowsSshTimeoutException()
{
await Assert.ThrowsExceptionAsync<SshOperationTimeoutException>(() => _client.ConnectAsync(CancellationToken.None));
}

[TestMethod]
public async Task ConnectAsyncWithCancelledTokenThrowsOperationCancelledException()
{
using var cancellationTokenSource = new CancellationTokenSource();
await cancellationTokenSource.CancelAsync();
await Assert.ThrowsExceptionAsync<OperationCanceledException>(() => _client.ConnectAsync(cancellationTokenSource.Token));
}

[TestCleanup]
public void Cleanup()
{
_client?.Dispose();
}

private class MyClient : BaseClient
{
public MyClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory) : base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
}
}
}
}