question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subprocess launched in Uvicorn worker always returns zero exit code

See original GitHub issue

Hi 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:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
julioasotodvcommented, Mar 6, 2020

It turns out that quite a bunch of libraries perform that call by using the autover library: https://github.com/pyviz-dev/autover

If Popen does not work correctly, uvicorn turns out to be incompatible with such libraries, for instance.

0reactions
euri10commented, Jan 2, 2021
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found