@@ -246,25 +246,34 @@ def dummy_task(x, y):
246
246
]
247
247
248
248
249
- def test_no_stackoverflows (celery ):
250
- """We used to have a bug in the Celery integration where its monkeypatching
249
+ def test_no_double_patching (celery ):
250
+ """Ensure that Celery tasks are only patched once to prevent stack overflows.
251
+
252
+ We used to have a bug in the Celery integration where its monkeypatching
251
253
was repeated for every task invocation, leading to stackoverflows.
252
254
253
255
See https://github.com/getsentry/sentry-python/issues/265
254
256
"""
255
257
256
- results = []
257
-
258
258
@celery .task (name = "dummy_task" )
259
259
def dummy_task ():
260
- sentry_sdk .get_isolation_scope ().set_tag ("foo" , "bar" )
261
- results .append (42 )
260
+ return 42
262
261
263
- for _ in range (10000 ):
264
- dummy_task .delay ()
262
+ # Initially, the task should not be marked as patched
263
+ assert not hasattr (dummy_task , "_sentry_is_patched" )
264
+
265
+ # First invocation should trigger patching
266
+ result1 = dummy_task .delay ()
267
+ assert result1 .get () == 42
268
+ assert getattr (dummy_task , "_sentry_is_patched" , False ) is True
269
+
270
+ patched_run = dummy_task .run
265
271
266
- assert results == [42 ] * 10000
267
- assert not sentry_sdk .get_isolation_scope ()._tags
272
+ # Second invocation should not re-patch
273
+ result2 = dummy_task .delay ()
274
+ assert result2 .get () == 42
275
+ assert dummy_task .run is patched_run
276
+ assert getattr (dummy_task , "_sentry_is_patched" , False ) is True
268
277
269
278
270
279
def test_simple_no_propagation (capture_events , init_celery ):
0 commit comments