Skip to content

Buffering causes async unpacking to fail #321

@b-r-o-c-k

Description

@b-r-o-c-k

In the following method, each time UnpackAsync() is called it will create a new BufferedStream to wrap the existing stream. This causes data in the stream to be discarded because reading from a BufferedStream can consume more data from the underlying stream than necessary. The documentation does state "it is preferred that you should wrap with System.IO.BufferedStream yourself", but I do not believe this is the intended behavior.

public static async Task TestAsyncBuffering()
{
    var context = new SerializationContext();
    var serializer = context.GetSerializer<string>();
    using (var stream = new MyMemoryStream()) // could also be a NetworkStream or PipeStream
    {
        await serializer.PackAsync(stream, "hello");
        await serializer.PackAsync(stream, "world");
        stream.Position = 0;
        await serializer.UnpackAsync(stream);
        await serializer.UnpackAsync(stream); // throws MsgPack.InvalidMessagePackStreamException
    }
}
class MyMemoryStream : MemoryStream
{
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugDetected as bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions