diff --git a/src/Renci.SshNet/Common/PipeStream.cs b/src/Renci.SshNet/Common/PipeStream.cs
index bfb7787c1..57e3fc6e1 100644
--- a/src/Renci.SshNet/Common/PipeStream.cs
+++ b/src/Renci.SshNet/Common/PipeStream.cs
@@ -213,10 +213,22 @@ public override int Read(byte[] buffer, int offset, int count)
/// True if data available; otherwisefalse.
private bool ReadAvailable(int count)
{
+ if (Length == 0 && !IsEndOfStream())
+ {
+ return false;
+ }
+
return (Length >= count || _isFlushed) &&
(Length >= (count + 1) || !BlockLastReadBuffer);
}
+ public Func EndOfStream { private get; set; }
+
+ private bool IsEndOfStream()
+ {
+ return (EndOfStream == null) ? Length == 0 : EndOfStream();
+ }
+
///
///When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
///
diff --git a/src/Renci.SshNet/SshCommand.cs b/src/Renci.SshNet/SshCommand.cs
index 391bec8b0..86be8866d 100644
--- a/src/Renci.SshNet/SshCommand.cs
+++ b/src/Renci.SshNet/SshCommand.cs
@@ -246,7 +246,9 @@ public IAsyncResult BeginExecute(AsyncCallback callback, object state)
// Initialize output streams
OutputStream = new PipeStream();
+ ((PipeStream)OutputStream).EndOfStream = () => { return _asyncResult.IsCompleted; };
ExtendedOutputStream = new PipeStream();
+ ((PipeStream)ExtendedOutputStream).EndOfStream = () => { return _asyncResult.IsCompleted; };
_result = null;
_error = null;