Skip to content

Skip importing the app in the parent process when using subprocesses#2988

Closed
Kludex wants to merge 4 commits into
worker-readinessfrom
skip-parent-app-load
Closed

Skip importing the app in the parent process when using subprocesses#2988
Kludex wants to merge 4 commits into
worker-readinessfrom
skip-parent-app-load

Conversation

@Kludex

@Kludex Kludex commented Jun 11, 2026

Copy link
Copy Markdown
Owner

Stacked on #2987.

Summary

This is the actual fix for #2980. Since #2919, uvicorn.run() eagerly imports the app in the parent process - but with --workers or --reload the parent only supervises subprocesses, which import the app themselves. The parent's copy is pure overhead. With #2987 the worker already builds its own Server and reports readiness, so the parent no longer needs the app at all in the subprocess case.

The change is two lines: only call config.load_app() in the parent when it actually serves the app in-process.

if not config.use_subprocess:
    config.load_app()

Thread workers (#2900) stay on the eager path: they aren't subprocesses, so use_subprocess is false and the parent loads as before, priming sys.modules for them.

Measured

200 MB app, workers=2, Python 3.14:

supervisor RSS
#2987 (base) 245 MB
this PR 44 MB

Workers unchanged (~247 MB each).

Behavior change

A bad import string with --workers is no longer caught by the parent's eager load; it now surfaces when the worker fails to boot and the supervisor shuts down with STARTUP_FAILURE (the mechanism added in #2987, covered by test_multiprocess_stops_on_startup_failure). So #2440 fail-fast is preserved, just relocated from parent to supervisor. Under --reload, a broken app logs the error and waits for a file change instead of killing the reloader.

The single-process path is untouched: it still imports eagerly before the event loop opens, keeping #941 fixed (test_run_imports_app_before_starting_event_loop).

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

Re-trigger cubic

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.

1 participant