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 += 1

View on GitHub (pinned to d436792e77)

Solutions

  1. Increase the timeout: call wait_for_background_tasks(timeout=30) explicitly before exiting the context
  2. Reduce work enqueued right before exit, or flush incrementally during the run
  3. Check Supermemory API latency/rate limits and network connectivity; retry with backoff on the underlying client
  4. 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

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

Related errors


AI-assisted analysis of supermemoryai/supermemory@d436792e77 (2026-08-28). Data as JSON: /api/errors/c8951240c686574f. Report an issue: GitHub.