-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Request.wait_for_disconnection() method #4200
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4200 +/- ##
==========================================
+ Coverage 97.55% 97.56% +<.01%
==========================================
Files 43 43
Lines 8841 8855 +14
Branches 1383 1385 +2
==========================================
+ Hits 8625 8639 +14
Misses 93 93
Partials 123 123
Continue to review full report at Codecov.
|
One check is failing because of:
I don't think it's my fault that it's failing. Possibly the CI environment is broken. Limiting to 10 minutes seems rather restrictive... |
Yes, tests hang on MacOS periodically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix my note and update the documentation.
.. versionadded::
should use 4.0
as you use 4.0-specific request._cancel()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- A client opens a connection in HTTP keep-alive mode
- server code calls
await request.wait_for_disconnection()
- HTTP request is handled without socket closing (keep-alive mode is on)
await request.wait_for_disconnection()
hangs forever
Please fix it
as means of allowing request handlers to be notified of premature client disconnections.
The method name clearly says "wait for disconnection". In case "HTTP request is handled without socket closing (keep-alive mode is on)" there is no disconnection. That is a use case I expected. The way I envision this to be used, then def cancel_on_disconnect(handler):
@functools.wraps(handler)
async def wrap_handler(request):
wait = asyncio.create_task(request.wait_for_disconnection())
bgtask = asyncio.create_task(handler())
asyncio.wait([wait, bgtask], return_when=asyncio.FIRST_COMPLETED)
if bgtask.done():
wait.cancel()
return web.Response(bgtask.result())
bgtask.cancel()
raise web.HTTPGatewayTimeout()
return wrap_handler
@cancel_on_disconnect
def my_handler(request):
... The above code probably has bugs, I didn't even test it. But the point is that I can try to make it return at request end, even if there is no disconnection due to keepalive, but honestly I don't think it matters. |
I have a feeling that time of |
OK, fine... but then the documentation, and maybe method name, might need to be updated to reflect that the function also returns at the end of request, not just client disconnection. |
I was talking about cancellation, not a regular |
I didn't understand this, but makes sense.
Because it cancels it, there is no need for renaming, I think. |
looks good. The last remaining part is documentation update |
Note: feel free to rebase/squash commits, or else I can do it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please apply the suggestion.
No rebase is required; we use squash-on-merging strategy for aiohttp RPs
Co-Authored-By: Andrew Svetlov <[email protected]>
Thanks! |
as means of allowing request handlers to be notified of premature client disconnections. Co-Authored-By: Andrew Svetlov <[email protected]>
Backport to 3.10: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply 7f77733 on top of patchback/backports/3.10/7f777333a4ec0043ddf2e8d67146a626089773d9/pr-4200 Backporting merged PR #4200 into master
🤖 @patchback |
as means of allowing request handlers to be notified of premature client disconnections. Co-Authored-By: Andrew Svetlov <[email protected]> (cherry picked from commit 7f77733)
Co-authored-by: Andrew Svetlov <[email protected]> Co-authored-by: Gustavo J. A. M. Carneiro <[email protected]> Co-authored-by: J. Nick Koston <[email protected]>
as means of allowing request handlers to be notified of premature client
disconnections.
What do these changes do?
Adds a new async method
Request.wait_for_disconnection()
. This method does nothing except to wait until the connection that sent a request to be disconnected.This may be useful, in certain cases, now that aiohttp no longer automatically cancels request handlers upon client disconnection. E.g., if there is a slow DB query, it is beneficial to be able to cancel that DB query as soon as the client disconnects, to avoid the DB doing useless work.
This async method provides the needed "signal" for that. Although making use of it is not pretty, it can be simplified by a utility function/wrapper, probably outside the scope of aiohttp (and outside the scope of this PR).
Are there changes in behavior for the user?
A new method was added, as described above. It is opt-in, so should not affect existing code.
Related issue number
#2492
Checklist
CONTRIBUTORS.txt
CHANGES
folder<issue_id>.<type>
for example (588.bugfix)issue_id
change it to the pr id after creating the pr.feature
: Signifying a new feature..bugfix
: Signifying a bug fix..doc
: Signifying a documentation improvement..removal
: Signifying a deprecation or removal of public API..misc
: A ticket has been closed, but it is not of interest to users.