Skip to content

Fix range requests returning excess data in file_stream()#3150

Open
ChihweiLHBird wants to merge 2 commits into
mainfrom
zhiwei/fix-file-stream-range-overread
Open

Fix range requests returning excess data in file_stream()#3150
ChihweiLHBird wants to merge 2 commits into
mainfrom
zhiwei/fix-file-stream-range-overread

Conversation

@ChihweiLHBird

Copy link
Copy Markdown
Member

Summary

  • Fix file_stream() sending more bytes than requested for range requests when the range size is not aligned to chunk_size
  • The streaming loop used _range.size (a constant) instead of to_send (which decreases each iteration), causing over-reads on subsequent chunks
  • Add regression test verifying correct body length for multi-chunk range requests
  • Add get_file_size() test helper using stat() instead of reading entire file contents

Fixes #2507

Test plan

  • Existing test_file_stream_response_range tests pass
  • New test_file_stream_response_range_correct_length test verifies a 100-byte range with 32-byte chunks returns exactly 100 bytes
  • Static content range tests pass

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>

Copilot AI 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.

Pull request overview

Fixes an over-read bug in Sanic’s file_stream() range-response streaming loop that could send more bytes than requested when the range length is not aligned with chunk_size, and adds a regression test to lock in the correct behavior.

Changes:

  • Correct range streaming to read min(to_send, chunk_size) per iteration (instead of a constant range size).
  • Add a regression test asserting a multi-chunk range response returns the exact requested byte length.
  • Add a test helper to compute file sizes via stat() rather than reading whole files.

Reviewed changes

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

File Description
sanic/response/convenience.py Fix range streaming loop to prevent sending excess bytes on multi-chunk range reads.
tests/test_response.py Add regression test for correct range body length and introduce get_file_size() helper used by range tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_response.py
@codecov

codecov Bot commented Apr 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.731%. Comparing base (785d77f) to head (2262ed1).

Additional details and impacted files
@@              Coverage Diff              @@
##              main     #3150       +/-   ##
=============================================
- Coverage   87.793%   87.731%   -0.062%     
=============================================
  Files          105       105               
  Lines         8143      8143               
  Branches      1290      1290               
=============================================
- Hits          7149      7144        -5     
- Misses         687       692        +5     
  Partials       307       307               

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
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.

Range requests get wrong data when using file_stream()

2 participants