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

Conversation

cmanallen
Copy link
Member

@cmanallen cmanallen commented Dec 20, 2024

  • Removes manual overrides of copy behavior and leaves it up to the caller.
    • E.g. a future use case may require a non-deepcopy. If we override copy they would have to remove the dunder copy, update every implementation which relies copy, before finally creating their own copy implementation.
  • Deepcopies the flag buffer.
    • Though we do not cache mutable references yet we may soon and so this foot gun should be removed from possibility.
  • Removes "copy" test coverage from test_lru_cache.py. We're no longer assuming copy usage and leave it up to the caller.
    • The existing test in tests/test_scope.py covers the cache pollution case originally mentioned here.
    • The mutable cache pollution case is not covered because we do not currently cache mutable objects.

In general a generic class should assume as few implementation details as possible. If we leave the existing copy method someone may assume copy semantics and rely on it in a way that is inappropriate.

Closes: #3886

Copy link

codecov bot commented Dec 20, 2024

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
13808 1 13807 4140
View the top 1 failed tests by shortest run time
tests.integrations.launchdarkly.test_launchdarkly test_launchdarkly_integration_threaded
Stack Traces | 0.111s run time
.../integrations/launchdarkly/test_launchdarkly.py:90: in test_launchdarkly_integration_threaded
    assert len(events) == 3
E   AssertionError: assert 4 == 3
E    +  where 4 = len([{'breadcrumbs': {'values': []}, 'contexts': {'runtime': {'build': '3.12.8 (main, Dec  4 2024, 06:20:10) [GCC 9.4.0]', 'name': 'CPython', 'version': '3.12.8'}, 'trace': {'parent_span_id': None, 'span_id': '932f498e64d5c9af', 'trace_id': 'be5c9d7f646540af80bd45541a3516e9'}}, 'environment': 'production', 'event_id': 'bcb716664ee741cc94df69e5e8c60dc9', ...}, {'breadcrumbs': {'values': []}, 'contexts': {'flags': {'values': [{'flag': 'hello', 'result': True}, {'flag': 'world', 'result': False}]}, 'runtime': {'build': '3.12.8 (main, Dec  4 2024, 06:20:10) [GCC 9.4.0]', 'name': 'CPython', 'version': '3.12.8'}, 'trace': {'parent_span_id': None, 'span_id': '89ed637b6b19fee0', 'trace_id': '0a88fb77d3444306942349fd938e2d95'}}, 'environment': 'production', 'event_id': '436339a8fc0747d0988122aaaacb2e47', ...}, {'breadcrumbs': {'values': []}, 'contexts': {'flags': {'values': [{'flag': 'hello', 'result': True}, {'flag': 'other', 'result': False}]}, 'runtime': {'build': '3.12.8 (main, Dec  4 2024, 06:20:10) [GCC 9.4.0]', 'name': 'CPython', 'version': '3.12.8'}, 'trace': {'parent_span_id': None, 'span_id': '89ed637b6b19fee0', 'trace_id': '0a88fb77d3444306942349fd938e2d95'}}, 'environment': 'production', 'event_id': '58300f9a104549b2948a66da0deb53c8', ...}, {'breadcrumbs': {'values': []}, 'contexts': {'flags': {'values': [{'flag': 'hello', 'result': True}]}, 'runtime': {'build': '3.12.8 (main, Dec  4 2024, 06:20:10) [GCC 9.4.0]', 'name': 'CPython', 'version': '3.12.8'}, 'trace': {'parent_span_id': None, 'span_id': '89ed637b6b19fee0', 'trace_id': '0a88fb77d3444306942349fd938e2d95'}}, 'environment': 'production', 'event_id': 'c2cdce18f16849b9a58c50c91735d447', ...}, {'breadcrumbs': {'values': []}, 'contexts': {'runtime': {'build': '3.12.8 (main, Dec  4 2024, 06:20:10) [GCC 9.4.0]', 'name': 'CPython', 'version': '3.12.8'}, 'trace': {'parent_span_id': None, 'span_id': '932f498e64d5c9af', 'trace_id': 'be5c9d7f646540af80bd45541a3516e9'}}, 'environment': 'production', 'event_id': '791ff1b6df5f4becb1903d3c0acea0af', ...}])

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

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

Sounds good to me. Not changing the copy behavior and leaving it for the caller to do the right thing makes sense.

@antonpirker antonpirker merged commit bb85c26 into master Dec 23, 2024
137 checks passed
@antonpirker antonpirker deleted the cmanallen/lru-fix-pollution branch December 23, 2024 10:37
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.

LRUCache.__copy__ is still unsound
2 participants