mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-27 01:22:18 +00:00
Reported error during device-code login: Tokens obtained but DB persist failed: <asyncio.locks.Event object at 0x... [unset]> is bound to a different event loop Root cause: the background worker was a ``threading.Thread`` that ran ``asyncio.run(persist_credential_to_db(item))``. That creates a fresh event loop in the worker thread, but ``prisma_client``'s internal asyncio primitives (locks, futures) are bound to the proxy's main loop. Awaiting a prisma call from the worker loop hits the cross-loop error. Fix: - Refactor the background flow from a thread to an asyncio task (``asyncio.create_task``) scheduled on the proxy's main loop. Blocking IO in the flow (device-code poll, token exchange) runs in ``loop.run_in_executor``. The DB persist step (``await persist_credential_to_db(item)``) now naturally shares a loop with prisma_client. Same treatment for both ChatGPT and Copilot endpoints. - For the DBAuthenticator refresh path (called from sync ``validate_environment`` during a request, same cross-loop risk), add ``_register_proxy_main_loop`` + ``_schedule_db_persist`` now prefers ``asyncio.run_coroutine_threadsafe`` onto the registered loop. ``/chatgpt/oauth/start`` and ``/copilot/oauth/start`` register the loop on invocation (guaranteed to be the proxy's main loop). Falls back to the old thread + ``asyncio.run`` path only when no loop is registered (CLI / tests, where prisma_client is ``None`` anyway). Tests updated: ``TestBackgroundWorker`` now drives the async task directly via ``await _run_device_code_flow_async(...)``. The ``test_creates_session_and_spawns_worker`` test stubs the task to a no-op instead of patching ``threading.Thread``. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| _experimental/mcp_server | ||
| agent_endpoints | ||
| anthropic_endpoints | ||
| auth | ||
| chatgpt_oauth_endpoints | ||
| client | ||
| common_utils | ||
| copilot_oauth_endpoints | ||
| db | ||
| discovery_endpoints | ||
| experimental/mcp_server | ||
| google_endpoints | ||
| guardrails | ||
| health_endpoints | ||
| hooks | ||
| image_endpoints | ||
| management_endpoints | ||
| management_helpers | ||
| middleware | ||
| openai_files_endpoint | ||
| pass_through_endpoints | ||
| policy_engine | ||
| prompts | ||
| public_endpoints | ||
| rag_endpoints | ||
| realtime_endpoints | ||
| response_api_endpoints | ||
| spend_tracking | ||
| test_configs | ||
| ui_crud_endpoints | ||
| vector_store_endpoints | ||
| __init__.py | ||
| conftest.py | ||
| test_aiohttp_cleanup_closed.py | ||
| test_aiohttp_session_recovery.py | ||
| test_api_key_masking_in_errors.py | ||
| test_audio_speech_prometheus_hooks.py | ||
| test_batch_expiry.py | ||
| test_batch_metadata_none_fix.py | ||
| test_caching_routes.py | ||
| test_chat_completion_metadata.py | ||
| test_common_request_processing.py | ||
| test_custom_proxy.py | ||
| test_empty_model_list.py | ||
| test_enforce_user_param.py | ||
| test_fallback_management_endpoints.py | ||
| test_fastapi_offline_routes.py | ||
| test_health_check_functions.py | ||
| test_health_check_max_tokens.py | ||
| test_litellm_pre_call_utils.py | ||
| test_max_budget_env_var.py | ||
| test_model_dump_with_preserved_fields.py | ||
| test_model_id_header_propagation.py | ||
| test_model_info_default_limits.py | ||
| test_model_level_guardrails.py | ||
| test_openapi_schema_validation.py | ||
| test_prometheus_cleanup.py | ||
| test_proxy_cli.py | ||
| test_proxy_server.py | ||
| test_proxy_types.py | ||
| test_proxy_utils.py | ||
| test_pyroscope.py | ||
| test_response_model_sanitization.py | ||
| test_route_a2a_models.py | ||
| test_route_llm_request.py | ||
| test_shared_health_check.py | ||
| test_spend_log_cleanup.py | ||
| test_swagger_chat_completions.py | ||
| test_team_member_update.py | ||
| test_tools_allowlist_enforcement.py | ||
| test_update_llm_router_resilience.py | ||