llama-stack-mirror/llama_stack/providers/utils
Sébastien Han dd513449de
Some checks failed
Unit Tests / unit-tests (3.13) (push) Failing after 2m9s
Integration Auth Tests / test-matrix (oauth2_token) (push) Failing after 2s
SqlStore Integration Tests / test-postgres (3.13) (push) Failing after 1s
Integration Tests (Replay) / generate-matrix (push) Successful in 5s
Vector IO Integration Tests / test-matrix (push) Failing after 51s
SqlStore Integration Tests / test-postgres (3.12) (push) Failing after 53s
Unit Tests / unit-tests (3.12) (push) Failing after 2m20s
Pre-commit / pre-commit (push) Successful in 2m46s
Integration Tests (Replay) / Integration Tests (, , , client=, ) (push) Failing after 2m51s
fix: InferenceStore workers being cancelled on event loop change (#4373)
# What does this PR do?

The InferenceStore write queue workers were created during initialize()
which runs in the app factory's event loop. When uvicorn starts, it
creates its own event loop, causing the original worker tasks to be
cancelled. This resulted in chat completions being queued but never
written to the database.

Root cause: asyncio tasks are bound to the event loop they were created
in. When the event loop changes (app factory -> uvicorn server), tasks
from the old loop are cancelled. However, the task references remained
in self._worker_tasks, so _ensure_workers_started() thought workers
were still running.

The fix removes eager worker creation from initialize(). Workers must
not be started during app factory execution because that event loop is
temporary. Instead, _ensure_workers_started() creates workers lazily
during store_chat_completion(), which runs during request handling and
is guaranteed to be in uvicorn's event loop.

Additionally, _ensure_workers_started() now checks for active (non-done)
tasks rather than just checking if the list is non-empty. This makes
the code resilient to any future event loop transitions.

This follows the standard async Python pattern: defer background task
creation until the server is actually running, not during app init.

## Test Plan

Run the server with postgres or sqlite, I tested with postgres:

```
uv run llama stack run llama_stack/distributions/starter/run-with-postgres-store.yaml
```

Execute an inference call:

```
export INFERENCE_MODEL=gpt-4o-mini && curl -fsS http://127.0.0.1:8321/v1/chat/completions -H "Content-Type: application/json" -d "{\"model\": \"openai/$INFERENCE_MODEL\",\"messages\": [{\"role\": \"user\", \"content\": \"What color is grass?\"}], \"max_tokens\": 128, \"temperature\": 0.0}"
```

Check the DB being populated:

```
podman exec postgres psql -U llamastack -d llamastack -t -c "SELECT COUNT(*) FROM chat_completions;" 
    1
```

**AGAINST STABLE BRANCH 0.3.X SINCE MAIN DOES NOT HAVE THE ISSUE.**

---------

Signed-off-by: Sébastien Han <seb@redhat.com>
2025-12-11 11:32:04 -05:00
..
bedrock feat: use SecretStr for inference provider auth credentials (#3724) 2025-10-10 07:32:50 -07:00
common chore(rename): move llama_stack.distribution to llama_stack.core (#2975) 2025-07-30 23:30:53 -07:00
datasetio chore(misc): make tests and starter faster (#3042) 2025-08-05 14:55:05 -07:00
files fix(expires_after): make sure multipart/form-data is properly parsed (#3612) 2025-09-30 16:14:03 -04:00
inference fix: InferenceStore workers being cancelled on event loop change (#4373) 2025-12-11 11:32:04 -05:00
kvstore feat(stores)!: use backend storage references instead of configs (#3697) 2025-10-20 13:20:09 -07:00
memory fix: remove consistency checks (#3881) 2025-10-21 14:40:14 -07:00
responses fix: uninitialised enable_write_queue (#4264) 2025-12-02 09:37:21 -05:00
scoring chore: enable pyupgrade fixes (#1806) 2025-05-01 14:23:50 -07:00
sqlstore fix: enable SQLite WAL mode to prevent database locking errors (backport #4048) (#4226) 2025-11-24 11:30:57 -08:00
telemetry test(telemetry): Telemetry Tests (#3805) 2025-10-17 10:43:33 -07:00
tools feat(tools)!: substantial clean up of "Tool" related datatypes (#3627) 2025-10-02 15:12:03 -07:00
vector_io feat: migrate to FIPS-validated cryptographic algorithms (#3423) 2025-09-12 11:18:19 +02:00
__init__.py API Updates (#73) 2024-09-17 19:51:35 -07:00
pagination.py chore(refact): move paginate_records fn outside of datasetio (#2137) 2025-05-12 10:56:14 -07:00
scheduler.py refactor(logging): rename llama_stack logger categories (#3065) 2025-08-21 17:31:04 -07:00