Skip to content

[Python] Read all compressed file data for unbounded reads - #41622

Open
JustinBinber wants to merge 2 commits into
apache:masterfrom
JustinBinber:codex/fix-compressed-unbounded-read
Open

JustinBinber wants to merge 2 commits into
apache:masterfrom
JustinBinber:codex/fix-compressed-unbounded-read

Conversation

@JustinBinber

@JustinBinber JustinBinber commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #41621.

Compressed files currently return at most 16 MiB from a default read(). fileio.ReadableFile.read() and read_utf8() each perform one such read, so larger content can be silently truncated. Explicit read(None) raises and read(-1) can return empty data before reading the source.

Change the default to an unlimited read and drain the existing decompression buffer to EOF for omitted, None or negative sizes. Explicit nonnegative sizes keep their bounded behavior. Clamp negative absolute seek offsets to zero before skipping, so seek never interprets a negative distance as an unlimited read. No codec-specific logic or dependencies change.

Regression tests cover all five codecs, partial reads, empty streams, EOF, rewind and concatenated members. A real ReadMatches pipeline checks both public ReadableFile readers with a 16 MiB gzip member followed by a final row. The old code returns 16,777,216 bytes instead of 16,777,225.

Validation on Windows / Python 3.11.7:

  • New regression nodes fail against unchanged production code, then pass with the fix.
  • Complete filesystem_test.py and localfilesystem_test.py, plus fileio_test.py::ReadTest, WriteFilesTest and textio_test.py::TextSinkTest: 138 passed, 1 existing skip and 55 subtests passed on FnApiRunner, excluding one TestStream test unsupported by that runner. That test passes separately on BundleBasedDirectRunner (139 passed in total).
  • YAPF 0.43.0, Ruff 0.17.6, Pyrefly 1.5.0 (0 errors) and git diff --check pass.

The source checkout used generated protobuf bindings from the released 2.78.0 wheel; a fresh SDK/protobuf build, cloud execution and the full runner matrix were not run. Exploratory tests also reproduced existing zstd/lzma concatenated-frame chunk-boundary errors on the unchanged base with bounded reads; those are outside this change.

Unlimited reads use memory proportional to the remaining decompressed data. Callers needing bounded reads can continue to pass an explicit size. This does not address the separate CSV restriction-size issue in #41471.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

This PR fixes a silent data truncation bug in CompressedFile.read() where calling read() without arguments previously defaulted to returning at most DEFAULT_READ_BUFFER_SIZE (16 MiB) of decompressed data, and calling read(None) or read(-1) raised a TypeError or returned incomplete data. The implementation cleanly updates CompressedFile.read() and _fetch_to_internal_buffer() to follow standard Python io semantics for unbounded reads, clamps negative seek offsets, and includes comprehensive unit and pipeline tests.

🔍 General Feedback

  • The fix accurately aligns CompressedFile.read(num_bytes=None) with standard Python file-like object conventions (handling omitted, None, and negative sizes as read-to-EOF while preserving bounded behavior for non-negative sizes).
  • Clamping absolute_offset to >= 0 in seek() is a good defensive measure and aligns with the method's documented out-of-bounds behavior.
  • Test coverage is thorough across all supported compression formats, edge cases (empty files, offsets, rewinds, concatenated members), and fileio.ReadableFile integration.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions github-actions Bot added python io core size/m M: 50-249 lines changed labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.
Backup reviewer: jrmccluskey (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@JustinBinber

Copy link
Copy Markdown
Contributor Author

Synced the branch with current master (5508f743ec) and resolved the CHANGES.md conflict, keeping both bugfix entries. The three Python files are unchanged from the original PR commit; the diff against master remains four files, 97 additions and 6 deletions.

Rechecked the merged tree locally: 138 tests and 55 subtests passed with one existing skip on FnApiRunner, and the separate TestStream test passed on BundleBasedDirectRunner (139 tests passed in total). YAPF 0.43.0, the current Ruff 0.17.6 / Pyrefly 1.5.0 pins, and whitespace checks pass.

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.

[Bug]: Compressed whole-file reads silently truncate content at 16 MiB

1 participant