-
-
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
Close a connection if an unexpected exception occurs while sending a request #2828
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2828 +/- ##
=======================================
Coverage 97.96% 97.96%
=======================================
Files 39 39
Lines 7426 7426
Branches 1305 1305
=======================================
Hits 7275 7275
Misses 48 48
Partials 103 103 Continue to review full report at Codecov.
|
@asvetlov and @brycedrennan some comments on this MR and the issue related
|
|
|
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.
LGFM, but there is one little bit...
tests/test_client_session.py
Outdated
@@ -379,6 +380,42 @@ def test_context_manager(connector, loop): | |||
assert e.strerror == err.strerror | |||
|
|||
|
|||
async def test_close_conn_on_error(create_session): | |||
class UnexpectedException(Exception): |
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.
Your try/except block catches BaseExceptions, but here you test just for Exception. It's easy to change the code and break the behavior, but not the tests.
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.
Done.
aiohttp/client.py
Outdated
resp = await req.send(conn) | ||
except BaseException: | ||
conn.close() | ||
raise |
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.
It seems we're don't the same thing twice. How about to regroup try/except a bit?
try:
resp = await req.send(conn)
try:
await resp.start(conn, read_unit_eof)
except:
resp.close()
raise
except:
conn.close()
raise
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.
Done.
And sign question about context managers. It's strange that they doesn't helps. |
@brycedrennan Thanks! |
Change test to use context manager, BaseException
Checked, sorry for the noise but finally I was able to connect all of the dots, I would like to propose a silly change in this MR. The context manager is not able to close the connection because there is no response yet and won't be able to make it. My bet is that with your example @brycedrennan related with the context manager you are having another exception raised within the original exception. Basically, the change that I propose is adding a check to make sure that the resp attribute is present [1]. Thoughts @asvetlov and @brycedrennan ? [1] https://github.com/aio-libs/aiohttp/blob/master/aiohttp/client.py#L790 |
@pfreixes Also the test is catching the specific exception that was thrown. |
@brycedrennan my fault I was totally confused, the exception happens in the Forget my comment. The response and the connection teardown in case of exception within Client.__request can't rely on the context manager. |
Everything looks good, will merge tomorrow if nobody objects. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs. |
What do these changes do?
Close a connection if an unexpected exception occurs while sending a request
Are there changes in behavior for the user?
No.
Related issue number
Resolves #2827
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.