Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 30, 2026

  • Understand the issue: Debug.Assert(CanSeek) in InterleavedZipPackagePartStream.Length fails in debug builds
  • Research behavior: In release builds, Length returned correct values because ZipWrappingStream.Length gets length from ZipArchiveEntry.Length even when CanSeek is false
  • Fix: Remove the incorrect Debug.Assert(CanSeek) entirely - the underlying streams support Length even when not seekable
  • Add test InterleavedZipPackagePartStream_Length_ReturnsCorrectValueWhenCanSeekIsFalse with exact length verification
  • Run tests to validate changes
Original prompt

This section details on the original issue you should resolve

<issue_title>Reachable Debug.Assert in InterleavedZipPackagePartStream.Length</issue_title>
<issue_description>In System.IO.Packaging, InterleavedZipPackagePartStream.Length asserts that CanSeek is true here:

However this is not always true, and this assert is reachable from public APIs.

Using the existing PartPieceTests test fixture and data, the following unit test will hit the Debug.Assert:

[Fact]
public void InterleavedZipPackagePartStream_Length_Assert()
{
    using MemoryStream package = new(_partPieceSampleZipPackage);

    using Package zipPackage = Package.Open(package, FileMode.Open, FileAccess.Read);
    PackagePart partEntry = zipPackage.GetPart(new Uri("/ReadablePartPieceEntry.bin", UriKind.Relative));
    using Stream stream = partEntry.GetStream(FileMode.Open);
    Assert.NotEqual(0, stream.Length);
}

The assertion:

Stack Trace:
     at Microsoft.VisualStudio.TestPlatform.TestHost.TestHostTraceListener.GetException(String message)
   at Microsoft.VisualStudio.TestPlatform.TestHost.TestHostTraceListener.Fail(String message, String detailMessage)
   at System.Diagnostics.TraceInternal.Fail(String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceInternal.cs:line 261
   at System.Diagnostics.TraceInternal.TraceProvider.Fail(String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceInternal.cs:line 17
   at System.Diagnostics.Debug.Fail(String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs:line 135
   at System.Diagnostics.Debug.Assert(Boolean condition, String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs:line 99
   at System.Diagnostics.Debug.Assert(Boolean condition, String message) in /Users/vcsjones/Projects/runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs:line 88
   at System.IO.Packaging.InterleavedZipPackagePartStream.get_Length() in /Users/vcsjones/Projects/runtime/src/libraries/System.IO.Packaging/src/System/IO/Packaging/InterleavedZipPackagePartStream.cs:line 480
   at System.IO.Packaging.Tests.PartPieceTests.InterleavedZipPackagePartStream_Length_Assert() in /Users/vcsjones/Projects/runtime/src/libraries/System.IO.Packaging/tests/PartPieceTests.cs:line 609

</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…g NotSupportedException

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix reachable Debug.Assert in InterleavedZipPackagePartStream.Length Fix reachable Debug.Assert in InterleavedZipPackagePartStream.Length Jan 30, 2026
Copilot AI requested a review from stephentoub January 30, 2026 22:11
Copilot AI and others added 2 commits January 31, 2026 04:03
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub stephentoub marked this pull request as ready for review January 31, 2026 04:04
Copilot AI review requested due to automatic review settings January 31, 2026 04:04
Copilot AI requested a review from stephentoub January 31, 2026 04:04
@dotnet-policy-service
Copy link
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
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

This PR fixes a reachable Debug.Assert in InterleavedZipPackagePartStream.Length that fails in debug builds when accessing the Length property of a package part stream opened with read-only access.

Changes:

  • Removed incorrect Debug.Assert(CanSeek) from the Length property getter in InterleavedZipPackagePartStream
  • Added comprehensive test that verifies Length returns correct value even when CanSeek is false

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.IO.Packaging/src/System/IO/Packaging/InterleavedZipPackagePartStream.cs Removed the incorrect assertion that assumed CanSeek must be true for Length to work
src/libraries/System.IO.Packaging/tests/PartPieceTests.cs Added test that verifies Length works correctly when CanSeek is false, validating the exact byte count

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.

Reachable Debug.Assert in InterleavedZipPackagePartStream.Length

2 participants