Skip to content

Evict failed futures from cache; replace deprecated get_event_loop#13

Closed
jtschoonhoven wants to merge 1 commit intoimnotjames:mainfrom
jtschoonhoven:fix/evict-failed-futures-and-modernize-event-loop
Closed

Evict failed futures from cache; replace deprecated get_event_loop#13
jtschoonhoven wants to merge 1 commit intoimnotjames:mainfrom
jtschoonhoven:fix/evict-failed-futures-and-modernize-event-loop

Conversation

@jtschoonhoven
Copy link

Summary

  • Evict failed futures from cache: When a cached future has completed with an exception, the decorator correctly re-executes the function on the next call. However, the stale failed future was never removed from the cache, permanently occupying a slot. With bounded caches (e.g. LRUCache), this can cause valid entries to be evicted. This change explicitly dels failed futures before re-executing.
  • Replace get_event_loop() with get_running_loop(): asyncio.get_event_loop() has been deprecated since Python 3.10. Since these decorators are only called from within async contexts, get_running_loop() is the correct replacement.
  • Fix typo: "Crete" → "Create" in comments.

Both cached and cachedmethod are fixed.

Test plan

  • New tests test_failed_futures_are_evicted_from_cache for both cached and cachedmethod verify that after a failed call, the cache slot is freed and a subsequent successful call populates it correctly
  • All 27 existing + new tests pass

🤖 Generated with Claude Code

Failed futures were never removed from the cache after being detected as
errored. While the retry-on-error behavior was correct (the function would
re-execute), the stale failed future continued to occupy a cache slot. With
bounded caches like LRUCache this can evict valid entries. This change
explicitly deletes failed futures from the cache before re-executing.

Also replaces asyncio.get_event_loop() with asyncio.get_running_loop(),
as get_event_loop() has been deprecated since Python 3.10 and these
decorators are only called from within async contexts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@imnotjames
Copy link
Owner

You have three things in one pull request. I'd prefer these to be separate.

@imnotjames
Copy link
Owner

Can you point where in the docs that get_event_loop is marked as deprecated? I see just the features it calls.

@jtschoonhoven
Copy link
Author

Can you point where in the docs that get_event_loop is marked as deprecated? I see just the features it calls.

My bad, there's a DeprecationWarning that fires when there's not already an event loop, but that's not really relevant for this decorator. Dropping this change.

You have three things in one pull request. I'd prefer these to be separate.

Fair. Closing this PR in favor of #14 and #15.

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.

2 participants