@@ -246,25 +246,34 @@ def dummy_task(x, y):
246246 ]
247247
248248
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
251253 was repeated for every task invocation, leading to stackoverflows.
252254
253255 See https://github.com/getsentry/sentry-python/issues/265
254256 """
255257
256- results = []
257-
258258 @celery .task (name = "dummy_task" )
259259 def dummy_task ():
260- sentry_sdk .get_isolation_scope ().set_tag ("foo" , "bar" )
261- results .append (42 )
260+ return 42
262261
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
265271
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
268277
269278
270279def test_simple_no_propagation (capture_events , init_celery ):
0 commit comments