fix(gzip): bound decompression memory - #581
Draft
CyMule wants to merge 8 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BytesIO.UploadFileso FastAPI closes the decompressed temporary file after the response.Why
ungz_filecurrently reads the complete expanded payload into abytesobject before wrapping it inBytesIO. Peak Python memory therefore grows with the uncompressed file size.A raw
SpooledTemporaryFileis not sufficient by itself because existing downstream code recognizes that concrete type and copies its complete contents back into memory. Incremental decompression plus a normal file proxy avoids both document-sized allocations.This does not introduce a file-size limit or change the partition API contract.
Impact
Local end-to-end benchmarks with valid documents produced byte-identical responses:
In decompression-and-reread benchmarks, request-attributable RSS fell from 108.1 MiB to 5.3 MiB for the DOCX and from 133.6 MiB to 4.8 MiB for the PDF. That isolated stage was 4.0% and 8.3% slower respectively, but decompression was a small portion of the complete request.
Large expanded outputs use temporary disk space rather than equivalent heap memory.
Risk
Disk-backed outputs add temporary-file I/O. The 1 MiB copy buffer keeps this overhead small while maintaining bounded heap use.
Temporary-disk capacity remains the external bound; this change does not impose a maximum expanded size.
Validation
uv run pytest test_general/api/test_gzip.py -q— 3 passed, 28 xfailed.uv run ruff check --select I prepline_general/api/general.py test_general/api/test_gzip.py— passed.