Skip to content

Commit 3e28853

Browse files
authored
fix(integrations): Check retries_left before capturing exception (#3803)
Since rq/rq#1964 the job status is set to Failed before the handler decides whether to capture or not the exception while handle_job_failure has not yet been called so the job is not yet re-scheduled leading to all exceptions getting captured in RQ version >= 2.0. Related to #1076 Fixes #3707
1 parent 01146bd commit 3e28853

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

sentry_sdk/integrations/rq.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
9090

9191
def sentry_patched_handle_exception(self, job, *exc_info, **kwargs):
9292
# type: (Worker, Any, *Any, **Any) -> Any
93-
# Note, the order of the `or` here is important,
94-
# because calling `job.is_failed` will change `_status`.
95-
if job._status == JobStatus.FAILED or job.is_failed:
93+
retry = (
94+
hasattr(job, "retries_left")
95+
and job.retries_left
96+
and job.retries_left > 0
97+
)
98+
failed = job._status == JobStatus.FAILED or job.is_failed
99+
if failed and not retry:
96100
_capture_exception(exc_info)
97101

98102
return old_handle_exception(self, job, *exc_info, **kwargs)

tests/integrations/rq/test_rq.py

-5
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,6 @@ def test_traces_sampler_gets_correct_values_in_sampling_context(
254254
@pytest.mark.skipif(
255255
parse_version(rq.__version__) < (1, 5), reason="At least rq-1.5 required"
256256
)
257-
@pytest.mark.skipif(
258-
parse_version(rq.__version__) >= (2,),
259-
reason="Test broke in RQ 2.0. Investigate and fix. "
260-
"See https://github.com/getsentry/sentry-python/issues/3707.",
261-
)
262257
def test_job_with_retries(sentry_init, capture_events):
263258
sentry_init(integrations=[RqIntegration()])
264259
events = capture_events()

0 commit comments

Comments
 (0)