Emit AlruCacheLoopResetWarning on event loop auto-reset#744
Merged
Dreamsorcerer merged 5 commits intoaio-libs:masterfrom Mar 1, 2026
Merged
Emit AlruCacheLoopResetWarning on event loop auto-reset#744Dreamsorcerer merged 5 commits intoaio-libs:masterfrom
Dreamsorcerer merged 5 commits intoaio-libs:masterfrom
Conversation
added 3 commits
March 1, 2026 03:12
Add a UserWarning subclass that fires once per cache instance when _check_loop detects an event loop change and auto-clears stale entries. This helps users notice accidental multi-loop usage in production without breaking anything. Follow-up to aio-libs#743.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #744 +/- ##
==========================================
+ Coverage 97.45% 97.55% +0.09%
==========================================
Files 15 15
Lines 1022 1063 +41
Branches 57 59 +2
==========================================
+ Hits 996 1037 +41
Misses 23 23
Partials 3 3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Merging this PR will not alter performance
Comparing Footnotes
|
Member
Author
|
Demo: import asyncio
from async_lru import alru_cache
@alru_cache(maxsize=32)
async def fetch_data(item_id: int) -> str:
print(f"Fetching {item_id}...")
await asyncio.sleep(0.1)
return f"Data for {item_id}"
async def main() -> None:
# First loop usage
await fetch_data(1)
await fetch_data(1)
if __name__ == "__main__":
print("--- Running on Event Loop 1 ---")
asyncio.run(main())
print("\n--- Running on Event Loop 2 (Should trigger warning) ---")
asyncio.run(main())
print("\n--- Running on Event Loop 3 (Should NOT trigger warning) ---")
asyncio.run(main())Output: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
Adds a
AlruCacheLoopResetWarning(aUserWarningsubclass) that is emitted once per cache instance when_check_loopdetects an event loop change and auto-clears stale entries.This follows up on #743, which changed the behavior from raising
RuntimeErrorto silently clearing and rebinding. The warning closes the gap: users who accidentally share a cache across loops in production will now see a clear message without anything breaking.Users can suppress it cleanly:
Are there changes in behavior for the user?
Yes. A
AlruCacheLoopResetWarningis now emitted on the first event loop change per cache instance. It does not affect functionality — the auto-reset still works exactly as before.Is it a substantial change?
No. Adds a warning class, a boolean flag, and a
warnings.warn()call.Related issue number
Follow-up to #743 (which closed #611 and #742)
Checklist