aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

Fix infinite `iter_chunks()` on empty response

Open agners opened this issue 5 months ago • 3 comments

What do these changes do?

When aiohttp returns an empty reader, the first time the iter_chunks() iterator stops after the first read as expected. However, on the second empty reader return, iter_chunks() loops forever.

This is due to state sharing caused by the global EmptyStreamReader instance. It has been noticed during review in #7645, but so far hasn't been addressed.

This PR attempts to address the issue by returning a new instance.

Are there changes in behavior for the user?

The change makes it safe to read content of empty responses using iter_chunks().

Is it a substantial burden for the maintainers to support this?

Related issue number

Checklist

  • [ ] I think the code is well written
  • [ ] Unit tests for the changes exist
  • [ ] Documentation reflects the changes
  • [ ] If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • [ ] Add a new news fragment into the CHANGES/ folder
    • name it <issue_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an improper undesired behavior that got corrected to match pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking changes in behavior.
      • .breaking: When something public is removed in a breaking way. Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build process.
      • .packaging: Notes for downstreams about unobvious side effects and tooling. Changes in the test invocation considerations and runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g. Running tests, building the docs, setting up the development environment.
      • .misc: Changes that are hard to assign to any of the above categories.
    • Make sure to use full sentences with correct case and punctuation, for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.
      

      Use the past tense or the present tense a non-imperative mood, referring to what's changed compared to the last released version of this project.

agners avatar Aug 07 '25 09:08 agners

This came up in Home Assistant Core PR https://github.com/home-assistant/core/pull/147899. The PR now avoids reading empty responses as a work around. /cc @bdraco

agners avatar Aug 07 '25 09:08 agners

Codecov Report

:x: Patch coverage is 80.64516% with 6 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 98.74%. Comparing base (48082a7) to head (c08dce1). :warning: Report is 73 commits behind head on master. :white_check_mark: All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tests/test_streams.py 76.92% 4 Missing and 2 partials :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11397      +/-   ##
==========================================
- Coverage   98.76%   98.74%   -0.02%     
==========================================
  Files         129      129              
  Lines       43416    43444      +28     
  Branches     2324     2328       +4     
==========================================
+ Hits        42879    42899      +20     
- Misses        383      388       +5     
- Partials      154      157       +3     
Flag Coverage Δ
CI-GHA 98.63% <80.64%> (-0.02%) :arrow_down:
OS-Linux 98.36% <80.64%> (-0.02%) :arrow_down:
OS-Windows 96.78% <80.64%> (-0.03%) :arrow_down:
OS-macOS 97.67% <80.64%> (-0.02%) :arrow_down:
Py-3.10.11 97.30% <74.19%> (-0.03%) :arrow_down:
Py-3.10.18 97.70% <74.19%> (-0.03%) :arrow_down:
Py-3.11.13 97.90% <74.19%> (-0.03%) :arrow_down:
Py-3.11.9 97.50% <74.19%> (-0.04%) :arrow_down:
Py-3.12.10 97.60% <74.19%> (-0.04%) :arrow_down:
Py-3.12.11 98.00% <74.19%> (-0.02%) :arrow_down:
Py-3.13.5 98.25% <74.19%> (-0.03%) :arrow_down:
Py-3.9.13 97.19% <80.64%> (-0.03%) :arrow_down:
Py-3.9.23 97.60% <80.64%> (-0.02%) :arrow_down:
Py-pypy7.3.16 86.76% <74.19%> (-5.68%) :arrow_down:
VM-macos 97.67% <80.64%> (-0.02%) :arrow_down:
VM-ubuntu 98.36% <80.64%> (-0.02%) :arrow_down:
VM-windows 96.78% <80.64%> (-0.03%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 07 '25 09:08 codecov[bot]

CodSpeed Performance Report

Merging #11397 will not alter performance

Comparing agners:fix-infinte-iter-chunks-on-empty-response (c08dce1) with master (48082a7)

Summary

✅ 59 untouched benchmarks

codspeed-hq[bot] avatar Aug 07 '25 09:08 codspeed-hq[bot]