Skip to content
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

Fix response not closed when read stream canceled #2148

Closed

Conversation

guyskk
Copy link
Contributor

@guyskk guyskk commented Mar 28, 2022

See #2139 (comment), the PR only fix issues of httpx client.

@@ -933,7 +933,7 @@ def _send_handling_auth(
request = next_request
history.append(response)

except Exception as exc:
except BaseException as exc:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe catch Exception and asyncio.exceptions.CancelledError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need catch all exceptions to ensure response closed, just like async with context manager. And the exception will re-raised so it will not swallow errors.

@tomchristie
Copy link
Member

Great thank you.

Yup, I'm in favour of switching over to BaseException here - it makes sense to always close the response. Including for asyncio or trio cancelations or SystemExit, and KeyboardInterrupt.

I'm not keen on us having test cases that introduce sleeps, tho. Perhaps we could change those around somehow.

@guyskk
Copy link
Contributor Author

guyskk commented Mar 28, 2022

In Python 3.6, asyncio.wait_for will not cancel the task, so I skip the test in py36. https://stackoverflow.com/questions/69370252/asyncio-wait-for-didnt-cancel-the-task

@florimondmanca
Copy link
Member

florimondmanca commented Mar 28, 2022

I think we should move the close() / aclose() calls to finally blocks instead of except BaseException blocks, see my analysis here: #2139 (comment) (The result is essentially the same - "always close" but perhaps it's cleaner)

@guyskk
Copy link
Contributor Author

guyskk commented Mar 28, 2022

Hi @florimondmanca Seems the response not always need close, eg stream request: https://github.com/encode/httpx/blob/master/httpx/_client.py#L904 .
And in handle redirect cases, I'm not sure if the response should close https://github.com/encode/httpx/blob/master/httpx/_client.py#L969.

@tomchristie
Copy link
Member

Thanks @guyskk.

I've taken another pass at this in #2156, with a different approach to the test case, so that we don't need to introduce slow sleep behaviour into our test suite. (Anywhere we can avoid the tests using actual network I/O we should)

@guyskk
Copy link
Contributor Author

guyskk commented Mar 31, 2022

@tomchristie 👍👍 your test case is perfect!

@tomchristie
Copy link
Member

Thanks folks. Closing this in favour of #2156.

We also ought to address any similar cases in httpcore.

@guyskk guyskk closed this Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants