Replies: 2 comments
-
I'm going to start this off as a discussion rather than an issue. Okay, could you help me out by providing a minimal reproducible example? Bits of this that I think we'd need to understand more clearly...
|
Beta Was this translation helpful? Give feedback.
0 replies
-
I got a similar issue
raise a proper exception except |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello, I am using httpx to make asynchronous requests in a generator function. However, I encountered a problem when I tried to cancel the generator using
generator.close()
orasyncio.gather(*tasks, return_exceptions=True)
. The generator did not exit gracefully and raised anasyncio.CancelledError
instead of a return. This caused some unwanted side effects and made it difficult to handle the cancellation properly.I looked into the source code of httpx and found that the problem was in the
aiter_raw
andaiter_bytes
functions in thehttpx/_client.py
module. These functions useasync for
to iterate over the response stream, but they do not catch theasyncio.CancelledError
that may be raised when the stream is cancelled. According to the [documentation] ofasyncio.CancelledError
, this exception should be caught and either re-raised or suppressed. In this case, I think it would make sense to re-raise it as aGeneratorExit
, which is the expected exception for generator termination.I suggest adding a
try-except
block around theasync for
loop in theaiter_raw
andaiter_bytes
functions, like this:Beta Was this translation helpful? Give feedback.
All reactions