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
StopIteration
in a generator in a thread behaves differently in asyncio and Trio
#475
Comments
I think the correct fix would be to replicate the Trio behavior in the asyncio backend of AnyIO. |
The above PR ensures identical behavior on both backends. Feel free to review. |
That was very fast! Amazing, thank you! 🚀 🙇 🍰 |
Hey there! I hate it when people come and "ping" pushing for something open source that we do in our free time. ...and still, here I am. 🙈 Is there anything I could help with to get this included in a release? |
Is this a critical issue that cannot wait for AnyIO 4.0? If so, I will consider making a 3.6.3 patch release. |
I just realized this is part of a bigger 4.x release. No worries, I figured out a way to workaround internally to be able to install from a fixed AnyIO git commit and monkey patch the needed parts. |
StopIteration
in a generator in a thread behaves differently in asyncio and TrioAsyncio
This code will show an error on the terminal and hang there forever:
The error shows:
But I suspect the exception is being printed but not really raised, I'm not even sure. If I run it through the VS Code debugger the exception is not shown in the debugger, only printed in the terminal.
Trio
Changing only the backend to Trio:
It raises a
RuntimeError
wrapping (raise from
) theStopIteration
error, and the program stops with that error instead of hanging:Problem
This is a problem in FastAPI (via Starlette) because a sync (
def
) function that raises aStopIteration
error hangs in there and never returns the response (it should return a 500 Server Error).What to do
I see several places where this could be handled.
On asyncio directly. Not sure how feasible that is, and it would still only work for some future Python, so the fix would still be needed in other places for current Python versions.
On AnyIO: it could replicate the Trio behavior, even on asyncio. I guess that would mean AnyIO would have a more predictable behavior (in this specific corner case) independent of backend. But I understand if you would prefer to keep that backend-specific and not touch that.
On Starlette: if you would rather not have it on AnyIO, the next place to put it would be on Starlette. Although I guess it's possible they would not want to do that and expect users to clean up their own code.
On FastAPI: if none of the previous places would like to have that changed.
So, here's the question. Would you like a fix/change for that here on AnyIO?
The text was updated successfully, but these errors were encountered: