-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
"Timeout context manager should be used inside a task" slightly misleading #10153
Comments
I feel like running multiple event loops is not a typical or recommended practice, so if you are doing this, you'd probably need to be aware of these sort of issues anyway. If you have a suggestion for a better message that won't confuse typical users with a single loop (and possibly not even knowing what the loop is), then feel free to open a PR changing it. |
Yeah, it's a tricky one. If you want to keep the message simple for the common case, distinct errors would probably be best: task = asyncio.current_task(loop=self._loop)
if task is None:
if asyncio.current_task() is not None:
raise RuntimeError("Timeout context manager used in a task running on a different event loop")
else:
raise RuntimeError("Timeout context manager should be used inside a task") Then the error mentioning event loops shouldn't occur if there is only a single event loop. If you think this is reasonable I can put in a PR, but also I understand that this may not be worth changing. |
I think that looks reasonable. It shouldn't impact performance as the extra check is only done when we're already in an error state. |
This turned out to be too complicated to implement to be worth it for fixing such a minor problem. |
Describe the bug
This error is also raised when the timer context is used within a task, but that task is on a different event loop to the one the
ClientSession
was created with because it usesasyncio.current_task(loop=self._loop)
. Callingasyncio.current_task()
would show there is a task, but not on the correct event loop.To Reproduce
This stripped down example is a bit convoluted, but is the least amount of code I could come up with to reproduce what my application does. It has a long running context and spins up an event loop in another thread, which tries to use the session. Now that I have realised the problem have plans to change this entirely, but I thought it would be worth reporting as I think the error could be clearer.
Expected behavior
The error could make it clear that this error could also occur if the task was created on the wrong event loop and not just that the timeout manager was not used inside a task. Or possibly it could distinguish the two cases and give different errors.
Logs/tracebacks
Python Version
aiohttp Version
multidict Version
propcache Version
yarl Version
OS
RHEL8
Related component
Client
Additional context
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: