Skip to content

Make FileResponse.prepare re-entrant - #13399

Closed
AG0708 wants to merge 3 commits into
aio-libs:masterfrom
AG0708:codex/4647-reentrant-file-response
Closed

AG0708 wants to merge 3 commits into
aio-libs:masterfrom
AG0708:codex/4647-reentrant-file-response

Conversation

@AG0708

@AG0708 AG0708 commented Aug 11, 2026 •

Copy link
Copy Markdown

What do these changes do?

Makes FileResponse.prepare() return the existing response writer when the response has already been prepared, matching StreamResponse.prepare() instead of reopening and sending the file a second time. A regression test covers two calls on the same response.

Are there changes in behavior for the user?

Yes. Applications may explicitly await FileResponse.prepare() before returning the response—for example, so a temporary file can then be removed—without aiohttp preparing and sending it again. First-call behavior is unchanged.

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

No. The guard delegates the already-prepared case to the base implementation and adds no new state or dependency.

Related issue number

Fixes #4647.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • I added myself to CONTRIBUTORS.txt in alphabetical order
  • I added CHANGES/4647.feature.rst
Local validation
  • Relevant unit tests: 134 passed
  • Mypy on both changed Python files: passed
  • Changed-file pre-commit hooks: passed
  • Targeted Sphinx build with warnings as errors: passed

Drafted with OpenAI Codex (GPT-5); reviewed by @AG0708.

Signed-off-by: Abhinav Gorrepati <gorrepatiabhinav1@gmail.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Aug 11, 2026
@codecov

codecov Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.99%. Comparing base (d041d4d) to head (af230c0).
⚠️ Report is 3 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #13399   +/-   ##
=======================================
  Coverage   98.99%   98.99%           
=======================================
  Files         132      132           
  Lines       49454    49468   +14     
  Branches     2572     2573    +1     
=======================================
+ Hits        48959    48973   +14     
  Misses        371      371           
  Partials      124      124           
Flag Coverage Δ
Autobahn 22.08% <7.14%> (-0.01%) ⬇️
CI-GHA 98.91% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.68% <100.00%> (-0.01%) ⬇️
OS-Windows 97.04% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.93% <100.00%> (+<0.01%) ⬆️
Py-3.10 98.13% <100.00%> (+<0.01%) ⬆️
Py-3.11 98.39% <100.00%> (-0.01%) ⬇️
Py-3.12 98.47% <100.00%> (-0.01%) ⬇️
Py-3.13 98.46% <100.00%> (+<0.01%) ⬆️
Py-3.14 98.47% <100.00%> (-0.01%) ⬇️
Py-3.14t 97.56% <100.00%> (-0.01%) ⬇️
Py-pypy-3.11 97.43% <100.00%> (+<0.01%) ⬆️
VM-macos 97.93% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.68% <100.00%> (-0.01%) ⬇️
VM-windows 97.04% <100.00%> (+<0.01%) ⬆️
cython-coverage 38.14% <0.00%> (-0.02%) ⬇️

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

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

@codspeed

codspeed Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 9.64%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 83 untouched benchmarks
⏩ 83 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_simple_web_file_response[ssl-large] 307.7 ms 280.6 ms +9.64%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing AG0708:codex/4647-reentrant-file-response (af230c0) with master (d041d4d)2

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on master (77c5108) during the generation of this report, so d041d4d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@AG0708
AG0708 marked this pull request as ready for review August 12, 2026 18:12
@greptile-apps

greptile-apps Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Reviews (2): Last reviewed commit: "Clarify FileResponse.prepare return valu..." | Re-trigger Greptile

Comment thread docs/web_reference.rst Outdated
Signed-off-by: Abhinav Gorrepati <gorrepatiabhinav1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support FileResponse.prepare() to be re-entrant just as StreamResponse.prepare().

2 participants