Skip to content

fix(dedupe): Track seen exceptions via attribute instead of ContextVar - #7533

Open
ericapisani wants to merge 9 commits into
major/3.0from
py-2381-take-4
Open

ericapisani wants to merge 9 commits into
major/3.0from
py-2381-take-4

Conversation

@ericapisani

@ericapisani ericapisani commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Replace DedupeIntegration's weakref/ContextVar-based "last seen"
tracking with a _handled_by_sentry flag set directly on the
exception. This avoids the contextvar leaking across async contexts
and removes the need for reset_last_seen(), which the client used
to call after before_send dropped an exception.

Add a regression test for the case where the same exception instance
is re-raised (e.g. Django re-raising in middleware after a view
already handled it) to confirm it's still deduped correctly.

Fixes PY-2381
Fixes #6094

@linear-code

linear-code Bot commented Sep 17, 2026

Copy link
Copy Markdown

PY-2381

@github-actions

github-actions Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Codecov Results 📊

✅ 58593 passed | ❌ 1 failed | ⏭️ 2729 skipped | Total: 61323 | Pass Rate: 95.55% | Execution Time: 162m 46s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -75536
Passed Tests 📉 -71097
Failed Tests 📈 +1
Skipped Tests 📉 -4440

➕ New Tests (1)

View new tests
  • test_input_attributes_nonstreaming_chat[messages0-expected_system_instructions0-expected_input_messages0]
    • File: tests.integrations.mistral.test_mistral
    • Status: ❌ Failing

❌ Failed Tests

test_input_attributes_nonstreaming_chat[messages0-expected_system_instructions0-expected_input_messages0]

File: tests.integrations.mistral.test_mistral
Suite: py3.10-mistral-v2.6.0
Error: KeyError: 140424429125632

Stack Trace
tests/integrations/mistral/test_mistral.py:389: in test_input_attributes_nonstreaming_chat
    sentry_init(
tests/conftest.py:349: in inner
    client = sentry_sdk.Client(*a, **kw)
sentry_sdk/client.py:277: in __init__
    self._init_impl()
sentry_sdk/client.py:427: in _init_impl
    self.integrations = setup_integrations(
sentry_sdk/integrations/__init__.py:217: in setup_integrations
    for integration_cls in iter_default_integrations(
sentry_sdk/integrations/__init__.py:44: in iter_default_integrations
    yield getattr(import_module(module), cls)
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
    ???
<frozen importlib._bootstrap>:1024: in _find_and_load
    ???
<frozen importlib._bootstrap>:171: in __enter__
    ???
<frozen importlib._bootstrap>:123: in acquire
    ???
E   KeyError: 140424429125632

✅ Patch coverage is 100.00%. Project has 2068 uncovered lines.
❌ Project coverage is 90.07%. Comparing base (9d1c31d) to head (28204ee).

Coverage diff
@@            Coverage Diff             @@
##        master       #PR       +/-##
==========================================
- Coverage    90.28%    90.07%    -0.21%
==========================================
  Files          195       179       -16
  Lines        26195     20819     -5376
  Branches      9758      7350     -2408
==========================================
+ Hits         23649     18751     -4898
- Misses        2546      2068      -478
- Partials      1483      1213      -270

Generated by Codecov Action

@ericapisani ericapisani changed the title (WIP) Attempt #1 fix(dedupe): Track seen exceptions via attribute instead of ContextVar Sep 18, 2026
@ericapisani
ericapisani marked this pull request as ready for review September 18, 2026 19:10
@ericapisani
ericapisani requested a review from a team as a code owner September 18, 2026 19:10

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 900ad90. Configure here.

Comment thread sentry_sdk/integrations/dedupe.py

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant