|
1 | 1 | import sys |
2 | 2 | from copy import deepcopy |
3 | | -from functools import wraps |
4 | 3 | from typing import TYPE_CHECKING |
5 | 4 |
|
6 | 5 | import sentry_sdk |
|
25 | 24 | StarletteIntegration, |
26 | 25 | StarletteRequestExtractor, |
27 | 26 | _get_cached_request_body_attribute, |
| 27 | + _wrap_sync_handler, |
28 | 28 | ) |
29 | 29 | except DidNotEnable: |
30 | 30 | raise DidNotEnable("Starlette is not installed or incompatible") |
@@ -163,27 +163,8 @@ def _sentry_get_request_handler(*args: "Any", **kwargs: "Any") -> "Any": |
163 | 163 | dependant |
164 | 164 | and dependant.call is not None |
165 | 165 | and not iscoroutinefunction(dependant.call) |
166 | | - # FastAPI >= 0.137 calls get_request_handler() on every request |
167 | | - # (router-tree traversal) rather than once at registration. Guard |
168 | | - # against accumulating _sentry_call wrappers on the shared |
169 | | - # dependant object, which would cause a RecursionError after ~987 |
170 | | - # requests as the call chain grows past Python's recursion limit. |
171 | | - and not getattr(dependant.call, "_sentry_is_patched", False) |
172 | 166 | ): |
173 | | - old_call = dependant.call |
174 | | - |
175 | | - @wraps(old_call) |
176 | | - def _sentry_call(*args: "Any", **kwargs: "Any") -> "Any": |
177 | | - current_span = sentry_sdk.get_current_span() |
178 | | - |
179 | | - if type(current_span) is Span: |
180 | | - segment = current_span._segment |
181 | | - segment._update_active_thread() |
182 | | - |
183 | | - return old_call(*args, **kwargs) |
184 | | - |
185 | | - _sentry_call._sentry_is_patched = True # type: ignore[attr-defined] |
186 | | - dependant.call = _sentry_call |
| 167 | + dependant.call = _wrap_sync_handler(dependant.call) |
187 | 168 |
|
188 | 169 | old_app = old_get_request_handler(*args, **kwargs) |
189 | 170 |
|
|
0 commit comments