Skip to content

Fix flat GZip via ReaderFactory: SeekableSharpCompressStream.StopRecording() must seek back to anchor - #1392

Closed
adamhathcock with Copilot wants to merge 2 commits into
masterfrom
copilot/flat-gzip-fix-stream-rewind
Closed

Fix flat GZip via ReaderFactory: SeekableSharpCompressStream.StopRecording() must seek back to anchor#1392
adamhathcock with Copilot wants to merge 2 commits into
masterfrom
copilot/flat-gzip-fix-stream-rewind

Conversation

Copilot AI commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

ReaderFactory.OpenReader on a flat .gz file (non-tar) produced Key=null and extracted 0 bytes because SeekableSharpCompressStream.StopRecording() cleared _recordedPosition without seeking back to it.

After GZipFactory.TryOpenReader runs the tar probe (decompressing the entire GZip stream to check for a tar header), the underlying FileStream is left at an advanced position. StopRecording() must rewind before clearing the anchor — the non-seekable SharpCompressStream already does this by resetting _logicalPosition, but the seekable override was missing the seek.

Changes

  • SeekableSharpCompressStream.StopRecording() — seek to _recordedPosition before clearing it, matching the non-seekable implementation's behavior of rewinding to the recording anchor on stop.
// Before (bug): position stays wherever the tar probe left it
public override void StopRecording() => _recordedPosition = null;

// After (fix): rewind first, matching non-seekable SharpCompressStream semantics
public override void StopRecording()
{
    if (_recordedPosition.HasValue)
        _stream.Seek(_recordedPosition.Value, SeekOrigin.Begin);
    _recordedPosition = null;
}
  • GZipReaderTests.GZip_ReaderFactory_FlatGZip — extended to verify the extracted bytes, not just that MoveToNextEntry() returns true (the prior assertion missed the actual regression).
  • GZipReaderAsyncTests.GZip_ReaderFactory_FlatGZip_Async — async equivalent of the above.

… position

When SeekableSharpCompressStream.StopRecording() was called (e.g. after the tar
probe in GZipFactory.TryOpenReader), it cleared _recordedPosition without seeking
back to it. This left the underlying FileStream at the advanced position reached
during the probe, so the subsequent GZipReader would start reading from the
wrong offset, producing Key=null and 0 extracted bytes.

Fix: seek back to _recordedPosition before clearing it, matching the behavior of
the non-seekable SharpCompressStream.StopRecording() which rewinds _logicalPosition.

Also enhance the existing GZip_ReaderFactory_FlatGZip test to actually verify the
extracted content (not just MoveToNextEntry returns true), and add an async variant.

Closes #1391
Copilot AI changed the title [WIP] Fix flat GZip via ReaderFactory stream rewind issue Fix flat GZip via ReaderFactory: SeekableSharpCompressStream.StopRecording() must seek back to anchor Jul 31, 2026
Copilot AI requested a review from adamhathcock July 31, 2026 07:57
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

Successfully merging this pull request may close these issues.

Flat GZip via ReaderFactory broken: stream seems not to be rewound after tar probe in GZipFactory.TryOpenReader

2 participants