aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

Gunicorn with Aiohttp Worker: Subprocess Return Code is incorrect (Returns 0 Instead of Non-Zero)

Open NAnand3200 opened this issue 2 years ago • 3 comments

Describe the bug

When running an application with Gunicorn using the aiohttp.GunicornWebWorker, the subprocess.run() method returns 0 as the returncode, even though the command exits with a non-zero code (e.g., exit 2 or exit 27).

To Reproduce

1. Shell Script (hello.sh)

#!/bin/bash
exit 27

2. Python Application (app.py)

import subprocess
from aiohttp import web

router = web.RouteTableDef()

@router.get("/run")
async def run_subprocess(request):
    cmd = ["sh", "hello.sh"]
    result = subprocess.run(cmd, timeout=300)
    response_text = (
        f"Task initiated successfully.\n"
        f"Return code: {result.returncode}\n"
    )
    return web.Response(text=response_text, status=200)

async def init_app() -> web.Application:
    app = web.Application()
    app.add_routes(router)
    return app`

3. Gunicorn Configuration (abc1.py)

bind = "0.0.0.0:9080"
worker_class = "aiohttp.GunicornWebWorker"
workers = 1
loglevel = "debug"
accesslog = "-"
access_log_format = '%a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"'
timeout = 3600

4. Run the Application

gunicorn --config abc1.py --preload app:init_app

5. Send Request

curl http://localhost:9080/run --noproxy '*'

Expected behavior

The returncode should match the actual exit code of the script (27 in this case).

Logs/tracebacks

Request:
curl http://localhost:9080/run --noproxy '*'

Response:
Task initiated successfully.
Return code: 0

Python Version

$ python --version
Python 3.9.13

aiohttp Version

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.11.8
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author:
Author-email:
License: Apache-2.0
Location: /usr/local/lib64/python3.9/site-packages
Requires: aiohappyeyeballs, aiosignal, async-timeout, attrs, frozenlist, multidict, propcache, yarl
Required-by:

multidict Version

$ python -m pip show multidict
Name: multidict
Version: 6.1.0
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /usr/local/lib64/python3.9/site-packages
Requires: typing-extensions
Required-by: aiohttp, yarl

propcache Version

$ python -m pip show propcache
Name: propcache
Version: 0.2.0
Summary: Accelerated property cache
Home-page: https://github.com/aio-libs/propcache
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /usr/local/lib64/python3.9/site-packages
Requires:
Required-by: aiohttp, yarl

yarl Version

$ python -m pip show yarl
Name: yarl
Version: 1.18.0
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /usr/local/lib64/python3.9/site-packages
Requires: idna, multidict, propcache
Required-by: aiohttp

OS

Rocky Linux release 8.7 (Green Obsidian)

Related component

Server

Additional context

No response

Code of Conduct

  • [X] I agree to follow the aio-libs Code of Conduct

NAnand3200 avatar Nov 29 '24 10:11 NAnand3200

This probably needs someone familiar with gunicorn. I don't even see why the aiohttp code would have anything to do with this.

Dreamsorcerer avatar Dec 01 '24 01:12 Dreamsorcerer

subprocess.run is sync API and shouldn't be used in async code. Use https://docs.python.org/3/library/asyncio-subprocess.html instead.

More information https://docs.astral.sh/ruff/rules/run-process-in-async-function/

bdraco avatar Dec 01 '24 01:12 bdraco

Yes, Proceeded with asyncio module. Thanks!!

NAnand3200 avatar Dec 02 '24 05:12 NAnand3200