supermemoryai/supermemory · warning
Background tasks did not complete within {timeout}s timeout
Error message
Background tasks did not complete within {timeout}s timeout What it means
Logged when wait_for_background_tasks times out: memory-save tasks queued in the background (fire-and-forget ingestion) did not finish within the configured timeout (default applied at call site). The middleware cancels all remaining tasks, so those memory writes are abandoned. It is a degradation warning, not a crash.
Source
Thrown at packages/openai-sdk-python/src/supermemory_openai/middleware.py:557
if not self._background_tasks:
return
self._logger.debug(
f"Waiting for {len(self._background_tasks)} background tasks to complete"
)
try:
if timeout is not None:
await asyncio.wait_for(
asyncio.gather(*self._background_tasks, return_exceptions=True),
timeout=timeout,
)
else:
await asyncio.gather(*self._background_tasks, return_exceptions=True)
self._logger.debug("All background tasks completed")
except asyncio.TimeoutError:
self._logger.warn(
f"Background tasks did not complete within {timeout}s timeout"
)
# Cancel remaining tasks
tasks_to_cancel = [task for task in self._background_tasks if not task.done()]
for task in tasks_to_cancel:
task.cancel()
if tasks_to_cancel:
await asyncio.gather(*tasks_to_cancel, return_exceptions=True)
raise
def cancel_background_tasks(self) -> None:
"""Cancel all pending background tasks."""
cancelled_count = 0
for task in self._background_tasks:
if not task.done():
task.cancel()
cancelled_count += 1View on GitHub (pinned to d436792e77)
Solutions
- Increase the timeout: call wait_for_background_tasks(timeout=30) explicitly before exiting the context
- Reduce work enqueued right before exit, or flush incrementally during the run
- Check Supermemory API latency/rate limits and network connectivity; retry with backoff on the underlying client
- Use synchronous (non-background) memory saving if completeness matters more than latency
Example fix
// before
async with client:
for m in items:
await client.chat.completions.create_with_memory(...) # 5s exit timeout
// after
async with client:
for m in items:
await client.chat.completions.create_with_memory(...)
await client.wait_for_background_tasks(timeout=60.0) Defensive patterns
Strategy: retry
Validate before calling
pending = [t for t in client._background_tasks if not t.done()]
if len(pending) > 20:
await client.wait_for_background_tasks(timeout=10) # drain early Prevention
- Drain background tasks periodically instead of only at exit
- Set generous timeouts for high-volume sessions
- Watch for this warning and correlate with missing memories
When it happens
Trigger: Exiting the wrapped client's context manager (or calling wait_for_background_tasks(timeout=N)) while slow Supermemory API calls (embeddings, document ingestion) are still in flight; network latency or rate limiting exceeding the timeout; a large batch of create_with_memory calls right before exit.
Common situations: Fire-and-forget memory mode with short exit timeouts (5s default in __exit__/__aexit__), slow or rate-limited Supermemory endpoints, high-volume batch jobs that enqueue many saves then close the context, or transient network issues in serverless environments.
Understand the failure class
- Timeouts: ETIMEDOUT, deadlines, and hung requests — what actually expires when a request times out.
Related errors
- Some background memory tasks did not complete on exit
- 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/c8951240c686574f.
Report an issue: GitHub.