-
-
Notifications
You must be signed in to change notification settings - Fork 940
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
Sshclient deadlock/freeze on disconnect #355
Comments
I also encountered the same problem ( on OSX High Sierra, too). I found the code is locked by this line: |
The problem occurs on Sierra as well (using .NET Core 2.0). |
Same issue, also OSX High Sierra Any update? |
Now I am using the following code to avoid the issue: Task.Factory.StartNew(() => {
sshClient.Dispose();
}); and kill these threads later. Though the threads are blocked, the socket resources are in fact freed successfully before _messageListenerCompleted.WaitOne() is called. |
We run into the same issue, but on Linux. Can confirm that it's waiting on We have a wrapper class around the public void Dispose()
{
if (_client == null) return;
Task.Run(() =>
{
_log.Debug("Disposing _client");
var timer = new System.Timers.Timer();
timer.Interval = 2000;
timer.AutoReset = false;
timer.Elapsed += (s, e) =>
{
try
{
var sessionField = _client.GetType().GetProperty("Session", BindingFlags.NonPublic | BindingFlags.Instance);
if (sessionField != null)
{
var session = sessionField.GetValue(_client);
if (session != null)
{
var socketField = session.GetType().GetField("_socket", BindingFlags.NonPublic | BindingFlags.Instance);
if (socketField != null)
{
var socket = (Socket)socketField.GetValue(session);
if (socket != null)
{
_log.Debug($"Socket state: Connected = {socket.Connected}, Blocking = {socket.Blocking}, Available = {socket.Available}, LocalEndPoint = {socket.LocalEndPoint}, RemoteEndPoint = {socket.RemoteEndPoint}");
_log.Debug("Set _socket to null");
try
{
socket.Dispose();
}
catch (Exception ex)
{
_log.Debug("Exception disposing _socket", ex);
}
socketField.SetValue(session, null);
}
else
{
_log.Debug("_socket was null");
}
}
var messageListenerCompletedField = session.GetType().GetField("_messageListenerCompleted", BindingFlags.NonPublic | BindingFlags.Instance);
var messageListenerCompleted = (EventWaitHandle)messageListenerCompletedField.GetValue(session);
if (messageListenerCompleted != null)
{
var waitHandleSet = messageListenerCompleted.WaitOne(0);
_log.Debug($"_messageListenerCompleted was set = {waitHandleSet}");
if (!waitHandleSet)
{
_log.Debug($"Calling Set()");
messageListenerCompleted.Set();
}
}
else
{
_log.Debug("_messageListenerCompleted was null");
}
}
else
{
_log.Debug("Session was null");
}
}
}
catch (Exception ex)
{
_log.Debug($"Exception in Timer event handler", ex);
}
};
timer.Start();
_client.Dispose();
_log.Info("Disposed _client");
});
} A typical log when it fails to disconnect: But it usually disconnects just fine, not disconnecting is the exception. The ratios are 20 failures for 700 successful disconnects in about 12 hours. |
I have the same issue using .NET Core 2.0 on macOS Sierra 10.12.6 (16G1212)
|
Just ran into the same issue on Ubuntu 16.04, dotnet core 2.1 |
Ran into the same problem on macOS High Sierra, 10.13.5, dotnet core version 2.1.300 |
What version of SSH.NET are you using? |
@drieseng We use the latest ( |
@drieseng Any update? |
@drieseng - Getting it on macOS High Sierra dotnet core v2.1.301. Using the latest nuget package |
I don't have a mac :( |
Just tried to reproduce it in docker and couldn't... which I guess is a good thing because that's what we're using outside of our dev environments. Still, would be good to have it fixed for dev purposes. I'll see if I can have a crack at fixing but honestly, I wouldn't know where to start 😄 |
Hi, it fails here in SshCommand.cs
|
@Spica92 Please create a separate issue for this problem. Include as much details as possible (OS, SSH server, exact version of SSH.NET, …). |
@cvallance From your traces, it looks as if .NET Core doesn't break out of the You can always add some CWLs before and after the |
I've been able to reproduce this issue on Linux. |
Thanks for identifying the bug. |
@Calvfre the workaround code I posted above has been working well for us for a few months now. Nasty reflection, but it works. |
.NET say use Shutdown() to prevent endless hang: dotnet/corefx#26898 |
@mc-denisov If I am looking at this correctly, |
@jpmdl Thanks for the feedback! |
@drieseng I have the same issue when disconnecting the sftpclient, I'm so happy to see this. so when this package will upload to NuGet? |
Just encountered the same problem. Fix in 2020.0.0-beta1 works. Any idea when we can expect a stable 2020 release? |
Checkbox "Include prerelease" in NuGet's Browse within the package manager. |
Still buggy for me, handling ~1500 requests (sync too) this library goes in a deadlock with any version. I had to remove it and run ssh Linux command... :( |
Even with last week update
…Sent from my iPhone
On Jan 29, 2021, at 11:19 AM, ƒabio ***@***.***> wrote:
Still buggy for me, handling ~1500 requests (sync too) this library goes in a deadlock with any version. I had to remove it and run ssh Linux command... :(
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
If someone can provide the means for me to reproduce this issue, don't hesitate to reopen this issue (or create a new one). |
I'll try in a few days, thank you |
unfortunately, it still doesn't work, even with 2020.0.1 |
Can you share the code or the part of the code doing the connections so the author can replicate?
|
Sure, foreach (var address in addresses)
{
if (cancellationToken.IsCancellationRequested)
break;
var task = BackupAsync(address, cancellationToken);
tasks.Add(task);
}
await Task.WhenAll(tasks);
// code never reached here ... public class RenciWrapper : ISshWrapper
{
private const int DefaultPort = 22;
private readonly SshClient _client;
public RenciWrapper(string address, string user, string password)
{
var connectionInfo = new ConnectionInfo(
host: address,
port: DefaultPort,
username: user,
new AuthenticationMethod[]
{
new PasswordAuthenticationMethod(user, password),
}
)
{
Timeout = new TimeSpan(0, 0, 6),
RetryAttempts = 1,
};
_client = new SshClient(connectionInfo);
}
public Task<string> ExecuteCommandAsync(CancellationToken cancellationToken = default)
{
try
{
_client.Connect(); // not async :(
var command = _client.CreateCommand("export");
var response = command.Execute(); // I've also used https://github.com/JohnTheGr8/Renci.SshNet.Async
return Task.FromResult(response);
}
finally
{
_client?.Dispose();
}
}
public void Dispose()
{
_client?.Dispose();
}
} No problems with my async |
I think you should use disconnect BEFORE dispose. |
Still no luck try
{
_client.Connect();
var command = _client.CreateCommand("export");
var response = command.Execute();
return Task.FromResult(response);
}
finally
{
_client.Disconnect();
_client?.Dispose();
} I have some unreachable devices. unsuccessful connections could cause locks? |
We using the proposed fix in https://github.com/dotnet/corefx/issues/31368#issuecomment-424084210
We using the proposed fix in https://github.com/dotnet/corefx/issues/31368#issuecomment-424084210
Can confirm on Linux that 2020.0.0-beta1 works to avoid this issue, while 2020.0.1 has this problem This is also likely related to a problem that, on Linux, _client.IsConnected always returns true (after initial connection). Even sending a restart command and confirming the client disconnects, it still returns true. I was using it to confirm a client disconnected after attempting to restart it, and had to write separate logic to attempt repeated connections until one fails EDIT: Nope, beta doesn't work reliably either, it's just intermittent on both of them so it seemed like it was working properly. |
Hi @drieseng I'm getting issue also on my SFTP setup using the SSH.NET latest version using .NET 5.0. It works fine until I got this issue. Socket read operation has timed out after 30000 milliseconds. This is my current code.
|
Is this issue fixed with 2020.0.1 and .NET 5.0 or shall I simply avoid calling Disconnect() method and just Dispose() the connection? |
Better use the Process.Start to call the native to windows "ssh" program and pass args to it. The project is a mess of multithreading issues |
@effolkronium thanks for the tip, but my code needs to support Win Server 2012 and 2016 too where no windows native ssh exists (I would need to install PuTTY Plink or similar 3rd party) |
fork the code, fix the problems as described here and use it. that is my
recommendation and what i do for two years without any problem.
On 13 Jun 2022 Mon at 15:04 Agustin Berbegall ***@***.***> wrote:
@effolkronium <https://github.com/effolkronium> thanks for the tip, but
my code needs to support Win Server 2012 and 2016 too where no windows
native ssh exists (I would need to install PuTTY or similar 3rd party)
—
Reply to this email directly, view it on GitHub
<#355 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAWRXBUPZXYA3NR7BZ6CF2LVO4PUPANCNFSM4EH7XWNA>
.
You are receiving this because you were mentioned.Message ID: <sshnet/SSH.
***@***.***>
--
oguzhan
|
@oguzhantopcu Is this the update your fork contains or is there anything else to consider? |
Yeah, that one line code fixes that hang problem. My app makes thousands of sftp connections everyday without any problem. |
Then perhaps we could get that line into the official version? |
is there anyone that still faces this issue? i want to ditch my custom code and use the original one. maybe it is fixed in .net core side. @drieseng btw why we are not using as like here OctopusDeploy@7e8bf58 |
Yes its still issue for me, If I connect to remote client, and I lost connection, it hangs on |
.net core version? |
I am trying in on .NET Framework 4.7.2 project. I can easily test it in this order:
I fixed it by adding |
Hi,
My code is locking when attempting to call the Disconnect function.
This snippet is an example of what is locking:
I'm connecting to a ssh connection running on a QNAP NAS server, with the server version listed as "SSH-2.0-OpenSSH_7.6".
This is on OSX (High Sierra). I just tried in Windows, and the issue doesn't exist there.
Any help would be much appreciated.
Thanks.
The text was updated successfully, but these errors were encountered: