mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-10 03:28:53 +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> |
||
|---|---|---|
| .. | ||
| a2a_protocol | ||
| anthropic_interface/exceptions | ||
| caching | ||
| completion_extras | ||
| containers | ||
| enterprise | ||
| expected_fine_tuning_api | ||
| expected_responses_api_request | ||
| experimental_mcp_client | ||
| google_genai | ||
| images | ||
| integrations | ||
| interactions | ||
| litellm_core_utils | ||
| llms | ||
| ocr | ||
| passthrough | ||
| proxy | ||
| responses | ||
| router_strategy | ||
| router_utils | ||
| secret_managers | ||
| test_router | ||
| types | ||
| vector_stores | ||
| __init__.py | ||
| conftest.py | ||
| log.txt | ||
| readme.md | ||
| test_a2a_registry_lookup.py | ||
| test_acompletion_session_reuse_e2e.py | ||
| test_add_deployment_no_master_key.py | ||
| test_aembedding_session_reuse_e2e.py | ||
| test_anthropic_beta_headers_filtering.py | ||
| test_anthropic_skills_transformation.py | ||
| test_azure_video_router.py | ||
| test_chat_ui_responses_session.py | ||
| test_claude_haiku_4_5_config.py | ||
| test_claude_opus_4_6_config.py | ||
| test_constants.py | ||
| test_container_router.py | ||
| test_cost_calculation_log_level.py | ||
| test_cost_calculator.py | ||
| test_count_tokens_public_api.py | ||
| test_deepseek_model_metadata.py | ||
| test_eager_tiktoken_load.py | ||
| test_exception_exports.py | ||
| test_exception_header_preservation.py | ||
| test_exception_mapping_request_attribute.py | ||
| test_filter_out_litellm_params.py | ||
| test_get_blog_posts.py | ||
| test_gpt_image_cost_calculator.py | ||
| test_groq_streaming_encoding.py | ||
| test_lazy_imports.py | ||
| test_litellm_params_reserved_keys.py | ||
| test_logging.py | ||
| test_lowest_latency_zero_tokens.py | ||
| test_main.py | ||
| test_model_cost_aliases.py | ||
| test_model_param_helper.py | ||
| test_model_response_normalization.py | ||
| test_nested_drop_params.py | ||
| test_project_alias_tracking.py | ||
| test_project_tags_pydantic.py | ||
| test_redis.py | ||
| test_register_model_custom_pricing.py | ||
| test_responses_api_bridge_non_stream.py | ||
| test_responses_id_security.py | ||
| test_router.py | ||
| test_router_google_genai.py | ||
| test_router_model_cost_isolation.py | ||
| test_router_order_fallback.py | ||
| test_router_per_deployment_num_retries.py | ||
| test_router_redis_init.py | ||
| test_router_retry_non_retryable_errors.py | ||
| test_router_silent_experiment.py | ||
| test_secret_redaction.py | ||
| test_service_logger.py | ||
| test_setup_wizard.py | ||
| test_shared_session_integration.py | ||
| test_ssl_verify_unit.py | ||
| test_stream_chunk_builder_annotations.py | ||
| test_streaming_connection_cleanup.py | ||
| test_system_message_format_bug.py | ||
| test_utils.py | ||
| test_uuid_helper.py | ||
| test_video_generation.py | ||
| test_xai_responses_auto_routing.py | ||
Testing for litellm/
This directory 1:1 maps the the litellm/ directory, and can only contain mocked tests.
The point of this is to:
- Increase test coverage of
litellm/ - Make it easy for contributors to add tests for the
litellm/package and easily run tests without needing LLM API keys.
File name conventions
litellm/proxy/test_caching_routes.pymaps tolitellm/proxy/caching_routes.pytest_<filename>.pymaps tolitellm/<filename>.py