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

SSH exception after client connect using .NET 3.5 version #113

Closed
haimiko opened this issue Nov 11, 2016 · 13 comments
Closed

SSH exception after client connect using .NET 3.5 version #113

haimiko opened this issue Nov 11, 2016 · 13 comments
Assignees

Comments

@haimiko
Copy link

haimiko commented Nov 11, 2016

I'm able to successfully work with the .NET 4.0 version of SSH.NET but am experiencing issues with a when both the ssh.net and main project are .NET 3.5 version right after the client connect. I get a "value cannot be null" exception. This does not happen when the main project is .net 4.0 and SSH.NET is .NET 3.5.

I've tried both password and keyfile authentication and both exhibit the same issue. See attached code for the way I'm using Renci.SSH. sshclient.cs

System.ArgumentNullException was caught
Message=Value cannot be null.
Parameter name: All lists are either null or empty.
Source=Renci.SshNet
ParamName=All lists are either null or empty.
StackTrace:
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout) in Session.cs:line 812
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle) in Session.cs:line 783
at Renci.SshNet.Session.Connect() in Session.cs:line 643
at Renci.SshNet.BaseClient.Connect() BaseClient.cs:line 214
at SSH.clientSSH(ConnectionInfo connectionInfo, String Computer, String command) in SSH.cs:line 225
InnerException: null

More specifically, line 643 in Session.cs (WaitOnHandle(_keyExcahangeCompletedWaitHandle) is what's failing.

@sleeveroller
Copy link

Just to confirm I'm getting exactly the same issue with the 3.5 library.

@petarkekez
Copy link

Same problem. Any solution?

@sdahan-qualitrol
Copy link

Has there been any work done on this? Do we know why is the session hanging on that WaitOnHandle()? Where is that event set in 4.0?

@drieseng
Copy link
Member

drieseng commented Jan 18, 2017

I cannot seem to reproduce this problem, but I'm pretty sure the exception originally comes from Session.MessageListener(). It appears as if the readSockets gets cleared, even though the socket is still considered connected. When we subsequently go through the loop again, the Socket.Select(...) call will fail because all lists are either empty or null.

When an exception occurs in Session.MessageListener(), it is "recorded" and rethrown on the next call to Session.WaitHandle(...). By rethrowing the exception later, the original stacktrace is lost.
Try subscribing to the ErrorOccurred event and log the exception.

Can you reproduce the problem using nothing but a simple connect?
For example:

using (var client = new SshClient(new ConnectionInfo("host", "user", new PasswordAuthenticationMethod("user", "pwd"))))
{
    client.ErrorOccurred += (sender, e) => Console.WriteLine(e.Exception);
    client.Connect();
}

Can someone provide the absolute minimum amount of code to reproduce the problem?
Also, can you reproduce the issue with both version 2016.0.0 and 2016.1.0-beta1?

Thanks!

@drieseng drieseng self-assigned this Jan 18, 2017
@haimiko
Copy link
Author

haimiko commented Jan 18, 2017 via email

@sdahan-qualitrol
Copy link

sdahan-qualitrol commented Jan 18, 2017 via email

@drieseng drieseng added this to the 2016.1.0 milestone Jan 19, 2017
@drieseng
Copy link
Member

Was able to reproduce the issue, and found the cause

Apparently there's no fix available (nor expected), so I'll have to introduce a workaround.
I don't have much time right now, but expect a fix this weekend.

@Beast09
Copy link

Beast09 commented Jan 19, 2017

I have some insight into this. In fact I've been fighting it for the day. The issue is that in Session.cs in the method MessageListener. Inside the while loop there's a line of code

Socket.Select(readsockets, null, null, -1);

For some oddball reason after a few iterations of the readsockets array runs empty (I'm guessing being edited internally by .Select). In any case on a subsequent run through it will toss an Argument Null Exception.

I created this problem by compiling in Visual Studio 2012 for .NET 3.5 and then executing the .dll from a CLR/CLI C++ application in Visual Studio 2008. C++ CLR/CLI apps in Visual Studio 2012 .NET 4 are not affected.

This is the fix I'm playing with. Replace the line in Session.cs in method MessageListener
Socket.Select(readSockets, null, null, -1);
with this
_socket.Poll(-1, SelectMode.SelectRead);

Socket.Select and .Poll do roughly the same thing but .Poll is only focusing on single socket whereas .Select can focus on multiple.

Beast09 added a commit to Beast09/SSH.NET that referenced this issue Jan 19, 2017
…ption after client connect sshnet#113

Addressing the outstanding issue .NET 3.5 compiled version of SSH exception after client connect sshnet#113 opened by haimiko. 

Replaced Socket.Select(readSockets, null, null, -1) with _socket.Poll(-1, SelectMode.SelectRead);

When executing the .Net 3.5 code in a Visual Studio 2008 C++ CLR session Socket.Select would return an ArgumentNullException. Upon investigation while reading in data the readSockets list would go from a count of 1 to a count of 0 upon execution of Socket.Select. I'm thinking that Socket.Select was editing the contents of the array.

Looking at the comments it appears that the use of Socket.Select over .Poll is preferred so please feel free to disregard these changes.
@sdahan-qualitrol
Copy link

Thank you Best09! That fixes it for me too.

drieseng added a commit that referenced this issue Jan 22, 2017
… -1 is specified as timeout.

More information is available here:
https://support.microsoft.com/kb/2615314

Fixes issue #113.
@drieseng
Copy link
Member

Fixed by commit 710aa37 in the develop branch.

@drieseng
Copy link
Member

Note that I also updated the appveyor (CI) config to run our unit tests against both .NET 3.5 and .NET 4.0.

@Kantoboy2017
Copy link

Under Session.cs->MessageListener

I put
Thread.Sleep(500);
Socket.Select(readSockets, null, null, -1);
^^^^^^^^^^^^^^^^^^^^^^^^^
Above this
And it works
I believe it's something to do with .net4<tcp sockets handling
Thus delaying the read did the trick

@drieseng drieseng changed the title .NET 3.5 compiled version of SSH exception after client connect [.NET 3.5] SSH exception after client connect Feb 20, 2017
@drieseng drieseng changed the title [.NET 3.5] SSH exception after client connect SSH exception after client connect using .NET 3.5 compiled version Feb 20, 2017
@drieseng drieseng changed the title SSH exception after client connect using .NET 3.5 compiled version SSH exception after client connect using .NET 3.5 version Feb 20, 2017
@rrodriguez1975
Copy link

rrodriguez1975 commented Apr 11, 2017

Please, could you upload the binaries for .NET Framework 3.5? I have the same problem. I am using Visual Studio 2008 and .NET Framework 3.5. I highly appreciate you. Last binaries is from 14 Dec 2016 and the fix was applied before this date. So where is the binary for .NET Framework 3.5 with this fix applied? could you provide me?

Also, could you provide the solution for Visual Studio 2008? I have downloaded the zip package from developer branch and then I have open the source project "Renci.SshNet.NET35" but when I build it I get errors.

it's urgent. thx.

@drieseng drieseng modified the milestones: 2016-1.0-beta2, 2016.1.0 Jul 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants