Skip to content

Fix #380: drop empty moving-window keys in MemoryStorage.__expire_events - #383

Open
zheng-guannan577 wants to merge 3 commits into
alisaifee:masterfrom
zheng-guannan577:fix-380-memory-storage-expire-events
Open

zheng-guannan577 wants to merge 3 commits into
alisaifee:masterfrom
zheng-guannan577:fix-380-memory-storage-expire-events

Conversation

@zheng-guannan577

Copy link
Copy Markdown

Fixes #380.

When MemoryStorage.__expire_events removes the last expired
moving-window event for a key, it now removes the empty entry from
self.events as well, instead of leaving key: [] behind. A
long-lived storage instance therefore no longer retains one empty
entry per distinct moving-window key, and later cleanup passes no
longer scan those empty entries.

This mirrors the asynchronous implementation
(limits/aio/storage/memory.py), which already pops the empty event
entry at the same point.

Changes

  • limits/storage/memory.py: one line in __expire_events —
    self.events.pop(key, None) next to the existing
    self.locks.pop(key, None).
  • tests/storage/test_memory.py: new TestMovingWindowExpiry
    covering the issue's reproduction (1,000 keys expiring to an empty
    events dict), mixed live/expired keys, repeated cleanup, and reuse
    of an expired key afterwards.

Tests

  • Issue reproduction script: len(storage.events) goes from 1000
    (before) to 0 (after).
  • pytest tests/storage/test_memory.py — 3 passed.
  • Broader non-backend suite (tests/test_storage_schema.py,
    tests/storage/, tests/aio/storage/, tests/test_storage.py,
    tests/test_strategy.py, tests/test_limits.py, backend-marked
    tests deselected) — 85 passed, 0 failed.
  • ruff check and ruff format --check pass on the changed files.

Notes

  • AI assistance was used for investigation, implementation and
    automated testing of this change.
  • No maintainer approval or endorsement is claimed; this PR is
    submitted for review.

This branch has not been deployed

No deployments
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.

MemoryStorage retains empty moving-window keys after expiration

1 participant