Skip to content

fix "client not connected" after SFTP reconnect #1484

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 2 commits into from
Sep 6, 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
10 changes: 10 additions & 0 deletions src/Renci.SshNet/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public void Connect()
var session = Session;
if (session is null || !session.IsConnected)
{
if (session is not null)
{
DisposeSession(session);
}

Session = CreateAndConnectSession();
}

Expand Down Expand Up @@ -304,6 +309,11 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
var session = Session;
if (session is null || !session.IsConnected)
{
if (session is not null)
{
DisposeSession(session);
}

Session = await CreateAndConnectSessionAsync(cancellationToken).ConfigureAwait(false);
}

Expand Down
11 changes: 2 additions & 9 deletions src/Renci.SshNet/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2496,15 +2496,8 @@ protected override void OnConnected()
{
base.OnConnected();

var sftpSession = _sftpSession;
if (sftpSession is null)
{
_sftpSession = CreateAndConnectToSftpSession();
}
else if (!sftpSession.IsOpen)
{
sftpSession.Connect();
}
_sftpSession?.Dispose();
_sftpSession = CreateAndConnectToSftpSession();
}

/// <summary>
Expand Down
48 changes: 48 additions & 0 deletions test/Renci.SshNet.IntegrationTests/ConnectivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,54 @@ public async Task SftpClient_HandleSftpSessionCloseAsync()
}
}

[TestMethod]
public void SftpClient_HandleSftpSessionAbortByServer()
{
using (var client = new SftpClient(_connectionInfoFactory.Create()))
{
client.Connect();
Assert.IsTrue(client.IsConnected);

_sshConnectionDisruptor.BreakConnections();
WaitForConnectionInterruption(client);
Assert.IsFalse(client.IsConnected);

client.Connect();
Assert.IsTrue(client.IsConnected);

foreach (var file in client.ListDirectory("."))
{
}

client.Disconnect();
Assert.IsFalse(client.IsConnected);
}
}

[TestMethod]
public async Task SftpClient_HandleSftpSessionAbortByServerAsync()
{
using (var client = new SftpClient(_connectionInfoFactory.Create()))
{
await client.ConnectAsync(CancellationToken.None);
Assert.IsTrue(client.IsConnected);

_sshConnectionDisruptor.BreakConnections();
WaitForConnectionInterruption(client);
Assert.IsFalse(client.IsConnected);

await client.ConnectAsync(CancellationToken.None);
Assert.IsTrue(client.IsConnected);

await foreach (var file in client.ListDirectoryAsync(".", CancellationToken.None))
{
}

client.Disconnect();
Assert.IsFalse(client.IsConnected);
}
}

[TestMethod]
public void Common_DetectSessionKilledOnServer()
{
Expand Down