You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PipeStream returns 0 instead of blocking when running a command with intermittent output #12
@lucastheisen I know this is older and I'm sure you've since moved on from this. But, I saw your StackOverflow question and provided an answer. I've replicated it here:
Another problem with PipeStream is that if you attempt to read more than the available bytes, it will block. For performance reasons, blocking should be the job of the consumer of the PipeStream. I use SSH.NET to execute SshCommand and asynchronously read the standard output/error. My workaround for the problems is to write to an intermediary MemoryStream and then use standard mechanisms like StreamReader. This is a more generalized answer to read from the PipeStream:
publicclassSshCommandStreamReader:IDisposable{privatereadonlyStreamstream;privatereadonlyMemoryStreamintermediateStream;privatereadonlyStreamReaderreader;publicSshCommandOutputReader(Streamstream){this.stream=stream;this.intermediateStream=newMemoryStream();this.reader=newStreamReader(intermediateStream,Encoding.UTF8);}privateintFlushToIntermediateStream(){varlength=stream.Length;if(length==0){return0;}// IMPORTANT: Do NOT read with a count higher than the stream length (which is typical of reading// from streams). The streams for SshCommand are implemented by PipeStream (an internal class to// SSH.NET). Reading more than the current length causes it to *block* until data is available.// If the stream is flushed when reading, it does not block. It is not reliable to flush and then// read because there is a possible race condition where a write might occur between flushing and// reading (writing resets the flag that it was flushed). The only reliable solution to prevent// blocking when reading is to always read the current length rather than an arbitrary buffer size.varintermediateOutputBuffer=newbyte[length];varbytesRead=stream.Read(intermediateOutputBuffer,0,intermediateOutputBuffer.Length);intermediateStream.Write(intermediateOutputBuffer,0,bytesRead);returnbytesRead;}publicstringRead(){varbytesFlushed=FlushToIntermediateStream();// Allow reading the newly flushed bytes.intermediateStream.Position-=bytesFlushed;// Minor optimization since this may be called in a tight loop.if(intermediateStream.Position==intermediateStream.Length){returnnull;}else{varresult=reader.ReadToEnd();returnresult;}}publicvoidDispose(){reader.Dispose();intermediateStream.Dispose();}}
And then use it:
using(varcommand=client.CreateCommand("your command text")){varcmdAsyncResult=command.BeginExecute();using(varstandardOutputReader=newSshCommandStreamReader(command.OutputStream)){while(!cmdAsyncResult.IsCompleted){varresult=standardOutputReader.Read();if(!String.IsNullOrEmpty(result)){Console.Write(result);}// Or what ever mechanism you'd like to use to prevent CPU thrashing.Thread.Sleep(1);}// This must be done *after* the loop and *before* EndExecute() so that any extra output// is captured (e.g. the loop never ran because the command was so fast).varresultFinal=standardOutputReader.Read();if(!String.IsNullOrEmpty(resultFinal)){Console.Write(resultFinal);}}command.EndExecute(cmdAsyncResult);}
You should be able to modify this sample to read from standard error (via the ExtendedOutputStream) and also change it to read line-by-line which is specific to your application.
Activity
lucastheisen commentedon May 6, 2016
I kinda resolved this with this patch:
Though I imagine you may have a better approach in mind. If you would like, I could create a pull request with this patch... Just let me know.
PipeStream returns 0 instead of blocking when running a command with…
TheCloudlessSky commentedon Oct 18, 2017
@lucastheisen I know this is older and I'm sure you've since moved on from this. But, I saw your StackOverflow question and provided an answer. I've replicated it here:
Another problem with
PipeStreamis that if you attempt to read more than the available bytes, it will block. For performance reasons, blocking should be the job of the consumer of thePipeStream. I use SSH.NET to executeSshCommandand asynchronously read the standard output/error. My workaround for the problems is to write to an intermediaryMemoryStreamand then use standard mechanisms likeStreamReader. This is a more generalized answer to read from thePipeStream:And then use it:
You should be able to modify this sample to read from standard error (via the
ExtendedOutputStream) and also change it to read line-by-line which is specific to your application.