Jump to solution
Verify

The Fix

Upgrade to version 0.21.0 or later.

Based on closed Kludex/uvicorn issue #1763 · PR/commit linked

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -3,6 +3,7 @@ if [ -d 'venv' ] ; then export PREFIX="venv/bin/" + export PATH=${PREFIX}:${PATH} fi export SOURCE_FILES="uvicorn tests"
repro.py
+ '[' -z ']' + scripts/check + ./scripts/sync-version + venv/bin/black --check --diff --target-version=py37 uvicorn tests All done! ✨ 🍰 ✨ 63 files would be left unchanged. + venv/bin/flake8 uvicorn tests + venv/bin/mypy --show-error-codes Success: no issues found in 53 source files + venv/bin/isort --check --diff --project=uvicorn uvicorn tests + venv/bin/python -m tools.cli_usage --check Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 69, in <module> rv |= _generate_cli_usage(path, check=args.check) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 47, in _generate_cli_usage usage_lines = _get_usage_lines() ^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 13, in _get_usage_lines res = subprocess.run(["uvicorn", "--help"], stdout=subprocess.PIPE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line 546, in run with Popen(*popenargs, **kwargs) as process: ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line 1022, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line 1899, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'uvicorn'
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\nUpgrade to version 0.21.0 or later.\nWhen NOT to use: This fix is not applicable if the environment is correctly set up with 'uvicorn' in the path.\n\n

Why This Fix Works in Production

  • Trigger: + '[' -z ']'
  • Mechanism: The cli_usage tool fails to find 'uvicorn' in the system path when not activated in a virtual environment
  • Why the fix works: Fixes the cli_usage tool on systems without an activated virtualenv by updating the PATH variable. (first fixed release: 0.21.0).
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • Shows up under Python 3.11 in real deployments (not just unit tests).
  • The cli_usage tool fails to find 'uvicorn' in the system path when not activated in a virtual environment
  • Surfaces as: + '[' -z ']'

Proof / Evidence

  • GitHub issue: #1763
  • Fix PR: https://github.com/encode/uvicorn/pull/1823
  • First fixed release: 0.21.0
  • Reproduced locally: No (not executed)
  • Last verified: 2026-02-09
  • Confidence: 0.85
  • Did this fix it?: Yes (upstream fix exists)
  • Own content ratio: 0.27

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“I understand the problem now”
@Kludex · 2022-11-28 · source
“I'm also having problems with python3.11 with uvicorn[standard] installation, I don't know if I should open an issue though...”
@Ou7law007 · 2022-11-11 · source
“> I'm also having problems with python3.11 with uvicorn[standard] installation, I don't know if I should open an issue though... > > That looks like…”
@Kludex · 2022-11-28 · source
“Let me try to do that. I haven't contributed for a while and forgot everything 🥲”
@cdeler · 2022-12-29 · source

Failure Signature (Search String)

  • + '[' -z ']'

Error Message

Stack trace
error.txt
Error Message ------------- + '[' -z ']' + scripts/check + ./scripts/sync-version + venv/bin/black --check --diff --target-version=py37 uvicorn tests All done! ✨ 🍰 ✨ 63 files would be left unchanged. + venv/bin/flake8 uvicorn tests + venv/bin/mypy --show-error-codes Success: no issues found in 53 source files + venv/bin/isort --check --diff --project=uvicorn uvicorn tests + venv/bin/python -m tools.cli_usage --check Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 69, in <module> rv |= _generate_cli_usage(path, check=args.check) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 47, in _generate_cli_usage usage_lines = _get_usage_lines() ^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 13, in _get_usage_lines res = subprocess.run(["uvicorn", "--help"], stdout=subprocess.PIPE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line 546, in run with Popen(*popenargs, **kwargs) as process: ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line ... (truncated) ...
Stack trace
error.txt
Error Message ------------- httptools/parser/parser.c:212:12: fatal error: longintrepr.h: No such file or directory 212 | #include "longintrepr.h" | ^~~~~~~~~~~~~~~ compilation terminated. error: command '/usr/bin/gcc' failed with exit code 1 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: legacy-install-failure × Encountered error while trying to install package. ╰─> httptools

Minimal Reproduction

repro.py
+ '[' -z ']' + scripts/check + ./scripts/sync-version + venv/bin/black --check --diff --target-version=py37 uvicorn tests All done! ✨ 🍰 ✨ 63 files would be left unchanged. + venv/bin/flake8 uvicorn tests + venv/bin/mypy --show-error-codes Success: no issues found in 53 source files + venv/bin/isort --check --diff --project=uvicorn uvicorn tests + venv/bin/python -m tools.cli_usage --check Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 69, in <module> rv |= _generate_cli_usage(path, check=args.check) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 47, in _generate_cli_usage usage_lines = _get_usage_lines() ^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/playground/uvicorn/tools/cli_usage.py", line 13, in _get_usage_lines res = subprocess.run(["uvicorn", "--help"], stdout=subprocess.PIPE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line 546, in run with Popen(*popenargs, **kwargs) as process: ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line 1022, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/Users/wuyusheng/.pyenv/versions/3.11.0/lib/python3.11/subprocess.py", line 1899, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'uvicorn'

Environment

  • Python: 3.11

What Broke

Running tests fails with a FileNotFoundError for 'uvicorn' when not in the system path.

Why It Broke

The cli_usage tool fails to find 'uvicorn' in the system path when not activated in a virtual environment

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

Upgrade to version 0.21.0 or later.

When NOT to use: This fix is not applicable if the environment is correctly set up with 'uvicorn' in the path.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Option D — Guard side-effects with OnceOnly Guardrail for side-effects

Mitigate duplicate external side-effects under retries/timeouts/agent loops by gating the operation before calling external systems.

  • Place OnceOnly between your code/agent and real side-effects (Stripe, emails, CRM, APIs).
  • Use a stable key per side-effect (e.g., customer_id + action + idempotency_key).
  • Fail-safe: configure fail-open vs fail-closed based on blast radius and spend risk.
Show example snippet (optional)
onceonly.py
from onceonly import OnceOnly import os once = OnceOnly(api_key=os.environ["ONCEONLY_API_KEY"], fail_open=True) # Stable idempotency key per real side-effect. # Use a request id / job id / webhook delivery id / Stripe event id, etc. event_id = "evt_..." # replace key = f"stripe:webhook:{event_id}" res = once.check_lock(key=key, ttl=3600) if res.duplicate: return {"status": "already_processed"} # Safe to execute the side-effect exactly once. handle_event(event_id)

See OnceOnly SDK

When NOT to use: Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.

Fix reference: https://github.com/encode/uvicorn/pull/1823

First fixed release: 0.21.0

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not applicable if the environment is correctly set up with 'uvicorn' in the path.
  • Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
  • Pin production dependencies and upgrade only with a reproducible test that hits the failing path.

Version Compatibility Table

VersionStatus
0.21.0 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.