Skip to content

fix(memory): reject a cleanup_interval that is not positive - #198

Merged
allen0099 merged 1 commit into
masterfrom
fix/memory-cleanup-interval
Sep 26, 2026
Merged

allen0099 merged 1 commit into
masterfrom
fix/memory-cleanup-interval

Conversation

@allen0099

Copy link
Copy Markdown
Owner

Closes #180.

Problem

MemoryBackend(cleanup_interval=0), or any negative value, was accepted. asyncio.sleep() returns at once for these values, so the cleanup loop never yielded for long. Measured over 0.5 s:

cleanup_interval sweeps CPU time
0 72 005 0.50 s
-5 70 759 0.50 s
60 0 0.00 s

Each sweep takes the cache lock and scans every entry, so requests also contend with it.

Change

  • MemoryBackend.__init__ raises ValueError when cleanup_interval <= 0.
  • The docstring, docs/BACKENDS.md (en and zh-TW) and the CHANGELOG are updated.

Tests

  • New test_cleanup_interval_must_be_positive, parametrized over 0 and -5.
  • Mutation check: without the check, only those two cases fail.
  • Full suite: 678 passed, and memory.py coverage stays at 100%. No live servers were run, since the change is memory-only.

With 0 or a negative value asyncio.sleep() returned at once, so the
cleanup loop spun on a full CPU core and took the cache lock on every
pass. MemoryBackend now raises ValueError for it.

Closes #180
@allen0099 allen0099 added this to the 0.3.8 milestone Sep 26, 2026
@allen0099 allen0099 added bug Something isn't working backends Cache backends and their atomic primitives labels Sep 26, 2026
@allen0099
allen0099 merged commit 24427ec into master Sep 26, 2026
11 checks passed
@allen0099
allen0099 deleted the fix/memory-cleanup-interval branch September 26, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backends Cache backends and their atomic primitives bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemoryBackend: reject cleanup_interval <= 0

1 participant