Repository navigation
fix(proxy): delete large teams without per-member transaction fan-out - #42998
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…ric on delete Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
bugbot run |
|
@greptileai Please review the latest commit again. Both previous findings have fixes, and their review threads are now resolved |
|
bugbot run Please review the current PR tip again, including email-only member cache invalidation and the deleted team metric reset |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
bugbot run |
|
@greptileai review the latest commit |
The unbounded-IN lint flagged the case-insensitive email lookup added for /team/delete cache eviction. chunked_in.find_many_in cannot carry Prisma's insensitive mode, so the repository slices the deduplicated list into IN_LIST_CHUNK_SIZE statements itself and concatenates the pages. Empty input still returns () without a query.
|
@greptileai re review |
The audit sent {"team_ids": [T, T]}: main answered 400 "User not found in
team" after deleting the keys and memberships and writing two tombstones,
leaving the team row behind; this branch answered 200 but still wrote the
tombstone, audit row and eviction twice. DeleteTeamRequest now collapses
repeated ids in order, so every later step sees each team once and the
response lists each deleted team once.
…concurrent teams Thirty-eight deterministic cells in tests/integration/management/ (the CircleCI integration-management group) covering the /team/delete happy, sad, edge and chaos rows: 250 members against a pool limit of five on two workers, the advisory-lock wait, email-only legacy roster entries in every casing, member and team cache eviction on both proxies for every client and endpoint, the Prometheus gauge, audit rows, malformed and duplicate input, the route gate, and a worker kill, a Redis outage and a proxy restart mid-burst. Every cell runs against the real proxy, Postgres and Redis with the scripted upstream; no component is mocked. On the merge base the rows this fix changes are red (P2028 on the 250-member team, two lock waiters, case-mismatched email lookups, duplicate ids, orphaned LiteLLM_UserTable.teams references under a concurrent burst); on the tip every cell is green twice with identical selections. Two pre-existing behaviours are pinned as observed rather than fixed here: a roster entry with neither user_id nor user_email answers 500, and the LiteLLM_DeletedTeamTable row is committed before the locked transaction, so a delete that dies in between leaves a tombstone for a live team and the retry adds a second.
|
@greptileai re review |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 4dc8f28. Configure here.
|
@greptileai re review |
|
@greptileai re review |
The three chaos cells applied the outage once three deletes had answered, which on a fast run let the whole burst finish before the worker kill, Redis stop or SIGTERM landed, so the cells passed without exercising the failure. Each cell now holds the first team's advisory lock from a test-owned transaction, waits until that team's delete is queued behind it in Postgres with its request unanswered, applies the outage, and only then releases the lock, so an in-flight delete meets the failure on every run and both legs. The pinned team's outcome and the number of deletes answered before the outage are recorded as junit properties (pinned_delete, answered_before_outage).
|
@greptileai re review |
|
bugbot run |
Bugbot is paused — on-demand spend limit reachedBugbot uses usage-based billing for this team and has hit its on-demand spend limit. A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue. |
TLDR
Problem this solves:
/team/deleteon a team with more members than the DB pool failsHow it solves it:
_team_member_deletefan-out, every DB effect is already covered by the team-scoped key delete and the locked sweepUserRepository.find_by_emailslookup, sliced to bounded IN statements), then evict their caches once after the team rows are goneteam_idsso a team is deleted once (main answers 400 after half of the delete already ran)User Flow
Before: an admin deleting a large team gets an error and the team stays around
{"team_ids": ["<250-member team>"]}no_db_connectionon some setups) quoting Prisma P2028 "Unable to start a transaction in the given time"After: the same delete finishes in one pass
{"team_ids": ["<250-member team>"]}{"deleted_teams": ["<id>"]}in about a secondLinear ticket
Resolves LIT-8533
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Audit run of 2026-09-30. Head
043f7e3169f8f3260f6b4fcc4ca47dfc04fbb31c(the last product commit; the tests commits4dc8f28c883fb9ecd6bc4c79e3247d29962bd20dand328df38da82d6000573042945a705012a995516fon top add onlytests/integration/management/) against merge base8afabe81f17a778bbb006413e6039144cb938dbc. Each leg is the CircleCIintegration-managementshape: real Postgres and Redis, the scripted upstream, a primary and a peer proxy with two uvicorn workers each,LITELLM_DISABLE_NO_REDIS_WARNING=true, no--detailed_debug; nothing inside the proxy is mocked. Cells that need their own proxy (pool limit 5, Prometheus, audit logs, chaos) boot one from the same commit. CI: on4dc8f28c88therun-cilabel's CircleCIintegrationworkflow (https://app.circleci.com/workflow/a3217444-3576-496a-8401-6a43ae554ef9) ran these cells inintegration-management(job 2231402) andintegration-management-replica(job 2231406): every one of the 38 cells green in both jobs (432 passed, 7 skipped, 1 failed per job). The one failure in each istests/integration/authorization/test_team_admin_gate.py::test_status_code[team_update_budget_permitted[team_admin]], main's own fixture that raised a team budget as a team admin (403 by design); #43788 repaired it on main after this PR's merge base, and this diff does not touch it. On328df38da8(this tip) the re-applied label's workflow (https://app.circleci.com/workflow/f1a53e91-89c1-4e6a-8f71-dbb220025ce8) has bothintegration-managementjobs green (20:34Z and 20:36Z). The Buildkitelitellm-e2e-prbuild on4dc8f28c88runstests/e2eonly; its 13 failures are the same fleet-wide reds as the neighbouring PR builds.Run ids:
final-043f7e3-head-1,final-043f7e3-head-2(identical 38-cell selections, 38 passed each, no skips, no retries),final-043f7e3-base-1(38 collected, the rows this fix changes red). Command per leg:Node ids are under
tests/integration/management/<file>::.test_delete_small_team_removes_rows_keys_tombstone_and_cachetest_delete_250_member_team_succeeds_with_pool_limit_five_on_two_workerstest_delete_waits_for_the_team_advisory_lock_and_completes_after_releasetest_team_delete_evicts_legacy_email_only_member_from_redis[exact-case]test_team_delete_evicts_legacy_email_only_member_from_redis[different-case]test_team_delete_evicts_member_cached_on_peer_and_peer_rehydrates_without_the_teamtest_team_delete_refuses_the_team_key_for_every_client_on_both_proxiestest_team_delete_rejects_the_deleted_key_before_the_response_cachetest_team_members_gauge_reads_roster_size_then_zero_after_deletetest_deleting_two_teams_sharing_a_member_in_one_call_clears_both_from_the_membertest_deleting_one_team_leaves_the_members_other_team_and_key_intacttest_team_admin_of_own_team_is_refused_by_the_route_gatetest_delete_writes_one_audit_row_for_the_team_and_one_per_keytest_rejects_malformed_team_ids[S1-unknown-id]test_empty_list_deletes_nothingtest_rejects_malformed_team_ids[S3-string-not-list]test_rejects_malformed_team_ids[S4-integer-item]test_rejects_malformed_team_ids[S5-empty-id]test_rejects_malformed_team_ids[S6-5kb-id]test_duplicate_ids_delete_oncetest_missing_authorization_is_401test_internal_user_outside_team_is_refused_by_the_route_gatetest_admin_of_another_team_is_refused_by_the_route_gatetest_roster_user_whose_row_was_removedtest_email_only_roster_entry_matching_no_usertest_email_only_roster_entry_matching_two_case_variantstest_roster_entry_without_id_or_email_pins_the_500test_member_add_and_delete_released_together_leave_no_team_referencetest_failed_delete_leaves_unrelated_key_servingtest_second_delete_of_the_same_team_is_404_with_one_tombstonetest_delete_empty_team_writes_tombstone_and_team_info_is_404test_delete_keys_only_team_removes_keys_and_revokes_themtest_team_delete_evicts_team_object_and_key_on_both_proxiestest_delete_three_teams_in_one_call_lists_all_and_tombstones_each_oncetest_recreating_the_same_team_id_after_delete_serves_the_fresh_teamtest_worker_killed_mid_burst_leaves_every_team_fully_deleted_after_retrytest_redis_stopped_mid_burst_keeps_deletes_answering_200test_proxy_terminated_mid_burst_then_restarted_leaves_every_team_fully_deletedRed on the base, green on the head, are exactly the rows the fix changes: P2028 on the 250-member team (H2), two sessions queued on the lock (H3), the case-mismatched email lookup (H4b, S13), duplicate ids (S7), and orphaned
LiteLLM_UserTable.teamsreferences left by concurrent deletes under an outage (C2, C3). Every other row is green on both legs.Type
🐛 Bug Fix
Caveats (if any)
Low
team_idsnow collapse to one delete and one tombstone; main answered 400 "User not found in team" after the keys and memberships were already gone and wrote two tombstonesPre-existing, on main and on this branch, outside this diff (left as-is, not addressed in this PR)
LiteLLM_DeletedTeamTablerow is committed before the locked transaction that removes the team, so a delete that dies in between leaves a tombstone for a live team and the retry writes a second one; the chaos cells pin one tombstone per attempt and record the teams asdouble_tombstonesuser_idnoruser_emailanswers 500 (LiteLLM_TeamTable.model_validateoutside the 404 handler); pinned bytest_roster_entry_without_id_or_email_pins_the_500spend:team:<id>,{team:<id>}:tokensand{model_per_team:<id>:<model>}:tokensstay in Redis after the deleteChaos overlap fix, tests commit
328df38da82d6000573042945a705012a995516f(no product diff). Greptile's review of4dc8f28c88noted that the three chaos cells applied the outage once three deletes had answered, which on a fast run let the whole burst finish before the failure landed. Each cell now holds the first team's advisory lock (pg_advisory_xact_lock(hashtext(team_id))) from a test-owned transaction before the burst, waits until that team's delete is queued behind it inside Postgres with its request unanswered, applies the outage (worker SIGKILL, Redis stop, proxy SIGTERM), and only then releases the lock; the pinned team's outcome and the number of deletes answered before the outage are recorded as junit properties (pinned_delete,answered_before_outage). Reruns of the whole 38-cell selection at this tip on the same rig shape (head product code043f7e3169, merge base8afabe81f1):chaos-overlap-1(head, the three chaos cells, 3 passed),overlap-head-full-1(head, 38 collected, 38 passed, no skips, no retries),overlap-base-full-1(base, 38 collected, 32 passed, 6 failed:test_team_delete_chaos::test_redis_stopped_mid_burst_keeps_deletes_answering_200,test_team_delete_inputs::test_duplicate_ids_delete_once,test_team_delete_inputs::test_email_only_roster_entry_matching_two_case_variants,test_team_delete_large_membership::test_delete_250_member_team_succeeds_with_pool_limit_five_on_two_workers,test_team_delete_large_membership::test_delete_waits_for_the_team_advisory_lock_and_completes_after_release,test_team_delete_member_cache_eviction::test_team_delete_evicts_legacy_email_only_member_from_redis[different-case]). The base reds are the five rows this fix changes (H2 P2028 on the 250-member team, H3 queued lock sessions, S7 duplicate ids, S13 and H4b case-mismatched email eviction) plus C2, where the merge base's per-member fan-out left one team'sLiteLLM_UserTable.teamsreference orphaned under the pinned overlap (1 of 24, the lost-update race the PR removes); every other cell is green on both legs..chaos-overlap-1: C1 PASS: 5 deletes answered before the outage, pinned delete RemoteProtocolError, split {'200': 5, 'RemoteProtocolError': 19}, 18 rows present before the retry, 18 half-deleted, 18 double tombstoneschaos-overlap-1: C2 PASS: 9 deletes answered before the outage, pinned delete 200, split {'200': 24}, 0 rows present before the retry, 0 half-deleted, 0 double tombstoneschaos-overlap-1: C3 PASS: 7 deletes answered before the outage, pinned delete 200, split {'200': 24}, 0 rows present before the retry, 0 half-deleted, 0 double tombstonesoverlap-head-full-1: C1 PASS: 7 deletes answered before the outage, pinned delete RemoteProtocolError, split {'200': 7, 'RemoteProtocolError': 17}, 16 rows present before the retry, 16 half-deleted, 16 double tombstonesoverlap-head-full-1: C2 PASS: 7 deletes answered before the outage, pinned delete 200, split {'200': 24}, 0 rows present before the retry, 0 half-deleted, 0 double tombstonesoverlap-head-full-1: C3 PASS: 7 deletes answered before the outage, pinned delete 200, split {'200': 24}, 0 rows present before the retry, 0 half-deleted, 0 double tombstonesoverlap-base-full-1: C1 PASS: 3 deletes answered before the outage, pinned delete RemoteProtocolError, split {'200': 3, 'RemoteProtocolError': 21}, 21 rows present before the retry, 21 half-deleted, 21 double tombstonesoverlap-base-full-1: C2 FAIL: 4 deletes answered before the outage, pinned delete 200, split {'200': 24}, 0 rows present before the retry, 1 half-deleted, 0 double tombstonesoverlap-base-full-1: C3 PASS: 4 deletes answered before the outage, pinned delete 200, split {'200': 24}, 0 rows present before the retry, 0 half-deleted, 0 double tombstonesLink to Devin session: https://app.devin.ai/sessions/0d1ac22012ca46ab97bab2d26f085a82
Open in Devin Desktop: https://app.devin.ai/desktop/session/0d1ac22012ca46ab97bab2d26f085a82?variant=devin
Note
Medium Risk
Changes core team deletion and auth cache invalidation for all deletes; behavior is heavily tested but touches advisory locks, transactions, and Redis eviction paths used on every team removal.
Overview
Fixes
/team/deletefailing with Prisma P2028 when a team has more members than the DB connection pool, by removing the oldasyncio.gatherof_team_member_delete(one transaction per member, all queued on the team advisory lock). Membership and userteamscleanup still run through the existing_sweep_deleted_team_referencespath inside a single locked transaction per delete.After the team rows are removed, the handler now resolves member user ids (including legacy email-only roster entries via new
UserRepository.find_by_emails, case-insensitive and chunked for Postgres IN limits), then evicts member auth cache and team spend state in one pass.DeleteTeamRequestdedupesteam_idsso repeated ids do not double-delete or double-tombstone.litellm_team_members_metricis reset to 0 on delete.Adds e2e/integration coverage for 250-member deletes, duplicate ids, cache eviction across peers, chaos mid-burst, and Prometheus gauge behavior.
Reviewed by Cursor Bugbot for commit 4dc8f28. Bugbot is set up for automated code reviews on this repo. Configure here.