Skip to content

Commit 0e3d0ec

Browse files
authored
fix: Dont crash when client is bound inside of push_scope (#163)
See #159
1 parent 3fe8712 commit 0e3d0ec

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

sentry_sdk/hub.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ def __enter__(self):
9090
return scope
9191

9292
def __exit__(self, exc_type, exc_value, tb):
93-
assert self._hub.pop_scope_unsafe() == self._layer, "popped wrong scope"
93+
layer = self._hub.pop_scope_unsafe()
94+
assert layer[1] == self._layer[1], "popped wrong scope"
95+
if layer[0] != self._layer[0]:
96+
warning = (
97+
"init() called inside of pushed scope. This might be entirely "
98+
"legitimate but usually occurs when initializing the SDK inside "
99+
"a request handler or task/job function. Try to initialize the "
100+
"SDK as early as possible instead."
101+
)
102+
logger.warning(warning)
94103

95104

96105
class Hub(with_metaclass(HubMeta)):

tests/test_basics.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,16 @@ def test_integration_scoping():
224224
logger.warning("This is not a warning")
225225

226226
assert len(events) == 1
227+
228+
229+
def test_client_initialized_within_scope(sentry_init, capture_events, caplog):
230+
caplog.set_level(logging.WARNING)
231+
232+
sentry_init(debug=True)
233+
234+
with push_scope():
235+
sentry_init()
236+
237+
record, = (x for x in caplog.records if x.levelname == "WARNING")
238+
239+
assert record.msg.startswith("init() called inside of pushed scope.")

0 commit comments

Comments
 (0)