litellm/litellm/models/fallback.py
joshua-berri 2c7ed0713a
fix(fallback): preserve concurrent writes and serve saved configuration (#51245)
* fix(fallback): serialize fallback writes and wait for replicas on read-back

POST and DELETE /fallback now read, edit and write router_settings in one
writer transaction under the same per-param advisory lock the config section
writer takes, so concurrent writes on any replica no longer drop each other's
rules. The cache is invalidated and the router updated only after commit.

The Terraform provider now polls the read after a litellm_fallback create or
update until the proxy serves the written list, within new create/update
timeouts, instead of clearing the ID on a stale 404 or saving a stale list.
Refresh 404 still removes the resource, and error responses are not retried.

Fixes #47888

* fix(fallback): bound provider requests by the apply context and inject the fallback repository

The provider's fallback write and read-back requests now carry the apply
context through a context-aware MakeRequest variant, so a cancelled apply
sends nothing and the create/update timeout also ends a hung request.

The POST and DELETE /fallback routes take a RouterFallbackRepositoryDep that
returns a RouterFallbackStore. Router settings parsing moved into
RouterFallbackRepository, which edits the rules through the locked
ConfigRepository operation.

Unit tests now check the outcome of the race and rollback through a fake
lock instead of pinning the lock SQL. They also cover a racing config
section write, a 404 delete inside the lock, route resolution of the
repository and the no-database path.

* refactor(fallback): keep atomic edits in the config repository

* fix(terraform): release hung test handlers and audit MakeRequestWithContext calls

The hung-request cancellation test now releases its handler through a test
owned channel before closing the server, since the server does not notice
the client's cancel while the POST body is unread.

The endpoint audit now reads the method and path of MakeRequestWithContext
calls from their shifted argument positions, and skips only MakeRequest's own
forwarding call inside the helper files, so POST /fallback counts as covered
again and dynamic paths elsewhere still fail closed.

* test(fallback): keep stored-rule regressions with their helpers

* fix(fallback): read saved rules without Terraform changes

* fix(fallback): preserve valid rules when stored entries are malformed

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
2026-11-11 15:26:23 -08:00

25 lines
876 B
Python

from collections.abc import Callable, Sequence
from dataclasses import dataclass
from typing import Annotated, Final, Protocol, TypeAlias
from pydantic import Field, JsonValue, TypeAdapter
FallbackRule = dict[str, list[str]]
StoredFallback = Annotated[FallbackRule | dict[str, object] | str, Field(union_mode="left_to_right")]
STORED_FALLBACKS: Final = TypeAdapter(list[StoredFallback])
SavedFallbackRule: TypeAlias = JsonValue
@dataclass(frozen=True, slots=True)
class FallbackRulesEdit:
rules: Sequence[StoredFallback]
replaced: bool
FallbackRulesEditor: TypeAlias = Callable[[Sequence[StoredFallback]], FallbackRulesEdit]
class RouterFallbackStore(Protocol):
async def saved_rules(self, fallback_key: str) -> Sequence[SavedFallbackRule] | None: ...
async def edit_rules(self, fallback_key: str, edit: FallbackRulesEditor) -> FallbackRulesEdit: ...