fix(fastapi): Prevent double wrapping of sync handlers on FastAPI >= 0.137#6569
Merged
alexander-alderman-webb merged 2 commits intoJun 15, 2026
Conversation
…ts with FastAPI >= 0.137 FastAPI 0.137 changed include_router() to preserve a router tree instead of flattening routes. This causes get_request_handler() to be called on every request rather than once at registration. patch_get_request_handler() mutates dependant.call in-place, accumulating one _sentry_call wrapper per request. After ~987 requests Python's 1000-frame recursion limit is hit and the sync endpoint returns 500 permanently. Fix: add a _sentry_is_patched sentinel to _sentry_call so subsequent calls to the patched factory skip re-wrapping an already-patched dependant. Fixes getsentry#6568 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
alexander-alderman-webb
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #6568
What
patch_get_request_handler()wrapsdependant.callin a_sentry_callclosure and writes it back to the shareddependantobject. In FastAPI ≤ 0.136 this was called once per route at registration time, so one wrapper was applied and never repeated. In FastAPI 0.137,include_router()preserves a router tree instead of flattening routes, causingget_request_handler()to be called on every request — so the wrapper count grows by 1 per request, until Python's 1000-frame recursion limit is hit at ~987 requests and the endpoint returns 500 permanently.Only
def(sync) endpoints are affected.async defendpoints go through_sentry_app, not_sentry_call, and are unaffected.Why the root cause is correct
Test proves it
test_sync_endpoint_does_not_crash_after_many_requestssends 1,000 requests to a sync endpoint registered viainclude_router()and asserts all return 200. With the bug it fails at request 987; with the fix all 1,000 pass.Note: the test takes ~75 seconds because it sends 1,000 real requests through
TestClient. Happy to discuss a faster approach if the team prefers (e.g. inspectingdependant.calldirectly, or parameterising the request count).Would not reccommed to merge this test as-is
Additionally, the diagnosis was confirmed by local reproduction before filing the issue:
include_router()calls), 1,200 rapid-fireTestClientrequestsHow
Add a
_sentry_is_patched = Truesentinel attribute to_sentry_callafter wrapping, and check for it before wrapping. Ifdependant.callis already patched, skip — the_sentry_callstill executes on every request (thread/profiling context updates still happen), it just isn't re-wrapped.