Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix cache pollution when copying mutable references #3887

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions sentry_sdk/_lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ def __init__(self, max_size):
self.hits = self.misses = 0
self.full = False

def __copy__(self):
# type: () -> LRUCache
new = LRUCache(max_size=self.max_size)
new.hits = self.hits
new.misses = self.misses
new.full = self.full
new._data = self._data.copy()
return new

def set(self, key, value):
# type: (Any, Any) -> None
current = self._data.pop(key, _SENTINEL)
Expand Down
7 changes: 0 additions & 7 deletions sentry_sdk/flag_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import copy
from typing import TYPE_CHECKING

import sentry_sdk
Expand All @@ -25,12 +24,6 @@ def clear(self):
# type: () -> None
self.buffer = LRUCache(self.capacity)

def __copy__(self):
# type: () -> FlagBuffer
buffer = FlagBuffer(capacity=self.capacity)
buffer.buffer = copy(self.buffer)
return buffer

def get(self):
# type: () -> list[FlagData]
return [{"flag": key, "result": value} for key, value in self.buffer.get_all()]
Expand Down
4 changes: 2 additions & 2 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
import warnings
from copy import copy
from copy import copy, deepcopy
from collections import deque
from contextlib import contextmanager
from enum import Enum
Expand Down Expand Up @@ -252,7 +252,7 @@ def __copy__(self):

rv._last_event_id = self._last_event_id

rv._flags = copy(self._flags)
rv._flags = deepcopy(self._flags)

return rv

Expand Down
53 changes: 0 additions & 53 deletions tests/test_lru_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from copy import copy, deepcopy

from sentry_sdk._lru_cache import LRUCache

Expand Down Expand Up @@ -59,55 +58,3 @@ def test_cache_get_all():
assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]
cache.get(1)
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]


def test_cache_copy():
cache = LRUCache(3)
cache.set(0, 0)
cache.set(1, 1)

copied = copy(cache)
cache.set(2, 2)
cache.set(3, 3)
assert copied.get_all() == [(0, 0), (1, 1)]
assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]

copied = copy(cache)
cache.get(1)
assert copied.get_all() == [(1, 1), (2, 2), (3, 3)]
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]


def test_cache_deepcopy():
cache = LRUCache(3)
cache.set(0, 0)
cache.set(1, 1)

copied = deepcopy(cache)
cache.set(2, 2)
cache.set(3, 3)
assert copied.get_all() == [(0, 0), (1, 1)]
assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]

copied = deepcopy(cache)
cache.get(1)
assert copied.get_all() == [(1, 1), (2, 2), (3, 3)]
assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]


def test_cache_pollution():
cache1 = LRUCache(max_size=2)
cache1.set(1, True)
cache2 = copy(cache1)
cache2.set(1, False)
assert cache1.get(1) is True
assert cache2.get(1) is False


def test_cache_pollution_deepcopy():
cache1 = LRUCache(max_size=2)
cache1.set(1, True)
cache2 = deepcopy(cache1)
cache2.set(1, False)
assert cache1.get(1) is True
assert cache2.get(1) is False
Loading