Close request body Payload on exception#12985
Conversation
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.
Merging this PR will not alter performance
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
No description provided.