Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/basic_memory/deps/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,12 @@ def schedule(self, task_name: str, **payload: Any) -> None:


def _log_task_failure(completed: asyncio.Task) -> None:
if completed.cancelled():
return
try:
completed.result()
except asyncio.CancelledError:
return
except Exception as exc: # pragma: no cover
logger.exception("Background task failed", error=str(exc))

Expand Down
38 changes: 38 additions & 0 deletions tests/deps/test_task_failure_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Tests for background task done-callback error handling."""

import asyncio
from unittest.mock import patch

import pytest

from basic_memory.deps.services import _log_task_failure


@pytest.mark.asyncio
async def test_log_task_failure_ignores_cancelled_task():
async def slow():
await asyncio.sleep(10)

task = asyncio.create_task(slow())
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task

with patch("basic_memory.deps.services.logger.exception") as mock_exc:
_log_task_failure(task)
mock_exc.assert_not_called()


@pytest.mark.asyncio
async def test_log_task_failure_logs_real_exception():
async def boom():
raise ValueError("sync failed")

task = asyncio.create_task(boom())
with pytest.raises(ValueError):
await task

with patch("basic_memory.deps.services.logger.exception") as mock_exc:
_log_task_failure(task)
mock_exc.assert_called_once()
assert "sync failed" in str(mock_exc.call_args)