Skip to content

ShellStream.Read not blocking when no data available #63

Closed
@oblaise

Description

@oblaise

ShellStream.Read returns immediately with 0 bytes read when no data is available instead of blocking the call.
Consequently the other functions implemented in the parent class Stream that implement the asynchronous patterns based on the Read function (BeginRead/EndRead, ReadAsync) return also immediately with a 0 byte read instead of being suspended until data is available.
This prevent using correct asynchronous pattern with these functions.

The following modified function waiting on _dataReceived when no data is available seems to work:

public override int Read(byte[] buffer, int offset, int count)
{

    var i = 0;
    while (true)
    {
        lock (_incoming)
        {
            for (; i < count && _incoming.Count > 0; i++)
            {
                buffer[offset + i] = _incoming.Dequeue();
            }
        }

        if (i != 0) 
            return i;

        _dataReceived.WaitOne();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions