Skip to content

Close request body Payload on exception#12985

Open
Dreamsorcerer wants to merge 4 commits into
masterfrom
fix-client-leak
Open

Close request body Payload on exception#12985
Dreamsorcerer wants to merge 4 commits into
masterfrom
fix-client-leak

Conversation

@Dreamsorcerer

Copy link
Copy Markdown
Member

No description provided.

The HTTP client did not close the request-body Payload when the body
write failed part-way through (e.g. the server resets the TCP connection
mid-upload). For file-backed bodies (open(path, "rb"), pathlib.Path, or
FormData with a file field), the underlying OS file descriptor was leaked
until garbage collection, so repeated failed uploads to a peer that
disconnects mid-upload accumulate descriptors and can exhaust
RLIMIT_NOFILE, denying service to the client process.

This mirrors the server-side fix (Response.write_eof try/finally close)
on the symmetric client path. The body is closed in ClientSession._request's
terminal except BaseException handler, alongside the existing close on the
success path and the redirect-abort branches. Closing here (rather than in
ClientRequest._write_bytes) preserves the retry_persistent_connection path,
which continues the request loop and must keep the body intact for retry.
Payload.close() is idempotent, so this is safe even where abort branches
already close the body.
@Dreamsorcerer Dreamsorcerer requested a review from asvetlov as a code owner June 23, 2026 23:11
@Dreamsorcerer Dreamsorcerer added backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot labels Jun 23, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 83 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing fix-client-leak (8ce8c69) with master (c4de983)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 (51da10a) during the generation of this report, so c4de983 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Comment thread tests/test_client_functional.py Dismissed
@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.96%. Comparing base (c4de983) to head (8ce8c69).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12985   +/-   ##
=======================================
  Coverage   98.96%   98.96%           
=======================================
  Files         131      131           
  Lines       48087    48127   +40     
  Branches     2496     2497    +1     
=======================================
+ Hits        47587    47627   +40     
  Misses        376      376           
  Partials      124      124           
Flag Coverage Δ
Autobahn 22.22% <10.00%> (-0.02%) ⬇️
CI-GHA 98.90% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.67% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.04% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.95% <100.00%> (+<0.01%) ⬆️
Py-3.10 98.14% <100.00%> (-0.01%) ⬇️
Py-3.11 98.41% <100.00%> (-0.01%) ⬇️
Py-3.12 98.50% <100.00%> (-0.01%) ⬇️
Py-3.13 98.47% <100.00%> (-0.01%) ⬇️
Py-3.14 98.49% <100.00%> (-0.01%) ⬇️
Py-3.14t 97.58% <100.00%> (-0.01%) ⬇️
Py-pypy-3.11 97.47% <100.00%> (+0.01%) ⬆️
VM-macos 97.95% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.67% <100.00%> (+<0.01%) ⬆️
VM-windows 97.04% <100.00%> (+<0.01%) ⬆️
cython-coverage 38.07% <100.00%> (+0.05%) ⬆️

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.

@Dreamsorcerer Dreamsorcerer requested a review from webknjaz as a code owner June 23, 2026 23:52
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jun 23, 2026
Comment thread aiohttp/client.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot 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.

2 participants