While Unit Testing my solution using SSH.NET I run into some problem.
Sometimes I get error from SSH.NET during test (it is weird, because it happen once 5-6 runs)
Exception: System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.Queue`1.ToArray()
at Renci.SshNet.ShellStream.Expect(Regex regex, TimeSpan timeout)
my code looks like:
string buffer = string.Empty;
using (ShellStream shellStream = _sshClient.CreateShellStream("xterm0", 80, 24, 800, 600, 1024))
{
try
{
shellStream.ErrorOccurred += ErrorHandling;
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
shellStream.WriteLine(cmd);
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
}
finally
{
if (shellStream != null)
{
shellStream.ErrorOccurred -= ErrorHandling;
}
}
}
I looked into source code, it look like problem is in ShellStram Expect Method (line 273)
it is:
if (this._incoming.Count > 0)
text = this._encoding.GetString(this._incoming.ToArray(), 0, this._incoming.Count);
it should be:
if (this._incoming.Count > 0)
{
var array = this._incoming.ToArray();
text = this._encoding.GetString(array, 0, array.Length);
}
Comments: ** Comment from web user: blakeja **
Sometimes I get error from SSH.NET during test (it is weird, because it happen once 5-6 runs)
Exception: System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.Queue`1.ToArray()
at Renci.SshNet.ShellStream.Expect(Regex regex, TimeSpan timeout)
my code looks like:
string buffer = string.Empty;
using (ShellStream shellStream = _sshClient.CreateShellStream("xterm0", 80, 24, 800, 600, 1024))
{
try
{
shellStream.ErrorOccurred += ErrorHandling;
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
shellStream.WriteLine(cmd);
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
}
finally
{
if (shellStream != null)
{
shellStream.ErrorOccurred -= ErrorHandling;
}
}
}
I looked into source code, it look like problem is in ShellStram Expect Method (line 273)
it is:
if (this._incoming.Count > 0)
text = this._encoding.GetString(this._incoming.ToArray(), 0, this._incoming.Count);
it should be:
if (this._incoming.Count > 0)
{
var array = this._incoming.ToArray();
text = this._encoding.GetString(array, 0, array.Length);
}
Comments: ** Comment from web user: blakeja **
I went ahead and tried the code that piomark pasted and still ran into the same error. A bit of googling seems to indicate this is a locking issue with the queue, like something else is still accessing the _incoming queue while trying to convert it to an array. I'm trying to wrap a lock (this._incoming) around the foreach in the Channel_DataReceived method to see if that maybe corrects this? So far my initial test look good, but due to the sporadic nature of the error, won't know for sure without quite a bit more testing.