Skip to content

test: deflake the JEV classifier select and two router tests that inherited leaked state - #44840

Merged
mateo-berri merged 2 commits into
mainfrom
litellm_deflake_20261006
Oct 6, 2026
Merged

mateo-berri merged 2 commits into
mainfrom
litellm_deflake_20261006

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Three tests flaked in CI in the last 24h, each one passing on a re-run at the same SHA
  • The JEV classifier UI test queried a select option before the popup had opened
  • Two router tests picked up work that earlier tests in the same worker had leaked

How it solves it:

  • The JEV test picks its model through the existing chooseSelectOption helper
  • The prompt-caching capture logger only records the request it sent itself
  • The max-parallel embedding test reaps earlier tests' garbage before it starts recording warnings
  • Only test code changes. No production code and no assertions changed

User Flow

Before: a contributor's unrelated PR or a main push goes red on a UI or unit shard and has to be re-run

  1. They push a commit that touches none of the three tests or the code under them
  2. ui-unit-tests, enterprise-routing or misc-dirs fails on one of the tests below
  3. They re-run the job with no changes and it passes

After: the same commit passes on the first attempt

  1. They push the same commit
  2. Each test now waits on a condition or ignores other tests' leftovers, so nothing left over from scheduling or earlier tests can change its result
  3. The job passes on the first attempt

Flake evidence (run date 2026-10-06)

src/components/add_model/JevClassifierConfig.integration.test.tsx > JEV classifier editor > preserves bespoke, custom tiers and context through save, reload and probe (2 flakes)

Failed on attempt 1 and passed on attempt 2 at the same SHA, both on main:
https://github.com/BerriAI/litellm/actions/runs/37388637599 (c561372) and https://github.com/BerriAI/litellm/actions/runs/37405618290 (4a963a6)

Mechanism: async interaction race. The test does await userEvent.click(trigger) and then calls the synchronous screen.getByRole("option", ...). The Base UI select popup opens and joins the accessibility tree asynchronously, so on a loaded runner the query runs while the trigger still has aria-expanded="false". Locally the file failed 1 run in 10 at the merge base, that time on the laya case with option multilingual, so every non-Jev case of the it.each is exposed

Fix: use chooseSelectOption from tests/test-utils.tsx, which the dashboard already uses for this race (a5f9410, eb53639). It awaits the click, finds the option with findByRole, waits for the positioner to stop blocking pointer events, and then clicks

tests/unit/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py::test_affinity_key_matches_the_messages_auto_caching_actually_sends (1 flake)

Failed on attempt 2 and passed on attempt 3 at the same SHA: https://github.com/BerriAI/litellm/actions/runs/37369495882 (da14262, job 111985880102)

CI failure: the capture returned [{'role': 'user', 'content': 'Hello!'}], which is not a message this test sends, and the test log shows LoggingWorker: event loop changed; carried 4 pending ... onto the new loop

Mechanism: shared module-level state between tests. GLOBAL_LOGGING_WORKER moves the success-logging tasks an earlier test left queued onto the next test's event loop. They run after this test has installed _SentMessagesCapture in litellm.callbacks, so the capture records another test's request, and _eventually returns whichever event lands first. Repro: a scratch test that leaves one acompletion logging task queued, followed by a capture-installing test, makes the capture receive both the foreign Hello! event and its own event on every run

Fix: _SentMessagesCapture(litellm_call_id) only records success events whose litellm_call_id matches. Both tests that use it now pass a fresh uuid4 as litellm_call_id to litellm.acompletion. test_pin_matches_when_the_success_event_truncated_an_image_payload has the same exposure and gets the same change

tests/unit/test_router/test_router.py::test_router_embedding_path_rejects_past_max_parallel_requests_without_orphan_coroutines (1 flake)

Failed on attempt 1 and passed on attempt 2 at the same SHA, on main: https://github.com/BerriAI/litellm/actions/runs/37438745150 (18c3118, job 112187131162)

CI failure: coroutine 'BaseRoutingStrategy.periodic_sync_in_memory_spend_with_redis' was never awaited, together with Task was destroyed but it is pending! task: <Task pending name='Task-325' ...>. This test creates no budget limiter, and the low task number points to a task an earlier test in the worker had created

Mechanism: GC state shared across tests. The test calls gc.collect() inside its warnings.catch_warnings(record=True) window. A full collection also reaps unreachable old-generation garbage left by earlier tests, so their never-awaited coroutines get attributed to this test. Repro: a scratch test that leaves an unawaited coroutine in old-generation cyclic garbage, run just before this test, makes it fail on every run with coroutine '_orphan' was never awaited

Fix: call gc.collect() once before the recording window opens. The collection inside the window and the assertion stay as they were, so coroutines this test orphans are still caught

Mutation checks

Every fixed test still fails when the behavior it covers is broken. Each mutation was reverted afterwards

  • JEV: asking for an option name that does not exist fails with Unable to find role="option" and name "bespokelabs/Bespoke-Nimble-9B-wrong"
  • Affinity: making messages_with_default_injections return the messages unchanged fails at assert routing_key is not None
  • Image pin: removing base64 truncation before cache-key hashing fails, because both deployments stay instead of only dep-2
  • Max parallel: removing the max-parallel admission guard fails at assert len(rejected) == 2 (0 == 2)

Flakes seen but not fixed in this PR

  • tests/unit/test_secret_redaction.py::test_redact_string_stays_sub_quadratic_on_a_long_adversarial_url, attempt 1 of https://github.com/BerriAI/litellm/actions/runs/37378002491: cost grew 18.8x for 4x the URL length against a ceiling of 11. This is a wall-clock ratio, and locally it already sits near 7x, so the ceiling has little headroom. It needs an operation-count measure rather than a tweak, so it is left for a human
  • tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py::test_batch_expires_at_the_completion_window_and_keeps_what_finished, attempt 1 of https://github.com/BerriAI/litellm/actions/runs/37414003000: completed=0 instead of 1. The 0.2s completion_window_seconds deadline is real wall-clock time, so on a loaded runner the first row can be cut off. The right fix is to inject a clock into LiteLLMExecutedBatchRunner, a production change left for a follow-up

Not flakes in the window

Real breakage: documentation fails on main and on this PR because DEFER_PYDANTIC_BUILD in litellm/constants.py has no docs entry (https://github.com/BerriAI/litellm/actions/runs/37448756259). It is not a required check and is left for a separate fix. Several PR-branch failures (component allowlist, seed tracing fixtures) failed on every attempt in files those PRs changed. Infra: 12 runner shutdowns and 4 job timeouts. None of these were touched

Other open deflake PRs

#43437, #42936 and #41219 each carry a human approval, so they are left untouched under the human-reviewed exception, and this PR is the new rolling PR

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<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
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

There is no proxy or Admin UI surface for these changes, so the proof is the flake reproduction itself, run with the same triggers at the merge base and at the tip. The ordered repros use an uncommitted scratch test that leaves behind exactly the leftovers seen in CI

Before (18c3118)

JEV select race

  1. Ran npx vitest run --project integration src/components/add_model/JevClassifierConfig.integration.test.tsx 10 times in ui/litellm-dashboard
  2. Run 7 failed with Unable to find an accessible element with the role "option" and name "multilingual", with the trigger at aria-expanded="false"

Leaked logging task

  1. Ran a scratch test that leaves an acompletion logging task queued, then a capture-installing test
  2. The capture received [{'content': 'Hello!', ...}] from the earlier test before its own event

Leaked garbage

  1. Ran a scratch test that leaves an unawaited coroutine in old-generation garbage, then the max-parallel embedding test, twice
  2. Both runs failed with coroutine '_orphan' was never awaited

After (d97bba1)

JEV select race

  1. Ran the same vitest command 30 times
  2. All 30 runs passed

Leaked logging task

  1. Ran the same scratch leak test before both capture-using tests, 5 times
  2. All 5 runs passed. The capture ignores the foreign event

Leaked garbage

  1. Ran the same scratch leak test before the max-parallel embedding test, 5 times
  2. All 5 runs passed
  • d97bba1 passes /audit (skipped: no product code on a request path, tests only)

Type

✅ Test

Caveats (if any)

Low

  • Test-only change, production behavior is untouched, so there is no end-user before/after to show
  • The secret redaction and batch expiry flakes above stay unfixed and need a human

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/a981af76fc76402dab98ee4ae041776d
Open in Devin Desktop: https://app.devin.ai/desktop/session/a981af76fc76402dab98ee4ae041776d?variant=devin


Note

Low Risk
Test-only changes with no impact on runtime behavior, auth, or data handling.

Overview
Stabilizes three CI flakes by isolating each test from async races and leftover state from other tests in the same worker. No production code or assertion changes.

The JEV classifier integration test now picks classifier models via chooseSelectOption, which waits for the select popup before clicking an option, instead of querying role="option" immediately after opening the trigger.

Prompt-caching tests give _SentMessagesCapture a litellm_call_id and only record success events for that id; both capture tests pass a fresh UUID through litellm.acompletion so leaked logging tasks from earlier tests cannot populate the capture first.

The max-parallel embedding router test runs gc.collect() once before the warnings capture window so garbage from prior tests is not attributed as orphan coroutines during this test’s assertion.

Reviewed by Cursor Bugbot for commit d97bba1. Bugbot is set up for automated code reviews on this repo. Configure here.

Base UI exposes its select option asynchronously, and the helper waits for the option and its positioner to become clickable before selection

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Collect earlier tests' garbage before warning capture so their unawaited coroutines cannot be attributed to the target's warning assertion

Filter success-event callbacks by the request's litellm_call_id so queued logging work cannot replace the current request's captured messages

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 d97bba1. Configure here.

@greptile-apps

greptile-apps Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Low risk] Test fixes for flaky test isolation.

The PR appears safe to merge; no actionable regression was identified.

Summary

The PR changes only tests to reduce three scheduling- and shared-state-dependent flakes:

  • Waits for classifier model options before selecting them.
  • Correlates captured logging events with each test’s own call.
  • Collects earlier tests’ garbage before recording orphan-coroutine warnings.

Reviews (1) · Last reviewed commit: "test: isolate two router tests from work..."

@codecov

codecov Bot commented Oct 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mateo-berri
mateo-berri merged commit 10444df into main Oct 6, 2026
98 of 101 checks passed
@mateo-berri
mateo-berri deleted the litellm_deflake_20261006 branch October 6, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants