Skip to content

Commit 0a75283

Browse files
committed
Merge branch 'master' into major/3.0
2 parents f589a72 + 4b9d329 commit 0a75283

2 files changed

Lines changed: 22 additions & 25 deletions

File tree

‎sentry_sdk/integrations/fastapi.py‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22
from copy import deepcopy
3-
from functools import wraps
43
from typing import TYPE_CHECKING
54

65
import sentry_sdk
@@ -25,6 +24,7 @@
2524
StarletteIntegration,
2625
StarletteRequestExtractor,
2726
_get_cached_request_body_attribute,
27+
_wrap_sync_handler,
2828
)
2929
except DidNotEnable:
3030
raise DidNotEnable("Starlette is not installed or incompatible")
@@ -163,27 +163,8 @@ def _sentry_get_request_handler(*args: "Any", **kwargs: "Any") -> "Any":
163163
dependant
164164
and dependant.call is not None
165165
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)
172166
):
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)
187168

188169
old_app = old_get_request_handler(*args, **kwargs)
189170

‎sentry_sdk/integrations/starlette.py‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,25 @@ def event_processor(event: "Event", hint: "Dict[str, Any]") -> "Event":
545545
)
546546

547547

548+
def _update_active_thread() -> None:
549+
span = sentry_sdk.get_current_span()
550+
if type(span) is Span:
551+
span._segment._update_active_thread()
552+
553+
554+
def _wrap_sync_handler(handler: "Callable[..., Any]") -> "Callable[..., Any]":
555+
if getattr(handler, "_sentry_patched", False):
556+
return handler
557+
558+
@functools.wraps(handler)
559+
def _sentry_sync_handler(*args: "Any", **kwargs: "Any") -> "Any":
560+
_update_active_thread()
561+
return handler(*args, **kwargs)
562+
563+
_sentry_sync_handler._sentry_patched = True # type: ignore[attr-defined]
564+
return _sentry_sync_handler
565+
566+
548567
def patch_request_response() -> None:
549568
old_request_response = starlette.routing.request_response
550569

@@ -569,10 +588,7 @@ def _sentry_sync_func(*args: "Any", **kwargs: "Any") -> "Any":
569588
if integration is None:
570589
return old_func(*args, **kwargs)
571590

572-
current_span = sentry_sdk.get_current_span()
573-
574-
if type(current_span) is Span:
575-
current_span._segment._update_active_thread()
591+
_update_active_thread()
576592

577593
sentry_scope = sentry_sdk.get_isolation_scope()
578594

0 commit comments

Comments
 (0)