supermemoryai/supermemory · warning
Some background memory tasks did not complete on exit
Error message
Some background memory tasks did not complete on exit
What it means
Logged in the async context manager's __aexit__ when waiting for pending background memory tasks exceeds the hardcoded 5-second timeout. Remaining tasks are cancelled, so unsaved memories from those tasks are dropped. It signals the fire-and-forget pipeline could not drain in time during cleanup.
Source
Thrown at packages/openai-sdk-python/src/supermemory_openai/middleware.py:589
cancelled_count = 0
for task in self._background_tasks:
if not task.done():
task.cancel()
cancelled_count += 1
if cancelled_count > 0:
self._logger.debug(f"Cancelled {cancelled_count} pending background tasks")
async def __aenter__(self):
"""Async context manager entry."""
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit - wait for background tasks."""
try:
await self.wait_for_background_tasks(timeout=5.0)
except asyncio.TimeoutError:
self._logger.warn("Some background memory tasks did not complete on exit")
def __enter__(self):
"""Sync context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Sync context manager exit - attempt to wait for background tasks."""
if self._background_tasks:
try:
# Try to wait for background tasks in sync context
asyncio.run(self.wait_for_background_tasks(timeout=5.0))
except RuntimeError as e:
if "cannot be called from a running event loop" in str(e):
# In async context, just cancel the tasks
self._logger.warn(
"Cannot wait for background tasks in sync context from async environment. "
"Use async context manager or call wait_for_background_tasks() manually."
)View on GitHub (pinned to d436792e77)
Solutions
- Before exiting the context, await client.wait_for_background_tasks(timeout=30) with a larger timeout to drain tasks
- Lower the number of background saves per session or batch them
- Investigate slow Supermemory API responses (network, auth tenant throttling) and add retries to the underlying HTTP client
- Consider disabling background mode so saves are awaited inline and failures surface immediately
Example fix
# before
async with client:
await do_work(client) # exit gives tasks only 5s
# after
async with client:
await do_work(client)
await client.wait_for_background_tasks(timeout=60.0) Defensive patterns
Strategy: fallback
Validate before calling
if client._background_tasks:
await client.wait_for_background_tasks(timeout=60.0) # before exiting 'async with' Prevention
- Await wait_for_background_tasks with a large timeout before context exit
- Avoid enqueuing many saves immediately before exiting the context
- Use synchronous saving when every memory must persist
When it happens
Trigger: Using 'async with wrapped_client:' and exiting while background memory saves (embeddings, HTTP ingestion calls) are still running longer than 5s; slow network, cold-start latency, or many queued saves at exit.
Common situations: Serverless/Lambda handlers wrapping the client in an async context manager per request, batch scripts that finish quickly but leave slow ingestion behind, environments with throttled Supermemory API access.
Related errors
- Background tasks did not complete within {timeout}s timeout
- Cannot save memory in sync client from async context
- Cannot wait for background tasks in sync context from async
- Supermemory API request failed: ${error}
- Supermemory API request failed: ${error}
AI-assisted analysis of supermemoryai/supermemory@d436792e77 (2026-08-28).
Data as JSON: /api/errors/74f0631bba8508cf.
Report an issue: GitHub.