litellm/litellm/proxy/prisma_migration.py
Kerry Lu 943a42b6d7
fix(proxy): skip prisma generate in the migration job when the prisma package is not writable (#52460)
* fix(proxy): skip prisma generate in the migration job when the prisma package is not writable

prisma_migration.py runs `prisma generate` after the migrations are done. The
generator writes the client into the installed prisma package, so with a
read-only root filesystem, or a uid that can't write site-packages, it always
fails and the failure is only logged. Its subprocesses still run alongside the
parent that holds the imported proxy, which roughly doubles the job's peak
memory (1156 MiB vs ~630 MiB on v1.105.1) and OOMKills the Helm migration Job
at a 1Gi limit after the migrations already succeeded

main() now checks that every prisma package directory is writable before
running the generator and logs the skip otherwise. Nothing changes where the
package is writable

Fixes #47076

* test(proxy): cover the default writability check against the real filesystem

The other tests inject the writability check, so the default one that calls
os.access was never run. Check it against a real temp directory and a
missing one, which holds even when the tests run as root

Co-authored-by: Dan Cowell <admin@dfcowell.net>

* test(proxy): mark the prisma_migration test specs Final

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

---------

Co-authored-by: Dan Cowell <admin@dfcowell.net>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-19 01:12:10 +00:00

64 lines
2.4 KiB
Python

"""Standalone entrypoint for applying database migrations and generating the Prisma client.
A failed migration fails the entrypoint, the same way it fails proxy startup. A failed
'prisma generate' is log-only: every shipped image bakes the client at build time, and
refreshing it writes into site-packages, which an arbitrary non-root uid or a read-only
root filesystem cannot do. When the prisma package is not writable it is skipped instead,
because its subprocesses roughly double the job's peak memory for a step that cannot succeed.
"""
import importlib.util
import os
import subprocess
import sys
from collections.abc import Callable
from importlib.machinery import ModuleSpec
sys.path.insert(0, os.path.abspath("./"))
from typing import Final
from litellm_proxy_extras.prisma_toolchain import resolve_prisma_argv
from litellm._logging import verbose_proxy_logger
from litellm.proxy.proxy_cli import run_server
def _is_writable_dir(path: str) -> bool:
return os.access(path, os.W_OK)
def prisma_package_is_writable(
find_spec: Callable[[str], ModuleSpec | None] = importlib.util.find_spec,
is_writable: Callable[[str], bool] = _is_writable_dir,
) -> bool:
"""'prisma generate' writes the client into the installed prisma package, so it can only succeed here."""
spec: Final = find_spec("prisma")
locations: Final = tuple(spec.submodule_search_locations or ()) if spec is not None else ()
return bool(locations) and all(is_writable(location) for location in locations)
def main(can_generate: Callable[[], bool] = prisma_package_is_writable) -> int:
run_server(("--skip_server_startup",), standalone_mode=False)
if not can_generate():
verbose_proxy_logger.info(
"Skipping 'prisma generate' because the prisma package is not writable, using the client baked at image build time"
)
return 0
verbose_proxy_logger.info("Running 'prisma generate'...")
result: Final = subprocess.run(resolve_prisma_argv(("prisma", "generate")), capture_output=True, text=True)
verbose_proxy_logger.info("'prisma generate' stdout: %s", result.stdout)
if result.returncode != 0:
verbose_proxy_logger.warning(
"'prisma generate' exited %s; continuing with the client baked at image build time. stderr: %s",
result.returncode,
result.stderr,
)
return 0
if __name__ == "__main__":
sys.exit(main())