Skip to content

LRUCache.__copy__ is still unsound #3886

Closed
@cmanallen

Description

@cmanallen

How do you use Sentry?

Sentry Saas (sentry.io)

Version

latest git revision

Steps to Reproduce

Mutable references are copied -- not the underlying value. This is visible here:

new._data = self._data.copy()
Where we copy the _data attribute and not deepcopy.

class MutableObject:
    def __init__(self):
        self.count = 0

cache1 = LRUCache(max_size=2)
cache1.set(1, MutableObject())

cache2 = cache1.__copy__()
node = cache2.get(1)
node.count += 1

assert cache1.get(1).count == 0  # fail
assert cache2.get(1).count == 1

We should not be manually implementing __copy__ at all. This is a general helper class and should not assume copy semantics. I lay out this case in this comment and this comment.

Expected Result

Mutable objects do not pollute copied caches.

Actual Result

Mutable objects do pollute copied caches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions