Skip to content

Zip async correctness fix#129122

Open
alinpahontu2912 wants to merge 2 commits into
dotnet:mainfrom
alinpahontu2912:alin/zip-async-correctness-fix
Open

Zip async correctness fix#129122
alinpahontu2912 wants to merge 2 commits into
dotnet:mainfrom
alinpahontu2912:alin/zip-async-correctness-fix

Conversation

@alinpahontu2912
Copy link
Copy Markdown
Member

fix mix of sync/async reads when reading zip comment

alinpahontu2912 and others added 2 commits June 8, 2026 13:16
…path

ZipEndOfCentralDirectoryBlock.ReadBlockAsync called the synchronous Stream.ReadExactly when copying the archive comment after the EOCD header. This blocks the calling thread on the underlying stream and silently ignores the supplied CancellationToken. Switch to ReadExactlyAsync with ConfigureAwait(false), matching the rest of the async path.

Adds a regression test using CallTrackingStream to verify the async open path never invokes the synchronous Stream.Read or ReadByte when reading an archive with a non-empty comment.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes async ZIP reading correctness by ensuring the archive comment is read asynchronously (avoiding synchronous Stream.Read* calls) on the async code path, and adds a regression test to validate the behavior.

Changes:

  • Switch ZipEndOfCentralDirectoryBlock.ReadBlockAsync to use ReadExactlyAsync(..., cancellationToken) when reading the archive-level comment.
  • Add a unit test that asserts ZipArchive.CreateAsync(...) + ZipArchive.Comment does not invoke synchronous Stream.Read / Stream.ReadByte on the provided stream.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipBlocks.Async.cs Replaces a synchronous ReadExactly with await ReadExactlyAsync(..., cancellationToken) to keep the async path fully async.
src/libraries/System.IO.Compression/tests/ZipArchive/zip_ReadTests.cs Adds a regression test using CallTrackingStream to ensure async ZIP open + comment retrieval doesn’t call sync read APIs.

Comment on lines +942 to +945
ZipArchive readArchive = await ZipArchive.CreateAsync(tracker, ZipArchiveMode.Read, leaveOpen: true, entryNameEncoding: null);
Assert.Equal(ExpectedComment, readArchive.Comment);
await readArchive.DisposeAsync();

}

await using MemoryStream ms = new MemoryStream(zipBytes);
await using CallTrackingStream tracker = new CallTrackingStream(ms);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is something like AsyncOnlyStream that throws when any synchronous method is used that might be better fit for this kind of test.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants