Skip to content

Commit 34a3580

Browse files
authored
Use weakref in dedupe where possible (#4834)
We cannot use `weakref` always since built-in exception types like `ZeroDivisionError` raise a `TypeError`. This makes the memory usage at least a little better for custom exception types. #### Issues * resolves: #3165 * resolves: PY-17
1 parent a926640 commit 34a3580

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

sentry_sdk/integrations/dedupe.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import weakref
2+
13
import sentry_sdk
24
from sentry_sdk.utils import ContextVar, logger
35
from sentry_sdk.integrations import Integration
@@ -35,12 +37,24 @@ def processor(event, hint):
3537
if exc_info is None:
3638
return event
3739

40+
last_seen = integration._last_seen.get(None)
41+
if last_seen is not None:
42+
# last_seen is either a weakref or the original instance
43+
last_seen = (
44+
last_seen() if isinstance(last_seen, weakref.ref) else last_seen
45+
)
46+
3847
exc = exc_info[1]
39-
if integration._last_seen.get(None) is exc:
48+
if last_seen is exc:
4049
logger.info("DedupeIntegration dropped duplicated error event %s", exc)
4150
return None
4251

43-
integration._last_seen.set(exc)
52+
# we can only weakref non builtin types
53+
try:
54+
integration._last_seen.set(weakref.ref(exc))
55+
except TypeError:
56+
integration._last_seen.set(exc)
57+
4458
return event
4559

4660
@staticmethod

0 commit comments

Comments
 (0)