Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeserializeAsync fully consumes FileStreams #339

Open
bartimaeusnek opened this issue Oct 14, 2024 · 0 comments
Open

DeserializeAsync fully consumes FileStreams #339

bartimaeusnek opened this issue Oct 14, 2024 · 0 comments

Comments

@bartimaeusnek
Copy link

When using MemoryPackSerializer.DeserializeAsync with a FileStream, the expectation is that after deserializing an object from the stream, the stream's position should move forward only by the number of bytes required to deserialize that specific object. However, after the first call to DeserializeAsync, the stream is fully consumed, making it impossible to deserialize subsequent objects from the same stream without resetting the position.

Other types of Streams might also be affected, i havent tested that tho.

Example Code:

    public async Task MemoryPackBug()
    {
        await using var fs = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite);
        await MemoryPackSerializer.SerializeAsync(fs, "First String");
        var positionOne = fs.Position;
        await MemoryPackSerializer.SerializeAsync(fs, "Second String");
        var positionTwo = fs.Position;
        
        fs.Seek(0, SeekOrigin.Begin);
        
        _ = await MemoryPackSerializer.DeserializeAsync<string>(fs);
        if(fs.Position != positionOne) {
            throw new Exception();
        }
        _ = await MemoryPackSerializer.DeserializeAsync<string>(fs);
        if(fs.Position != positionTwo) {
            throw new Exception();
        }
    }

With the current version of MemoryPack (1.21.3) on .NET8 this throws at the if(fs.Position != positionOne)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant