Subprocess launched in Uvicorn worker always returns zero exit code
See original GitHub issueHi everyone, I am facing one of those little bugs that almost seem impossible to happen (at least IMHO):
Imagine that I have the following “ASGI server app” (server.py
), in which I want to make a call to a bash command using subprocess.Popen
(I know, there is no ASGI app, but this is just an example):
import subprocess
# The command mkdir without any other
# arguments would normally return nonzero
# exit code (it returns 1):
proc = subprocess.Popen(["mkdir"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error = (str(s.decode()).strip() for s in proc.communicate())
print("Output: ", output)
print("Error: ", error)
# However, inside a Uvicorn worker, it returns 0 exit code:
print("Exit code: ", proc.returncode)
And we run our “app” with:
gunicorn -k uvicorn.workers.UvicornWorker server.py
The exit code result of subprocess.Popen
is always 0, even though it shouldn’t. The output is:
Output:
Error: mkdir: missing operand
Try 'mkdir --help' for more information.
Exit code: 0
However, if I just use Gunicorn’s default sync
workers:
gunicorn server.py
The return code is the right one:
Output:
Error: mkdir: missing operand
Try 'mkdir --help' for more information.
Exit code: 1
Is there any reason why Uvicorn’s Gunicorn worker always return zero exit code from subprocess.Popen
?
I know it is a weird thing to do, but I actually need a bash call inside the Uvicorn workers.
Thank you!
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Command '[...]' returned non-zero exit status 1 - Stack Overflow
subprocess.check_output raises CalledProcessError on non-zero exit code, and ping returns non-zero exit code if something is wrong (e.g. ...
Read more >Deployment - Uvicorn
As a general rule, you probably want to: Run uvicorn --reload from the command line for local development. Run gunicorn -k uvicorn.workers.UvicornWorker for ......
Read more >Python 3: Get and Check Exit Status Code (Return Code) from ...
Get Exit Status Code. When running a command using subprocess.run() , the exit status code of the command is available as the .returncode ......
Read more >FastAPI in Containers - Docker
FastAPI framework, high performance, easy to learn, fast to code, ready for production.
Read more >Result Code build-failed-stage-build - The Debian Janitor
[feature]` may not be used on the stable release channel ... 2m45s, dpkg-buildpackage: error: fakeroot debian/rules clean subprocess returned exit status 2.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It turns out that quite a bunch of libraries perform that call by using the
autover
library: https://github.com/pyviz-dev/autoverIf Popen does not work correctly, uvicorn turns out to be incompatible with such libraries, for instance.
See also https://github.com/encode/uvicorn/pull/895