TASK-028: Add exception logging to fire-and-forget asyncio.create_task()
- Create logged_task() helper in backend/app/utils/async_utils.py to wrap fire-and-forget coroutines with exception logging - Ensures unhandled task exceptions are always logged to structlog instead of silently discarded (Python 3.11+ RuntimeWarning) - Update ban_service.py to use logged_task() for geo_cache.lookup_batch() background resolution - Add comprehensive tests for logged_task() in test_async_utils.py - Document fire-and-forget task conventions in Backend-Development.md The logged_task() wrapper catches any exception raised in a background task, logs it with full traceback context and task name, and never re-raises. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -8,12 +8,17 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import functools
|
||||
from collections.abc import Callable, Coroutine
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from typing import Callable, ParamSpec, TypeVar
|
||||
from typing import Any, ParamSpec, TypeVar
|
||||
|
||||
import structlog
|
||||
|
||||
P = ParamSpec("P")
|
||||
T = TypeVar("T")
|
||||
|
||||
log: structlog.stdlib.BoundLogger = structlog.get_logger()
|
||||
|
||||
DEFAULT_BLOCKING_EXECUTOR: ThreadPoolExecutor = ThreadPoolExecutor(
|
||||
max_workers=16,
|
||||
thread_name_prefix="bangui-blocking",
|
||||
@@ -43,3 +48,27 @@ async def run_blocking(
|
||||
func = functools.partial(func, *args, **kwargs)
|
||||
return await loop.run_in_executor(executor, func)
|
||||
return await loop.run_in_executor(executor, func, *args)
|
||||
|
||||
|
||||
async def logged_task(
|
||||
coro: Coroutine[Any, Any, Any],
|
||||
name: str,
|
||||
) -> None:
|
||||
"""Execute a coroutine with automatic exception logging.
|
||||
|
||||
Wraps fire-and-forget tasks to ensure exceptions are always logged and
|
||||
do not become unhandled task exceptions. Use with asyncio.create_task():
|
||||
|
||||
asyncio.create_task(
|
||||
logged_task(some_coroutine(), "task_name"),
|
||||
name="task_name"
|
||||
)
|
||||
|
||||
Args:
|
||||
coro: Coroutine to execute.
|
||||
name: Task name for logging context.
|
||||
"""
|
||||
try:
|
||||
await coro
|
||||
except Exception: # noqa: BLE001
|
||||
log.exception("background_task_failed", task_name=name)
|
||||
|
||||
Reference in New Issue
Block a user