Repository navigation
Description
using (var command = client.CreateCommand(CommandText))
{
command.CommandTimeout = TimeSpan.FromMinutes(2);
command.Execute();
}
I have problems with this code. Sometimes a thread with this part of the code may hang. I began to research this problem and found that the problem happens when I had a bad connection with the host (about 60-70 pocket loss).
Deep in the library code, I found this method with infinite wait time:
private static int TrySocketRead(Socket socket, byte[] buffer, int offset, int length)
{
return SocketAbstraction.Read(socket, buffer, offset, length, InfiniteTimeSpan);
}
And inside of this method in SocketAbstraction.cs
public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeSpan timeout)
{
var totalBytesRead = 0;
var totalBytesToRead = size;
socket.ReceiveTimeout = (int) timeout.TotalMilliseconds;
do
{
try
{
var bytesRead = socket.Receive(buffer, offset + totalBytesRead, totalBytesToRead - totalBytesRead, SocketFlags.None);
if (bytesRead == 0)
return 0;
totalBytesRead += bytesRead;
}
We will have socket.Recive() call with infinity timeout.
Therefore, in case of a bad connection, we will hang in the Receive method, and in another thread, we will call Dispose of SshCommand and hang when _socketReadLock is locked because this lock object is used when we call TrySocketRead in Sesseion.cs
private bool IsSocketConnected()
{
lock (_socketDisposeLock)
{
#if FEATURE_SOCKET_POLL
if (!_socket.IsConnected())
{
return false;
}
lock (_socketReadLock)
{
var connectionClosedOrDataAvailable = _socket.Poll(0, SelectMode.SelectRead);
return !(connectionClosedOrDataAvailable && _socket.Available == 0);
}
#else
return _socket.IsConnected();
#endif // FEATURE_SOCKET_POLL
}
}
I found 2 workarounds for this case:
- We can set a timeout for this method from Session.cs
private static int TrySocketRead(Socket socket, byte[] buffer, int offset, int length)
{
return SocketAbstraction.Read(socket, buffer, offset, length, InfiniteTimeSpan);
}
- We can disable FEATURE_SOCKET_POLL
What do you think about this?
Can you explain why FEATURE_SOCKET_POLL is necessary or what is bad if disable FEATURE_SOCKET_POLL?
Activity
jkillingsworth commentedon Dec 18, 2023
I am running into a similar problem. See #1275. I like the idea of setting the timeout to a finite value.
Rob-Hague commentedon Dec 28, 2023
Fixed by #1280
WojciechNagorski commentedon Dec 29, 2023
The 2023.0.1 version has been released to Nuget: https://www.nuget.org/packages/SSH.NET/2023.0.1