Skip to content

WriteAsync doesn't complete with ReadAsync running in background #1707

Closed

Description

@DeepakParamkusam

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) { }
        }
    }
}

Activity

  1. Rob-Hague commented on Oct 2, 2025

    @Rob-Hague
    Collaborator

    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

  2. DeepakParamkusam commented on Oct 2, 2025

    @DeepakParamkusam
    Author

    @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.

  3. Rob-Hague commented on Oct 4, 2025

    @Rob-Hague
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions