@@ -33,11 +33,18 @@ def setup_once():
3333 old_build_tracer = trace .build_tracer
3434
3535 def sentry_build_tracer (name , task , * args , ** kwargs ):
36- # Need to patch both methods because older celery sometimes
37- # short-circuits to task.run if it thinks it's safe.
38- task .__call__ = _wrap_task_call (task , task .__call__ )
39- task .run = _wrap_task_call (task , task .run )
40- task .apply_async = _wrap_apply_async (task , task .apply_async )
36+ if not getattr (task , "_sentry_is_patched" , False ):
37+ # Need to patch both methods because older celery sometimes
38+ # short-circuits to task.run if it thinks it's safe.
39+ task .__call__ = _wrap_task_call (task , task .__call__ )
40+ task .run = _wrap_task_call (task , task .run )
41+ task .apply_async = _wrap_apply_async (task , task .apply_async )
42+
43+ # `build_tracer` is apparently called for every task
44+ # invocation. Can't wrap every celery task for every invocation
45+ # or we will get infinitely nested wrapper functions.
46+ task ._sentry_is_patched = True
47+
4148 return _wrap_tracer (task , old_build_tracer (name , task , * args , ** kwargs ))
4249
4350 trace .build_tracer = sentry_build_tracer
0 commit comments