Close the request body in `ASGITransport` when the app doesn't read it all
AL
3d ago+79+79+01234567890123456789-1-1-0123456789
Summary
ASGITransport takes request.stream.__aiter__() and reads it from the app's receive(). An app that answers before the end of the body (a 413, a malformed body) leaves that async generator suspended, and nothing closes it: the garbage collector finalizes it later, on whatever event loop collects it. Under Trio that is a ResourceWarning: Async generator ... was garbage collected before it had been exhausted.
The body is now closed in the existing finally around the app call, unless it is still running: an app may leave a task in receive(), and closing a running generator raises.
The nested response iterators (aiter_text, aiter_lines, SSE, AsyncResponseStream) have the same problem; #1212 covers them, so they are left out here.
Checklist
- I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
- I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
- I've updated the documentation accordingly.