Skip to content

fix(proxy): kill the whole prisma process group when a boot migration command times out - #39509

Merged
yassin-berriai merged 2 commits into
litellm_internal_stagingfrom
litellm_prisma_killpg_proxy_db
Sep 8, 2026
Merged

yassin-berriai merged 2 commits into
litellm_internal_stagingfrom
litellm_prisma_killpg_proxy_db

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Boot's prisma db push timeout killed only the wrapper process
  • Node and the schema engine survived, still writing the schema
  • Four retries stacked four live engines on one database
  • Boot's prisma migrate diff had no timeout at all
  • A busy database hung boot forever with zero log output

How it solves it:

  • Both call sites now go through run_prisma
  • It starts prisma in its own process group
  • A timeout SIGKILLs the whole group, engine included
  • migrate diff gets the same budget, overridable per deployment

Based on litellm_prisma_timeout_killpg (#39466), which adds run_prisma, rather than duplicating
the helper. Retarget to litellm_internal_staging once that merges.

That base predates #39478, so ci/circleci: build_docker_database_image fails here on the dashboard
type check for tests/mocks/autoRouterPresets.ts. Nothing in this PR touches the UI, and staging
already carries the fix.

User Flow

Before: an operator restarting the proxy while their database is busy gets a proxy that never
becomes ready and leaves prisma processes running on the host after they stop it.

  1. They start the proxy with --use_prisma_db_push against a Postgres where a long-running
    migration or analytics transaction holds a lock the schema update needs
  2. The proxy log prints Attempt 1 timed out through Attempt 4 timed out, then
    LiteLLM Proxy: Database migration failed but continuing startup.
  3. They stop the proxy the way a Kubernetes pod delete does, with SIGTERM
  4. ps -eo pid,ppid,command | grep schema-engine still lists a schema-engine-* process owned by
    pid 1, still connected to their database and still writing the schema
  5. They try the other boot path instead, DISABLE_SCHEMA_UPDATE=true, and the proxy prints nothing
    at all: the log file stays 0 bytes and
    curl http://127.0.0.1:<port>/health/liveliness fails with exit 7 for as long as they wait
  6. They SIGTERM that proxy too, and ps shows the whole prisma migrate diff chain surviving with
    ppid 1

After: the same restart gives up on schedule, comes up healthy on the second path, and leaves
nothing running behind it.

  1. They start the proxy with --use_prisma_db_push against the same busy Postgres
  2. The proxy log prints Attempt 1 timed out after 60s through Attempt 4 timed out after 60s,
    then LiteLLM Proxy: Database migration failed but continuing startup.
  3. They stop the proxy with SIGTERM
  4. ps -eo pid,ppid,command | grep schema-engine lists nothing
  5. They try DISABLE_SCHEMA_UPDATE=true, and after 60 seconds the proxy prints
    Timed out after 60.0s generating the migration diff. Raise LITELLM_PRISMA_COMMAND_TIMEOUT if this database needs longer. and finishes booting, so
    curl http://127.0.0.1:<port>/health/liveliness returns 200
  6. They SIGTERM that proxy, and ps shows no surviving prisma processes

Relevant issues

Linear ticket

Resolves LIT-6801

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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
  • 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)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Shared setup, identical on both sides: a real Postgres, a real Anthropic key, and a table lock that
keeps the schema update from finishing inside its budget, which is what a live migration or a long
analytics transaction does on a real deployment. Both sides boot with --num_workers 2.

docker run -d --name lit6801-pg -p 5459:5432 \
  -e POSTGRES_USER=lit6801 -e POSTGRES_PASSWORD=lit6801 -e POSTGRES_DB=lit6801 postgres:16
export DATABASE_URL='postgresql://lit6801:lit6801@127.0.0.1:5459/lit6801'

cat config.yaml
model_list:
  - model_name: anthropic-haiku-4-5
    litellm_params:
      model: anthropic/claude-haiku-4-5
      api_key: os.environ/ANTHROPIC_API_KEY
general_settings:
  master_key: sk-lit6801

docker exec lit6801-pg psql -U lit6801 -d lit6801 \
  -c 'ALTER TABLE "LiteLLM_SpendLogs" DROP COLUMN "session_id";'
docker exec lit6801-pg psql -U lit6801 -d lit6801 \
  -c 'BEGIN; LOCK TABLE "LiteLLM_SpendLogs" IN ACCESS EXCLUSIVE MODE; SELECT pg_sleep(5400); COMMIT;' &

Before (9f4fe31)

prisma db push

  1. Boot the proxy on the legacy push path.
$ python litellm/proxy/proxy_cli.py --config config.yaml --port 18831 --num_workers 2 --use_prisma_db_push
  1. All four attempts burn their budget and boot moves on.
21:56:36 - LiteLLM Proxy:WARNING: prisma_client.py:956 - Attempt 1 timed out
21:57:49 - LiteLLM Proxy:WARNING: prisma_client.py:956 - Attempt 2 timed out
21:59:01 - LiteLLM Proxy:WARNING: prisma_client.py:956 - Attempt 3 timed out
22:00:12 - LiteLLM Proxy:WARNING: prisma_client.py:956 - Attempt 4 timed out
LiteLLM Proxy: Database migration failed but continuing startup.
  1. Stop the proxy the way a pod delete does, then look for prisma on the host.
$ kill -TERM 48225
$ ps -eo pid,ppid,command | grep schema-engine
63122     1 .../bin/node .../prisma/build/index.js db push --accept-data-loss --skip-generate
63297 63122 .../prisma/5.4.2/.../schema-engine-darwin-arm64

The engine is still running under pid 1, still connected to the database and still writing the
schema.

prisma migrate diff

  1. Boot the proxy on the other path, with the schema update disabled.
$ DISABLE_SCHEMA_UPDATE=true python litellm/proxy/proxy_cli.py --config config.yaml --port 18847 --num_workers 2
  1. Nothing happens: the proxy never becomes ready and prints nothing at all.
$ curl -s -m 5 -o /dev/null -w '%{http_code}\n' http://127.0.0.1:18847/health/liveliness
000
curl exit=7
$ wc -c lit6801-diff-before.log
       0
  1. Stop it after 12 minutes of hanging and look for prisma on the host.
$ kill -TERM 21350
$ ps -eo pid,ppid,command | grep -E 'schema-engine|migrate diff'
22110     1 .../.venv/bin/prisma migrate diff --from-url postgresql://... --to-schema-datamodel ./schema.prisma --script
22427 22110 .../prisma/build/index.js migrate diff --from-url postgresql://... --script
22428 22427 .../@prisma/engines/schema-engine-darwin-arm64
  1. The four new tests, run against the merge base 9f4fe3144a with only the test files copied over,
    fail there for the reasons this PR fixes.
$ PYTHONPATH=$PWD .venv/bin/python -m pytest -p no:randomly -q --tb=line \
    tests/test_litellm/proxy/db/test_check_migration.py::test_migrate_diff_stops_at_its_budget_and_takes_its_process_tree_with_it \
    tests/test_litellm/proxy/db/test_check_migration.py::test_migrate_diff_without_the_prisma_runner_skips_instead_of_crashing_boot \
    tests/test_litellm/proxy/db/test_prisma_client.py::test_db_push_timeout_takes_its_process_tree_with_it \
    tests/test_litellm/proxy/db/test_prisma_client.py::test_db_push_without_the_prisma_runner_fails_the_migration_instead_of_crashing_boot
E   Failed: Timeout (>30.0s) from pytest-timeout.        selectors.py:415    (migrate diff never returns)
E   Failed: Timeout (>45.0s) from pytest-timeout.        subprocess.py:2047  (db push waits out its budget, tree still alive)
E   FileNotFoundError: [Errno 2] No such file or directory: 'prisma'         (the helper raises into boot)
E   assert True is False                                 test_prisma_client.py:401
4 failed, 1 warning in 75.36s (0:01:15)

After (f8e34d9)

prisma db push

  1. Boot the proxy on the legacy push path, same lock still held.
$ python litellm/proxy/proxy_cli.py --config config.yaml --port 19851 --num_workers 2 --use_prisma_db_push
  1. All four attempts give up on schedule, and the log now names the budget they spent.
00:55:19 - LiteLLM Proxy:WARNING: prisma_client.py:966 - Attempt 1 timed out after 60s
00:56:25 - LiteLLM Proxy:WARNING: prisma_client.py:966 - Attempt 2 timed out after 60s
00:57:32 - LiteLLM Proxy:WARNING: prisma_client.py:966 - Attempt 3 timed out after 60s
00:58:39 - LiteLLM Proxy:WARNING: prisma_client.py:966 - Attempt 4 timed out after 60s
LiteLLM Proxy: Database migration failed but continuing startup. Set --enforce_prisma_migration_check or ENFORCE_PRISMA_MIGRATION_CHECK=true to exit on failure.
  1. The proxy comes up and serves a real Anthropic call.
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:19851/health/liveliness
200
$ curl -s http://127.0.0.1:19851/v1/chat/completions -H 'Authorization: Bearer sk-lit6801' \
    -H 'Content-Type: application/json' \
    -d '{"model":"anthropic-haiku-4-5","messages":[{"role":"user","content":"Reply with exactly: lit6801 push after ok"}]}'
anthropic-haiku-4-5 | lit6801 push after ok | {'prompt_tokens': 18, 'completion_tokens': 10, 'total_tokens': 28}
  1. Stop it the same way, and nothing prisma is left on the host.
$ kill -TERM 38507
$ ps -eo pid,ppid,command | grep -E 'schema-engine|db push'
$ lsof -nP -iTCP:19851 -sTCP:LISTEN

Both come back empty.

prisma migrate diff

  1. Boot the proxy on the other path, with the schema update disabled.
$ DISABLE_SCHEMA_UPDATE=true python litellm/proxy/proxy_cli.py --config config.yaml --port 19857 --num_workers 2
  1. After 60 seconds it says why it stopped waiting and finishes booting.
INFO:     Uvicorn running on http://0.0.0.0:19857 (Press CTRL+C to quit)
Timed out after 60.0s generating the migration diff. Raise LITELLM_PRISMA_COMMAND_TIMEOUT if this database needs longer.
INFO:     Application startup complete.
  1. The proxy is ready and serves a real Anthropic call.
$ curl -s -o /dev/null -w 'liveliness=%{http_code}\n' http://127.0.0.1:19857/health/liveliness
liveliness=200
$ curl -s http://127.0.0.1:19857/v1/chat/completions -H 'Authorization: Bearer sk-lit6801' \
    -H 'Content-Type: application/json' \
    -d '{"model":"anthropic-haiku-4-5","messages":[{"role":"user","content":"Reply with exactly: lit6801 diff after ok"}]}'
anthropic-haiku-4-5 | lit6801 diff after ok | {'prompt_tokens': 18, 'completion_tokens': 10, 'total_tokens': 28}
  1. Stop it the same way, and nothing prisma is left on the host.
$ kill -TERM 79092
$ ps -eo pid,ppid,command | grep -E 'schema-engine|migrate diff'
$ lsof -nP -iTCP:19857 -sTCP:LISTEN

Both come back empty.

An older proxy-extras install alongside this code

The two packages ship together, so this pairing only happens on a raw pip install '.[proxy]' of
the git tree between releases. Same clean database, same --use_prisma_db_push boot, with the
published litellm-proxy-extras==0.4.92 ahead of the worktree on PYTHONPATH.

At the merge base the push runs and the schema lands:

🚀  Your database is now in sync with your Prisma schema. Done in 1.23s
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:19877/health/liveliness
200

At this tip the import fails, boot says so and carries on, exactly as the default migrate path
already does when it cannot import the extras:

prisma_client.py:946 - LiteLLM: Failed to import proxy extras. Got cannot import name 'run_prisma' from 'litellm_proxy_extras.prisma_toolchain'
LiteLLM Proxy: Database migration failed but continuing startup. Set --enforce_prisma_migration_check or ENFORCE_PRISMA_MIGRATION_CHECK=true to exit on failure.
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:19863/health/liveliness
200

--enforce_prisma_migration_check (or ENFORCE_PRISMA_MIGRATION_CHECK=true) still turns that into
an exit, which is the operator's existing lever for a migration that did not run.

Type

🐛 Bug Fix

Caveats (if any)

Low

  • A diff slower than 60s now reports no drift instead of hanging
    • The printed message names LITELLM_PRISMA_COMMAND_TIMEOUT for a longer budget
    • Boot proceeds either way; the old behavior was an unbounded hang
  • prisma generate and the CLI runnability probe stay unbounded
    • Neither opens a database connection, so neither can hang on one
  • Pairing this proxy code with an extras build that predates run_prisma skips the command, not boot
    • migrate diff loses the drift diagnostic; db push logs the failure and leaves the schema alone
    • enforce_prisma_migration_check still decides whether that skipped push stops startup
    • Only a raw pip install of the git tree can pair them; releases publish both together
  • On Windows the runner still kills just the wrapper, not its descendants
    • Same as today: subprocess.run never reached them either, so nothing regresses

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

  • f8e34d9 passes /live-pr-risk

@greptile-apps

greptile-apps Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR routes boot-time Prisma schema checks and direct pushes through a timeout-aware runner so blocked database operations cannot stall startup indefinitely on supported process-group platforms

  • Adds a configurable timeout and process-group cleanup for prisma migrate diff and prisma db push
  • Handles unavailable Prisma runner imports without crashing proxy startup
  • Adds regression coverage using a fake Prisma process tree to verify timeout cleanup and retry behavior

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains

No blocking failure remains

Important Files Changed

Filename Overview
litellm/proxy/db/check_migration.py Replaces the unbounded migration-diff subprocess with the shared timeout-aware Prisma runner and handles timeout, command, and import failures
litellm/proxy/db/prisma_client.py Routes direct database pushes through the shared Prisma runner while preserving retry and post-push behavior
tests/test_litellm/proxy/db/conftest.py Adds a fake Prisma CLI fixture that records calls and creates a descendant process for cleanup verification
tests/test_litellm/proxy/db/test_check_migration.py Covers bounded migration-diff execution, descendant cleanup, and missing-runner degradation
tests/test_litellm/proxy/db/test_prisma_client.py Updates database-push tests to exercise the CLI boundary and adds timeout cleanup and missing-runner coverage

Reviews (3): Last reviewed commit: "fix(proxy): keep boot alive when the ins..." | Re-trigger Greptile

Comment thread litellm/proxy/db/check_migration.py Resolved
@codecov

codecov Bot commented Sep 3, 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

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor Author

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 cc4a066. Configure here.

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@mateo-berri mateo-berri added run-ci and removed run-ci labels Sep 3, 2026

@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 f8e34d9. Configure here.

Base automatically changed from litellm_prisma_timeout_killpg to litellm_internal_staging September 3, 2026 21:36
@yassin-berriai
yassin-berriai merged commit 9982453 into litellm_internal_staging Sep 8, 2026
113 of 117 checks passed
@yassin-berriai
yassin-berriai deleted the litellm_prisma_killpg_proxy_db branch September 8, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants