Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
fastapi: fix wrapping of middlewares #3012
base: main
Are you sure you want to change the base?
fastapi: fix wrapping of middlewares #3012
Changes from all commits
707b192
dcfab56
a78b1c6
e25e143
54d4482
81f1e83
96e66b9
46e1809
daa5a1b
de9e795
f9d348b
7bc89b3
f629932
437891b
1340ac7
003d7af
be26393
ec545b7
f825c76
d909633
0de3e37
d57b673
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Question: do you think it would be possible to use wrapt for monkeypatching instead?
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'd rather not. This is simpler and works just fine.
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.
@xrmx genuine question: what would be the benefit of that in this case?
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.
wrapt monkeypatching tend to generally work better than shuffling classes under the hood, had to move to wrapt in httpx instrumentation because otherwise the instrumentation did not patch stuff loaded before the load of the instrumentation.
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.
Unless there's a specific reason I don't think introducing more complexity and code to be executed is helpful here. There are plenty of tests checking that the monkey patching is working correctly. This is also not a method that users would ever call, in fact as someone who's responsible for significant changes to this very method I would have made it a private method if it had not already been public for years.
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.
This was the root cause of tests failures. It turns out every request was being made twice because it was getting redirected to https. Before this PR that wasn't being instrumented correctly, so this was not being caught! I think that's another major bug this PR is fixing.
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.
The
build_middleware_stack
this PR patches is called on the startup event - which theTestClient
only runs if called within a context manager, that's why @adriangb is usingExitStack
here. FYI