2

It seems a number of people have run into the same problem with this, but so far it is proved unfixable. I am hoping someone might have worked it out.

I am trying to run FastAPI on cPanel and keep getting the error [UID:1293][19858] Child process with pid: 20085 was killed by signal: 15, core dump: 0

I set it up in cPanel using the python app (my host provider allows python scripts) with python 3.8.12 and do the pip installs via terminal all of which worked fine (uvicorn, fastapi and various others). On GitHub someone recommended running it on its own subdomain, but that didn't solve the issue it either.

My passenger_wsygi.py code works fine like this to produce "It works!" in the browser when I visit api.mydomain.com

import os
import sys
from a2wsgi import ASGIMiddleware
from main import app  # Import your FastAPI app.


application = ASGIMiddleware(app)

# debugging section
async def log_middleware(scope, receive, send):
    async def _receive():
        msg = await receive()
        print(msg)
        return msg

    async def _send(msg):
        print(msg)
        await send(msg)

    return await app(scope, _receive, _send)

# end of debugging section


sys.path.insert(0, os.path.dirname(__file__))


def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    message = 'It works!\n'
    version = 'Python %s\n' % sys.version.split()[0]
    response = '\n'.join([message, version])
    return [response.encode()]

But if I remove the def application above, then I start getting the child process error.

Here is my test main.py:

from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def root():    
    return "Hello World!"

All the code is in the root folder of api.mydomain.com

There are no additional errors in the cPanel error logs and running it from the terminal gives no other clues.

Links I have looked at for solution are here, here and here among many others, but none of which have produced a successful outcome. I haven't yet tried this alternative to a2swgi but I am rapidly losing interest in FastAPI on cPanel, but wanted to give it one last shot at finding a fix.

EDIT: the only other clue I have is when trying to test uvicorn in the same python environment from terminal in cPanel it wont do anything. I run the python env. then type uvicorn main:app this announces that Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) I type: curl http://localhost:8000 and it hangs there until I cancel it. no errors in logs. nothing. but no "hello world" either.

EDIT 2: I ran ps aux in a terminal while using api.mysite.com to call the code and see the processes

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
thetempl 17063  0.0  0.0 215176 12544 ?        S    19:15   0:00 lsphp
thetempl 17531  0.0  0.0 113636  2044 pts/0    S    19:03   0:00 /bin/bash -l
thetempl 27453  0.0  0.0 153536  1792 pts/0    R+   19:15   0:00 ps aux
thetempl 30655  0.6  0.0 218740 27752 ?        S<l  19:13   0:00 /opt/alt/python38/bin/lswsgi -m /home/thetempl/api.mysite.com/passenger_wsgi.py
thetempl 31022  0.0  0.0 218740 20396 ?        S<s  19:13   0:00 /opt/alt/python38/bin/lswsgi -m /home/thetempl/api.mysite.com/passenger_wsgi.py

The above was just before it timed out. Once it timed out the lsphp line was gone and PID 31022 too, but I had to kill off the PID 30655 process manually. In stderr.log the following line showed up.

[UID:1293][30655] Child process with pid: 31022 was killed by signal: 15, core dump: 0

I have been looking at this issue and this one but still no luck getting uvicorn to run in terminal using different ports, or in browser.

mdkb
  • 372
  • 1
  • 14
  • I don't know how relevant this is, but your binding `application = ASGIMiddleware(app)` is shadowed by the function definition `def application(...): ...`. – jthulhu Aug 16 '22 at 05:31
  • anything helps. the ```passenger``` code you see works, once I remove ```def application``` it is when the errors start. so I guess this is because it is looking at ```main.py``` at that point and not liking it. I also tried putting all the fastapi code from ```main.py``` in the ```passenger_wsgi.py``` but same error happens. Also, in CPanel python app setup it specifies: ```"Application Entry Point: application"``` – mdkb Aug 16 '22 at 05:34
  • But what happens (i.e. the complete error, etc.) when you actually try to launch your FastAPI app through the ASGI middleware? When you're `def`-ing `application` as well, you're never running your _actual_ FastAPI application, you're just attaching a dummy application that doesn't do anything useful. – MatsLindh Aug 16 '22 at 07:14
  • @MatsLindh not sure that I follow. my understanding is that when I remove ```def application``` then ```main.py``` is accessible through ```from main import app``` and ```application=ASGIMiddleware(app)``` is what is pointing to fastapi in ```main.py```. could you define the code if you think something else should be happening? – mdkb Aug 16 '22 at 07:19
  • When you `def application` after you've done `application = ASGIMiddleware`, `application` no longer refers to your middleware, it refers to your function (which is what the first comment is about). So when you define a function named `application`, your FastAPI application is never registered or attempted to be used in any way - just calling `ASGIMiddleware(app)` doesn't actually do anything other than the FastAPI `app` object being wrapped in a middleware; a few lines later you replace `application` with a function, so that's the only part left. There is no attempt to invoke your FastAPI app – MatsLindh Aug 16 '22 at 07:22
  • @MatsLindh yea okay, but this doesnt work either and it is supposed to be all you need to invoke fastapi https://stackoverflow.com/a/65147451/9594269 – mdkb Aug 16 '22 at 07:27
  • My point ws that "doesn't work" doesn't actually help anyone to give you a better answer; _what_ happens? What does the complete log show? Do you get any errors? Does anything start and requests fail? Do the server process exit with an error (in addition to the child process you included)? Have you tried running passenger from the cli instead of through cpanel to see if you get any other errors? – MatsLindh Aug 16 '22 at 07:38
  • @MatsLindh logs pretty minimal. I tried it with all but the basic code stripped out. it hangs for a few minutes then ```500 internal server error``` in the browser saying ```request time out```. in the ```stderr.log``` is the following lines ```[UID:1293][21652] Child process with pid: 22130 was killed by signal: 15, core dump: 0``` ```[UID:1293][21652] Child process with pid: 25107 was killed by signal: 15, core dump: 0``` and nothing more. in CPanel error metrics there are no errors in the last few hours related to this. that is all I have to go on with error logs. – mdkb Aug 16 '22 at 07:42
  • running passenger through cli gives no message, it just presents the next line as if it ran perfectly. – mdkb Aug 16 '22 at 07:43

0 Answers0