-
-
Notifications
You must be signed in to change notification settings - Fork 953
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
Raise exception from background task on BaseHTTPMiddleware #2812
base: master
Are you sure you want to change the base?
Conversation
@graingert would you mind reviewing this? 🙏 |
ah I broke it |
ok back working again |
@@ -258,14 +259,15 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | |||
except OSError: | |||
raise ClientDisconnect() | |||
else: | |||
async with anyio.create_task_group() as task_group: | |||
with collapse_excgroups(): |
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.
Why was this added?
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's subtle but it's needed to get test_custom_middleware to pass
the reason is app_exc shouldn't be subject to collapse_excgroups (it's outside of with recv_stream, send_stream, collapse_excgroups():
)- eg it's from the user's code so might have use a TG or Nursery and therefore legitimately wrapped in a EG
however that means if something raises an exception from StreamingResponse they're not really expecting an (optional!) background task to watch for disconnections, so they expect the EG to be unwrapped
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.
I mention that it's an optional background task in StreamingResponse, because it should not be the case that StreamingResponse raises a different kind of exception based on asgi version
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.
I don't want to change anything outside the base http middleware because of it
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.
we can nest the raise app_exc
inside the collapse_excgroups() in base.py - but it's not really correct
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.
I'll have a think
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.
Do you have an MRE to share of a code that would be a problem with the raise app_exc
inside the collapse_excgroups
?
The PR you are working in seems to also change the StreamingResponse
, so it's not really avoiding changes in it.
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.
so the raise app_exc can't be put inside the async with of the task group because the app_exc might not be set until after the task group has finished, and you should not put anything in between a task group and collapse_excgroups because then you're collapsing exc groups that you've not created. Thus it has to be this way.
If you want to avoid touching StreamingResponse in this PR, I've separated the changes out into another PR that's more like the old anyio 3 behaviour and strict_exception_groups=False
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.
There's a discussion ongoing in the trio gitter to make strict_exception_groups not deprecated and expose it in anyio, then I'd make a PR to use that where available
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.
Do you have an MRE to share of a code that would be a problem with the
raise app_exc
inside thecollapse_excgroups
?The PR you are working in seems to also change the
StreamingResponse
, so it's not really avoiding changes in it.
MRE converted to test cases and in #2830
How to reproduce: