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
WriteAsync doesn't complete with ReadAsync running in background #1707
I have a simple program below that call ReadAsync in a concurrent task. On running the program, the connection succeeds and I see the initial preamble. But the program hangs on hitting WriteAsync, the await never completes. Am I doing something wrong? Or am I not supposed to use these async methods?
Same behavior on .NET 9 and .Net framework 4.8.1 with v2025.0.0
class Program
{
private async static Task Main(string[] args)
{
using var cts = new CancellationTokenSource();
using var client = new SshClient(args[0], 22, args[1], args[2]);
await client.ConnectAsync(cts.Token);
await using var stream = client.CreateShellStream("test", 80, 24, 80, 24, 1024);
var task = Task.Run(() => ReadStream(stream, cts.Token));
var command = Encoding.ASCII.GetBytes("ls\n");
// Continue after key press
Console.ReadKey(true)
await stream.WriteAsync(command, cts.Token);
await stream.FlushAsync(cts.Token);
// Continue after key press
Console.ReadKey(true)
cts.Cancel();
await task;
}
private static async Task ReadStream(ShellStream stream, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
try
{
var buffer = new byte[1024];
var readLength = await stream.ReadAsync(buffer, token);
Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, readLength));
}
catch (OperationCanceledException) { }
}
}
}
In your case, it means that the call to WriteAsync is probably waiting for the ReadAsync to complete, which it won't do because there is no data. I think it would work if you call the sync Read/Write
(*) the current implementation does not lend itself well to async, but since reads and writes on ShellStream do not depend on each other, perhaps at least Write could be overridden to help with this problem
@Rob-Hague Thank you for your reply. However my above program used to work correctly before. I see that it works as expected with v2023.0.1 and breaks on v2024.0.0 and higher. I see that ShellStream was reworked in v2024.0.0, so this appears to be a regression.
Ok fix is in #1711. I still see a slight issue which is that the cancellation token will not necessarily interrupt the ReadAsync (it didn't in 2023.0.1 either, but Read would often incorrectly return without having read anything). I suggest calling stream.Dispose() when (or instead of) cancelling the token, which will interrupt the Read safely.
Activity
Rob-Hague commentedon Oct 2, 2025
Read/WriteAsync are currently not overridden in ShellStream(*), so they fall back to the base Stream implementation, which serialises async operations (only allows one at a time): https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L496-L501
In your case, it means that the call to WriteAsync is probably waiting for the ReadAsync to complete, which it won't do because there is no data. I think it would work if you call the sync Read/Write
(*) the current implementation does not lend itself well to async, but since reads and writes on ShellStream do not depend on each other, perhaps at least Write could be overridden to help with this problem
DeepakParamkusam commentedon Oct 2, 2025
@Rob-Hague Thank you for your reply. However my above program used to work correctly before. I see that it works as expected with v2023.0.1 and breaks on v2024.0.0 and higher. I see that
ShellStreamwas reworked in v2024.0.0, so this appears to be a regression.Rob-Hague commentedon Oct 4, 2025
Ok fix is in #1711. I still see a slight issue which is that the cancellation token will not necessarily interrupt the ReadAsync (it didn't in 2023.0.1 either, but Read would often incorrectly return without having read anything). I suggest calling stream.Dispose() when (or instead of) cancelling the token, which will interrupt the Read safely.