You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using one SftpClient object, simultaneously on two different threads. While the primary thread is doing a large upload (UploadFile()), I have a background task making sure we're still connected by doing ListDirectory("/") every few seconds. When I do that, the SftpClient disconnects.
I tested this using two different SSH servers: My primary target is a small embedded device, running Dropbear SSH server. On that server, the disconnect usually happens on the very first call to ListDirectory. For testing, I also tried a server running OpenSSH on Rocky Linux 8. Using that, the call to ListDirectory generally succeeds several times, but it eventually fails as well.
I am using SSH.NET version 2024.2.0 on .Net 9.
Here is a self-contained demonstration program.
using Renci.SshNet;
namespace SshMultiUseTest;
internal class Program
{
#if false
// This is my small embedded device. Low power. Running PetaLinux on a Xilinx SoC.
// SSH Server is Dropbear v2020.80. (No, I can't change that.)
// (Why 127.0.0.1? I'm on a work computer, with a strict VPN. I have to use an SSH tunnel
// to make connections to machines on the local network.)
const string host = "127.0.0.1";
const int port = 2122;
const string user = "root";
const string privKey = "DeviceKey.id_rsa";
#else
// This is my Linux server. It's a normal desktop PC, maybe 10 years old. Intel i7 of some sort.
// Running Rocky Linux 8. SSH Server is OpenSSH 8.0p1.
const string host = "127.0.0.1";
const int port = 2200;
const string user = "dyaw";
const string privKey = "id_rsa";
#endif
const string largeFile = "C:\\Data\\deviceUpgrade.mfuf";
static void Main(string[] args)
{
SftpClient sftpClient = new(
host, port, user,
new PrivateKeyFile(typeof(Program).Assembly.GetManifestResourceStream(typeof(Program), privKey)!));
sftpClient.Connect();
// Verify connectivity
foreach (var item in sftpClient.ListDirectory("/")) { }
using FileStream fs = new(largeFile, FileMode.Open, FileAccess.Read);
int count = 0;
void callback(ulong uploaded)
{
int newCount = Interlocked.Increment(ref count);
if (newCount < 20 || newCount % 100 == 0)
Console.WriteLine("Uploaded: {0:N0}", uploaded);
}
Task largeUploadTask = Task.Run(() => sftpClient.UploadFile(fs, "/home/" + user + "/uploadTest", callback));
int exceptionCount = 0;
while (!largeUploadTask.IsCompleted)
{
Thread.Sleep(1000);
try
{
foreach (var item in sftpClient.ListDirectory("/")) { }
Console.WriteLine("Small Task: Directory Listing complete");
exceptionCount = 0;
}
catch (Exception e)
{
if (++exceptionCount < 5)
{
// These get repetitive after the first couple. Be quiet.
Console.WriteLine("From small task: {0}: {1}", e.GetType().FullName, e.Message);
}
}
}
try
{
largeUploadTask.Wait();
}
catch (Exception e)
{
if (e is AggregateException && e.InnerException != null)
e = e.InnerException;
Console.WriteLine("From large task: {0}: {1}", e.GetType().FullName, e.Message);
}
}
}
Result, connecting to the embedded device:
Uploaded: 65,460
Uploaded: 32,730
Uploaded: 98,190
Uploaded: 130,920
Uploaded: 163,650
Uploaded: 196,380
Uploaded: 229,110
Uploaded: 261,840
Uploaded: 294,570
Uploaded: 327,300
Uploaded: 360,030
Uploaded: 392,760
Uploaded: 425,490
Uploaded: 458,220
Uploaded: 490,950
Uploaded: 523,680
Uploaded: 556,410
Uploaded: 589,140
Uploaded: 621,870
Uploaded: 3,273,000
Uploaded: 6,546,000
From small task: Renci.SshNet.Common.SshException: Channel was closed.
From small task: System.InvalidOperationException: The session is not open.
From small task: System.InvalidOperationException: The session is not open.
From small task: System.InvalidOperationException: The session is not open.
(long pause here)
From large task: Renci.SshNet.Common.SshOperationTimeoutException: Session operation has timed out
I'm using one SftpClient object, simultaneously on two different threads. While the primary thread is doing a large upload (
UploadFile()
), I have a background task making sure we're still connected by doingListDirectory("/")
every few seconds. When I do that, the SftpClient disconnects.I tested this using two different SSH servers: My primary target is a small embedded device, running Dropbear SSH server. On that server, the disconnect usually happens on the very first call to ListDirectory. For testing, I also tried a server running OpenSSH on Rocky Linux 8. Using that, the call to ListDirectory generally succeeds several times, but it eventually fails as well.
I am using SSH.NET version 2024.2.0 on .Net 9.
Here is a self-contained demonstration program.
Result, connecting to the embedded device:
Result, connecting to my regular server:
The text was updated successfully, but these errors were encountered: