fix(profiling): arm the profiler in the workers, not only the supervisor (#4254)

`main()` installs the profiler before uvicorn starts, but `--workers N` makes uvicorn spawn
children that re-import `hindsight_api.server:app` and never run `main()`. So on any multi-worker
deployment the only armed profiler lived in the supervisor, and its report was `keep_subprocess_alive`
and `ping` at 0.01 cores while the workers served every request.

That is worse than having no profile, because it looks like an answer — it faithfully names the
busiest function in a process that serves nothing. Arming it where the app is imported covers the
worker processes, and `install()` already guards on a module-level flag and catches the ValueError
cProfile raises when the process-global tool is held, so arming in both places is safe.
This commit is contained in:
Nicolò Boschi
2026-09-14 11:03:06 +02:00
committed by GitHub
parent 5a60831751
commit cdac00842b
@@ -26,6 +26,19 @@ from hindsight_api.config import get_config, load_dotenv_for_entrypoint
load_dotenv_for_entrypoint()
# Arm profiling in THIS process, which is where the requests are served.
#
# `main()` arms it too, but with `--workers N` uvicorn's supervisor spawns children that re-import
# this module and never run `main()` — so the only profiler was the one in the supervisor, and its
# report was `keep_subprocess_alive`/`ping` at 0.01 cores while the workers did all the work. A
# profile that cannot see the request path is worse than none: it looks like an answer.
#
# Safe to call twice: cProfile is a process-wide tool since 3.12, and `install()` is a no-op when
# HINDSIGHT_API_PROFILE is unset and idempotent within a process.
from hindsight_api.profiling import install as _install_profiling
_install_profiling()
# Disable tokenizers parallelism to avoid warnings
os.environ["TOKENIZERS_PARALLELISM"] = "false"