fix(db): add mcp_namespaced_tool_name to daily spend sort key#17261
fix(db): add mcp_namespaced_tool_name to daily spend sort key#17261mfittko wants to merge 1 commit intoBerriAI:mainfrom
Conversation
Adds mcp_namespaced_tool_name to the sort key in _update_daily_spend to ensure consistent ordering across all indexed fields. This prevents potential deadlocks when batching upserts. The mcp_namespaced_tool_name field is part of the unique constraint and was missing from the sort key, which could cause issues when this field contains None values. Also updates the test to include a case for mcp_namespaced_tool_name with a None value.
|
@mfittko is attempting to deploy a commit to the CLERKIEAI Team on Vercel. A member of the Team first needs to authorize it. |
|
Hey @CAFxX 👋 This PR is a follow-up to your excellent deadlock prevention work in #15281. You mentioned in the original PR that the sort key should include "all fields that have an index" to ensure consistent ordering across concurrent transactions. After MCP tool support was added (via #12397 and #12894), the However, it was never added to the sort key in This PR simply adds the missing field to the sort key tuple, using the same Would appreciate your review if you have a moment! 🙏 |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Title
fix(db): add mcp_namespaced_tool_name to daily spend sort key
Relevant issues
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitScreenshot of tests passing
All 12 tests in
test_db_spend_update_writer.pypass ✅Type
🐛 Bug Fix
Changes
Problem
The
mcp_namespaced_tool_namefield is part of the unique constraint (e.g.,user_id_date_api_key_model_custom_llm_provider_mcp_namespaced_tool_name) but was missing from the sort key in the_update_daily_spendmethod.When transactions with different values for this field are processed in non-deterministic order, it can lead to database deadlocks during concurrent upsert operations.
Solution
Add
mcp_namespaced_tool_nameto the sort key tuple in_update_daily_spend, using the samex[1].get("mcp_namespaced_tool_name") or ""pattern already used for other optional fields.Files Changed
litellm/proxy/db/db_spend_update_writer.py- Addedmcp_namespaced_tool_nameto sort keytests/test_litellm/proxy/db/test_db_spend_update_writer.py- Added test case withmcp_namespaced_tool_name: None