-
Notifications
You must be signed in to change notification settings - Fork 63
feat: handle CancelledError - cancel if no other waiters #697
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
Draft
BobTheBuidler
wants to merge
13
commits into
aio-libs:master
Choose a base branch
from
BobTheBuidler:patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−6
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
865db04
feat: handle CancelledError - cancel if no other waiters
BobTheBuidler 9244bc7
Update __init__.py
BobTheBuidler 8c2b878
Create test_cancel.py
BobTheBuidler b283118
Update test_cancel.py
BobTheBuidler f8ceb96
Update test_cancel.py
Dreamsorcerer 27100f3
fix: name error CancelledError
BobTheBuidler 19b1b3b
feat(test): more cancel tests
BobTheBuidler e85e099
finish up impl
BobTheBuidler 2c4d654
Update test_cancel.py
BobTheBuidler f9cfbf9
lint
BobTheBuidler 6441a1d
Update __init__.py
BobTheBuidler 94c303b
Update __init__.py
BobTheBuidler 4495e48
Merge branch 'master' into patch-3
BobTheBuidler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,8 @@ class _CacheParameters(TypedDict): | |
| class _CacheItem(Generic[_R]): | ||
| fut: "asyncio.Future[_R]" | ||
| later_call: Optional[asyncio.Handle] | ||
| waiters: int | ||
| task: "asyncio.Task[_R]" | ||
|
|
||
| def cancel(self) -> None: | ||
| if self.later_call is not None: | ||
|
|
@@ -192,6 +194,15 @@ def _task_done_callback( | |
|
|
||
| fut.set_result(task.result()) | ||
|
|
||
| def _handle_cancelled_error(self, key: Hashable, cache_item: "_CacheItem") -> None: | ||
| # Called when a waiter is cancelled. | ||
| # If this is the last waiter and the underlying task is not done, | ||
| # cancel the underlying task and remove the cache entry. | ||
| if cache_item.waiters == 1 and not cache_item.task.done(): | ||
| cache_item.cancel() # Cancel TTL expiration | ||
| cache_item.task.cancel() # Cancel the running coroutine | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we either need to pop from __tasks here or just remove __tasks entirely. I'd prefer the latter but would like a second opinion before i implement |
||
| self.__cache.pop(key, None) # Remove from cache | ||
|
|
||
| async def __call__(self, /, *fn_args: Any, **fn_kwargs: Any) -> _R: | ||
| if self.__closed: | ||
| raise RuntimeError(f"alru_cache is closed for {self}") | ||
|
|
@@ -205,8 +216,20 @@ async def __call__(self, /, *fn_args: Any, **fn_kwargs: Any) -> _R: | |
| if cache_item is not None: | ||
| self._cache_hit(key) | ||
| if not cache_item.fut.done(): | ||
| return await asyncio.shield(cache_item.fut) | ||
|
|
||
| # Each logical waiter increments waiters on entry. | ||
| cache_item.waiters += 1 | ||
|
|
||
| try: | ||
| # All waiters await the same future. | ||
| return await asyncio.shield(cache_item.fut) | ||
| except asyncio.CancelledError: | ||
| # If a waiter is cancelled, handle possible last-waiter cleanup. | ||
| self._handle_cancelled_error(key, cache_item) | ||
| raise | ||
| finally: | ||
| # Each logical waiter decrements waiters on exit (normal or cancelled). | ||
| cache_item.waiters -= 1 | ||
| # If the future is already done, just return the result. | ||
| return cache_item.fut.result() | ||
|
|
||
| fut = loop.create_future() | ||
|
|
@@ -215,14 +238,22 @@ async def __call__(self, /, *fn_args: Any, **fn_kwargs: Any) -> _R: | |
| self.__tasks.add(task) | ||
| task.add_done_callback(partial(self._task_done_callback, fut, key)) | ||
|
|
||
| self.__cache[key] = _CacheItem(fut, None) | ||
| cache_item = _CacheItem(fut, None, 1, task) | ||
| self.__cache[key] = cache_item | ||
|
|
||
| if self.__maxsize is not None and len(self.__cache) > self.__maxsize: | ||
| dropped_key, cache_item = self.__cache.popitem(last=False) | ||
| cache_item.cancel() | ||
| dropped_key, dropped_cache_item = self.__cache.popitem(last=False) | ||
| dropped_cache_item.cancel() | ||
|
|
||
| self._cache_miss(key) | ||
| return await asyncio.shield(fut) | ||
|
|
||
| try: | ||
| return await asyncio.shield(fut) | ||
| except asyncio.CancelledError: | ||
| self._handle_cancelled_error(key, cache_item) | ||
| raise | ||
| finally: | ||
| cache_item.waiters -= 1 | ||
|
|
||
| def __get__( | ||
| self, instance: _T, owner: Optional[Type[_T]] | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from async_lru import alru_cache | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("num_to_cancel", [0, 1, 2, 3]) | ||
| async def test_cancel(num_to_cancel: int) -> None: | ||
| cache_item_task_finished = False | ||
|
|
||
| @alru_cache | ||
| async def coro(val: int) -> int: | ||
| # I am a long running coro function | ||
| nonlocal cache_item_task_finished | ||
| await asyncio.sleep(2) | ||
| cache_item_task_finished = True | ||
| return val | ||
|
|
||
| # create 3 tasks for the cached function using the same key | ||
| tasks = [asyncio.create_task(coro(1)) for _ in range(3)] | ||
|
|
||
| # force the event loop to run once so the tasks can begin | ||
| await asyncio.sleep(0) | ||
|
|
||
| # maybe cancel some tasks | ||
| for i in range(num_to_cancel): | ||
| tasks[i].cancel() | ||
|
|
||
| # allow enough time for the non-cancelled tasks to complete | ||
| await asyncio.sleep(3) | ||
|
|
||
| # check state | ||
| assert cache_item_task_finished == (num_to_cancel < 3) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_cancel_single_waiter_triggers_handle_cancelled_error(): | ||
| # This test ensures the _handle_cancelled_error path (waiters == 1) is exercised. | ||
| cache_item_task_finished = False | ||
|
|
||
| @alru_cache | ||
| async def coro(val: int) -> int: | ||
| nonlocal cache_item_task_finished | ||
| await asyncio.sleep(2) | ||
| cache_item_task_finished = True | ||
| return val | ||
|
|
||
| task = asyncio.create_task(coro(42)) | ||
| await asyncio.sleep(0) | ||
| task.cancel() | ||
| try: | ||
| await task | ||
| except asyncio.CancelledError: | ||
| pass | ||
|
|
||
| # The underlying coroutine should be cancelled, so the flag should remain False | ||
| assert cache_item_task_finished is False |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking further on this... if the _CacheItem now holds a reference to the task itself, does it even need a separate Future for the result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented the refactorings I mentioned as possible here #708