Description
Issue:
Race condition in ShellStream occurs when underlying connection abruptly disconnects. The disconnect causes the stream to dispose. The Expect(Regex, TimeSpan) method utilizes an AutoResetEvent used to synchronize when new data is received, but the AutoResetEvent is disposed of and set to null.
Fix:
Do not dispose of an object that could be in use by another thread. In this particular context, the Producer (underlying connection) has abruptly finished, so the ShellStream would need have a state that reflects this. Any methods that utilize the existing (and only remaining) data, should not wait for more data to arrive and either return null (such as Expect) immediately, or throw SshConnectionException. The Consumer (user of the API) should be the one that disposes.