litellm/tests/unit/proxy/response_polling
tavren27 897b10f370
fix(responses): stop the background stream when a polling response is cancelled (#49384)
* fix(responses): stop the background stream when a polling response is cancelled

With polling via cache on, POST /v1/responses/{id}/cancel only wrote the
cancelled status into Redis. The background task kept streaming from the
provider, and its next state write merged over the cancel, so the response
came back as completed with full output and full billing

The background task now races the stream against a watcher that reads the
stored state every second and stops the stream when it reads cancelled, on
any worker or pod. Every state write goes through a Lua script that only
sets the key when it exists and is not cancelled, so a write already in
flight when the cancel lands can no longer overwrite it

* fix(responses): settle a cancelled background call and stop it on delete

A cancel that lands before the provider answers now reports one client
disconnect through the failure hooks, so the key's parallel slot and budget
reservation come back and the spend logs get a failure row.

The state guard and the watcher compare a status-first prefix with GETRANGE
instead of decoding the stored response inside Redis, so each write and read
costs the same whatever the response size. The watcher also stops when the
response was deleted, and a cancelled stream closes its upstream response.

The TTFT keepalive now cancels the in-flight call when its caller is
cancelled before the first ping.

* fix(proxy): free the parallel slot when a keepalive stream is cancelled before the first token

With SSE keepalive pings on, a reader that left during the pings cancelled the
upstream call but never reported the failure, so the key's parallel slot stayed
taken until its TTL. The ping loop now reports one client disconnect, or the
producer's own error when it failed first.

A configured polling TTL that is not a number now falls back to the cache
default instead of failing every state write.

* fix(proxy): report no disconnect when the model answered before a keepalive reader left

A reader that left during keepalive pings just as the model answered got a 499 failure report on top of the answered stream's own disconnect cleanup, so one call was settled twice. The wrapper now reports a disconnect only when the producer was cancelled or failed.

* test(proxy): settle the keepalive producer directly instead of sleeping in reader-leave tests

The two reader-leave tests now hand the wrapper a future and resolve it after
the first ping, and the limiter test waits for the reader's first chunk before
cancelling it, so none of them depends on a 10 ms sleep

* fix(proxy): keep polling working on a Redis that refuses scripts, keep the cancel ttl, and sanitize polling ids in logs

A Redis whose ACL denies scripting answered every polling state write with
an error, so background responses stayed queued and a cancel never stopped
the call. The write now falls back to GETRANGE then SET with the same
cancelled and missing checks when Redis refuses the script, and the
background task's cancel check is a plain GETRANGE.

The cancel route now writes with the configured polling ttl instead of the
1 hour default, and the polling id is sanitized in the state write logs.

* fix(responses): keep a cancel when Redis refuses scripts

On a Redis whose ACL denies scripting, the polling write fell back to a
GETRANGE status check and a plain SET, so a cancel that arrived between the
two calls was overwritten. The fallback now writes with SET XX GET, which
returns the value it replaced, and writes a replaced cancel back. A Redis
older than 6.2 has no SET GET and keeps the plain SET

* fix(responses): keep a cancel on Redis older than 6.2 without scripts

Redis before 6.2 has no SET GET, so the no-scripts fallback still wrote with a
plain SET and could overwrite a cancel. RedisCache now answers that error with
GETSET, deleting a key it recreated and setting the TTL again, so the polling
handler always gets back the value it replaced and restores a cancel on every
Redis version

* fix(responses): set the TTL in the same transaction as GETSET on old Redis

On Redis older than 6.2 the no-scripts fallback sent GETSET and then EXPIRE as
two calls, so a link that dropped between them left the response with no TTL.
Both now go in one MULTI/EXEC, and a cluster client that refuses transactions
sends them in one pipeline

* fix(responses): log only the error type when a polling state write fails

On Redis older than 6.2, redis-py puts the whole GETSET command into a refused pipeline's error, and that command holds the stored response. The polling handler logged the error text at ERROR, so model output and instructions reached the logs without debug logging. It now logs only the error's type

* chore(ci): start the smoke workflow that never ran at 0cd0bff8a2

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-18 14:33:39 +00:00
..
__init__.py
test_response_polling_handler.py fix(responses): stop the background stream when a polling response is cancelled (#49384) 2026-11-18 14:33:39 +00:00