Skip to content

Commit

Permalink
[stable-1] remove improper usage of @cache on async functions (#67) (#69
Browse files Browse the repository at this point in the history
)

* remove improper usage of @cache on async functions (#67)

* remove improper usage of @cache on async functions

Neither functools.cache nor functools.lru_cache which was previously
used here works properly with async functions. These decorators cache
the coroutine, but coroutines can only be awaited once, so this falls
apart.

Take the following example:

```
In [1]: from functools import cache

In [2]: @cache
   ...: async def abc():
   ...:     return 1234
   ...:

In [3]: print(await abc())
1234

In [4]: print(await abc())
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[4], line 1
----> 1 print(await abc())

RuntimeError: cannot reuse already awaited coroutine
```

* Fix changelog.

---------

Co-authored-by: Felix Fontein <[email protected]>

* update changelog entry

---------

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
gotmax23 and felixfontein authored Apr 19, 2023
1 parent 5f7b8e3 commit 7399502
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 6 additions & 0 deletions changelogs/fragments/67-cache.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bugfixes:
- Remove improper usage of ``@functools.lru_cache`` on async functions in the
``antsibull_core.ansible_core`` module
(https://github.com/ansible-community/antsibull-core/pull/67,
https://github.com/ansible-community/antsibull-core/pull/69).
5 changes: 1 addition & 4 deletions src/antsibull_core/ansible_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import re
import tempfile
import typing as t
from functools import lru_cache, partial
from functools import partial
from urllib.parse import urljoin

import aiofiles
Expand Down Expand Up @@ -65,7 +65,6 @@ async def _get_json(self, query_url: str) -> t.Dict[str, t.Any]:
pkg_info = await response.json()
return pkg_info

@lru_cache(None)
async def get_release_info(self) -> t.Dict[str, t.Any]:
"""
Retrieve information about releases of the ansible-core/ansible-base package from pypi.
Expand Down Expand Up @@ -244,7 +243,6 @@ def source_is_correct_version(ansible_core_source: t.Optional[str],
return False


@lru_cache(None)
async def checkout_from_git(download_dir: str, repo_url: str = _ANSIBLE_CORE_URL) -> str:
"""
Checkout the ansible-core git repo.
Expand All @@ -260,7 +258,6 @@ async def checkout_from_git(download_dir: str, repo_url: str = _ANSIBLE_CORE_URL
return ansible_core_dir


@lru_cache(None)
async def create_sdist(source_dir: str, dest_dir: str) -> str:
"""
Create an sdist for the python package at a given path.
Expand Down

0 comments on commit 7399502

Please sign in to comment.