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 lru cache copying #3883

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
35 changes: 21 additions & 14 deletions sentry_sdk/_lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

"""

from copy import copy, deepcopy
from typing import cast, Any

SENTINEL = object()

Expand Down Expand Up @@ -92,11 +92,24 @@ def __init__(self, max_size):
self.hits = self.misses = 0

def __copy__(self):
cache = LRUCache(self.max_size)
cache.full = self.full
cache.cache = copy(self.cache)
cache.root = deepcopy(self.root)
return cache
# walk around the circle and fill the new root / cache
cache = {}
node_old = root_old = self.root
node_new = root_new = [cast(Any, None)] * 4
while (node_old := node_old[NEXT]) is not root_old:
_, _, key, val = node_old
cache[node_old[KEY]] = node_new[NEXT] = [node_new, None, key, val]
node_new = node_new[NEXT]

# close the circle
node_new[NEXT] = root_new
root_new[PREV] = node_new

lru_cache = LRUCache(self.max_size)
lru_cache.full = self.full
lru_cache.cache = cache
lru_cache.root = root_new
return lru_cache
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

def set(self, key, value):
link = self.cache.get(key, SENTINEL)
Expand Down Expand Up @@ -168,14 +181,8 @@ def get_all(self):
nodes = []
node = self.root[NEXT]

# To ensure the loop always terminates we iterate to the maximum
# size of the LRU cache.
for _ in range(self.max_size):
# The cache may not be full. We exit early if we've wrapped
# around to the head.
if node is self.root:
break
while node is not self.root:
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
nodes.append((node[KEY], node[VALUE]))
node = node[NEXT]

return nodes
return nodes
37 changes: 36 additions & 1 deletion tests/test_lru_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from copy import copy
from copy import copy, deepcopy

from sentry_sdk._lru_cache import LRUCache

Expand Down Expand Up @@ -76,3 +76,38 @@ def test_cache_copy():
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
22 changes: 22 additions & 0 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ def test_all_slots_copied():
assert getattr(scope_copy, attr) == getattr(scope, attr)


def test_scope_flags_copy():
# Assert forking creates a deepcopy of the flag buffer. The new
# scope is free to mutate without consequence to the old scope. The
# old scope is free to mutate without consequence to the new scope.
old_scope = Scope()
old_scope.flags.set("a", True)

new_scope = old_scope.fork()
new_scope.flags.set("a", False)
old_scope.flags.set("b", True)
new_scope.flags.set("c", True)

assert old_scope.flags.get() == [
{"flag": "a", "result": True},
{"flag": "b", "result": True},
]
assert new_scope.flags.get() == [
{"flag": "a", "result": False},
{"flag": "c", "result": True},
]


def test_merging(sentry_init, capture_events):
sentry_init()

Expand Down
Loading