Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(cli): stop asking a keychain that already stopped answering

Mateo5099379e0345
diff --git a/litellm/litellm_core_utils/cli_keyring.py b/litellm/litellm_core_utils/cli_keyring.py
index 8da3e5226d4..70b1773739d 100644
--- a/litellm/litellm_core_utils/cli_keyring.py
+++ b/litellm/litellm_core_utils/cli_keyring.py
@@ -18,7 +18,7 @@ throwaway value, because a keychain can answer neither way and block forever.
import os
import threading
from contextlib import suppress
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from typing import Final, Protocol, TypeAlias
KEYRING_SERVICE: Final = "litellm-cli"
@@ -152,11 +152,20 @@ def _forget_the_preflight(api: KeyringApi) -> None:
@dataclass(frozen=True, slots=True)
class KeyringVault:
- """The OS keychain, reached through the optional `keyring` package."""
+ """The OS keychain, reached through the optional `keyring` package.
+
+ A keychain that let the pre-flight time out is not asked anything else for the rest of the
+ process. The probe that timed out is still sitting in the keychain on a thread of its own, and
+ it holds the keychain against every later call, so the read after it would block on the main
+ thread with no timeout to save it. One silence is answer enough.
+ """
preflight_timeout_seconds: float = _PREFLIGHT_TIMEOUT_SECONDS
+ stopped_answering: threading.Event = field(default_factory=threading.Event, compare=False, repr=False)
def read(self) -> SecretRead:
+ if self.stopped_answering.is_set():
+ return KeyringUnreachable()
api: Final = _keyring_api()
if isinstance(api, (KeyringNotInstalled, KeyringDisabled)):
return api
@@ -177,10 +186,13 @@ class KeyringVault:
The keychain is pre-flighted first, because one that blocks rather than answering would
otherwise hang `lite login` outright.
"""
+ if self.stopped_answering.is_set():
+ return KeyringUnreachable()
api: Final = _keyring_api()
if isinstance(api, (KeyringNotInstalled, KeyringDisabled)):
return api
if not _answers_a_write(api, self.preflight_timeout_seconds):
+ self.stopped_answering.set()
return KeyringUnreachable()
_forget_the_preflight(api)
try:
diff --git a/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py b/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py
index b8fcf3618cf..c6fee6d0c25 100644
--- a/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py
+++ b/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py
@@ -832,6 +832,26 @@ class _NeverAnsweringKeyringModule(_FakeKeyringModule):
threading.Event().wait()
+class _KeychainHeldByABlockedWrite(_NeverAnsweringKeyringModule):
+ """The same keychain, plus what the blocked write does to everything after it: the stuck call
+ holds the keychain, so every later read blocks behind it too."""
+
+ def get_password(self, service_name, username):
+ self.calls.append(("get", service_name, username))
+ if self.blocked.is_set():
+ threading.Event().wait()
+ return self.stored
+
+
+def _answered_within(seconds, call):
+ answers = []
+ worker = threading.Thread(target=lambda: answers.append(call()), daemon=True)
+ worker.start()
+ worker.join(seconds)
+ assert not worker.is_alive(), f"{call.__qualname__} never returned"
+ return answers[0]
+
+
@pytest.fixture
def install_fake_keyring(monkeypatch):
def _install(fake):
@@ -927,6 +947,33 @@ class TestKeyringVault:
assert [call[2] for call in fake.calls] == [KEYRING_PREFLIGHT_ACCOUNT]
+ def test_a_keychain_that_stopped_answering_is_not_asked_again(self, install_fake_keyring):
+ """The write that timed out is still holding the keychain when we give up on it, so the
+ call after it is the one that hangs, and read has nothing to time out against. Anything
+ resolving the credential more than once in a process hits that: an SDK client built twice
+ pays the pre-flight timeout on the first build and never returns from the second."""
+ install_fake_keyring(_KeychainHeldByABlockedWrite())
+ vault = KeyringVault(preflight_timeout_seconds=0.05)
+
+ assert vault.write("blob-1") == KeyringUnreachable()
+
+ assert _answered_within(5, vault.read) == KeyringUnreachable()
+ assert _answered_within(5, vault.erase) == KeyringUnreachable()
+ assert _answered_within(5, lambda: vault.write("blob-2")) == KeyringUnreachable()
+
+ def test_a_keychain_that_stopped_answering_leaves_the_credential_in_the_file(
+ self, isolated_home, install_fake_keyring
+ ):
+ """The end of the same story: giving up on the keychain has to leave a login that still
+ works, and loading it back must not go asking the keychain that already stopped answering."""
+ install_fake_keyring(_KeychainHeldByABlockedWrite())
+ vault = KeyringVault(preflight_timeout_seconds=0.05)
+
+ outcome = save_cli_token(CliTokenRecord(base_url=SERVER, key="sk-only-copy"), vault=vault)
+
+ assert outcome == KeyringUnreachable()
+ assert _answered_within(5, lambda: load_cli_token(vault=vault)).key == "sk-only-copy"
+
def test_a_login_survives_a_keychain_that_never_answers(self, isolated_home, install_fake_keyring):
"""The end of the same story: the credential still has to be usable afterwards."""
install_fake_keyring(_NeverAnsweringKeyringModule())