Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Remove potentially problematic `do_not_wait_for` (#4032)

sarayourfriendcc475dfa179e
diff --git a/api/api/utils/asyncio.py b/api/api/utils/asyncio.py
index cfe3d4c1e..13a8bd5be 100644
--- a/api/api/utils/asyncio.py
+++ b/api/api/utils/asyncio.py
@@ -1,36 +1,6 @@
-import asyncio
-import logging
-from collections.abc import Awaitable
-
from rest_framework.generics import get_object_or_404
from asgiref.sync import sync_to_async
-parent_logger = logging.getLogger(__name__)
-
-
-_do_not_wait_for_logger = parent_logger.getChild("do_not_wait_for")
-
-
-def do_not_wait_for(awaitable: Awaitable) -> None:
- """
- Consume an awaitable without waiting for it to finish.
-
- This allows us to call an async function that we don't care about
- the result of, without needing to wait for it to complete. This is
- useful, for example, if some operation creates a side effect that
- isn't necessary for the response we're processing (e.g., Redis tallying).
- """
- try:
- loop = asyncio.get_running_loop()
- except RuntimeError as exc:
- _do_not_wait_for_logger.error(
- "`do_not_wait_for` must be called inside a running event loop."
- )
- raise exc
-
- loop.create_task(awaitable)
-
-
aget_object_or_404 = sync_to_async(get_object_or_404)
diff --git a/api/api/utils/image_proxy/__init__.py b/api/api/utils/image_proxy/__init__.py
index 21b0b3025..f6be7d117 100644
--- a/api/api/utils/image_proxy/__init__.py
+++ b/api/api/utils/image_proxy/__init__.py
@@ -14,7 +14,6 @@ from asgiref.sync import sync_to_async
from redis.exceptions import ConnectionError
from api.utils.aiohttp import get_aiohttp_session
-from api.utils.asyncio import do_not_wait_for
from api.utils.image_proxy.exception import UpstreamThumbnailException
from api.utils.image_proxy.extension import get_image_extension
from api.utils.image_proxy.photon import get_photon_request_params
@@ -166,9 +165,7 @@ async def get(
params=params,
headers=headers,
)
- do_not_wait_for(
- _tally_response(tallies, media_info, month, domain, upstream_response)
- )
+ await _tally_response(tallies, media_info, month, domain, upstream_response)
upstream_response.raise_for_status()
status_code = upstream_response.status
content_type = upstream_response.headers.get("Content-Type")
@@ -193,9 +190,7 @@ async def get(
if isinstance(exc, ClientResponseError):
status = exc.status
- do_not_wait_for(
- _tally_client_response_errors(tallies, month, domain, status)
- )
+ await _tally_client_response_errors(tallies, month, domain, status)
logger.warning(
f"Failed to render thumbnail "
f"{upstream_url=} {status=} "
diff --git a/api/api/utils/image_proxy/extension.py b/api/api/utils/image_proxy/extension.py
index cb9fad261..a7da85e9c 100644
--- a/api/api/utils/image_proxy/extension.py
+++ b/api/api/utils/image_proxy/extension.py
@@ -9,7 +9,6 @@ from asgiref.sync import sync_to_async
from redis.exceptions import ConnectionError
from api.utils.aiohttp import get_aiohttp_session
-from api.utils.asyncio import do_not_wait_for
from api.utils.image_proxy.exception import UpstreamThumbnailException
@@ -47,7 +46,7 @@ async def get_image_extension(image_url: str, media_identifier) -> str | None:
else:
ext = None
- do_not_wait_for(_cache_extension(cache, key, ext))
+ await _cache_extension(cache, key, ext)
except Exception as exc:
sentry_sdk.capture_exception(exc)
raise UpstreamThumbnailException(