Home > other >  Uvicorn not opening port on ubuntu
Uvicorn not opening port on ubuntu

Time:12-09

I've set up Uvicorn on Ubuntu 22.04 and am getting the uvicorn process to start successfully using the below. It looks like Uvicorn isn't actually opening a local port to listen to, despite saying that it is, and ufw allowing port 8000.

Thoughts?

./execWxApi.sh

#contents of execWxApi.sh

#!/bin/sh

uvicorn app.wxapi:app --host 0.0.0.0 --reload

Output results showing port open on 8000

ubuntu@ip-172-1-1-1:~$ ./execWxApi.sh
INFO:     Will watch for changes in these directories: ['/home/ubuntu']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL C to quit)
INFO:     Started reloader process [5216] using StatReload
^CINFO:     Stopping reloader process [5216]

I'm not getting a response on port 8000. I've double checked firewalls, both in AWs and in ufw, and port 8000 is open to all addresses.

So, lets make sure that port 8000 is actually in use on the Ubuntu machine:

sudo netstate -tlnp

Output of netstat:

ubuntu@ip-172-1-1-1:~/app$ sudo netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      654/sshd: /usr/sbin 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      403/systemd-resolve 
tcp6       0      0 :::22                   :::*                    LISTEN      654/sshd: /usr/sbin 

There's no port 8000 open. Huh?

############

Additionally,

On this machine if I set up a very simply python script and execute it, I get a result in browser - confirming the machine is reachable, and ports are open all the way through.

test_server.py

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

The execute with

python3 test_server.py

Then visit

http://my_internet_facing_ip:8000

CodePudding user response:

It looks like this issue might be related to the initialisation time of the script. I have no idea why the second thing I tried has refused to work on the remote host at this stage.

My script attempts to load some data into memory with xarray so that it's sitting there at the time a query arrives and there's no querying unindexed data from disk. The principle issue in this case seems to be that when using the --reload flag that Uvicorn does not fail, it sits there and reports that the api endpoint is alive and well and the process is running. When the --reload flag is omitted the process takes 5-10 seconds to return a message, and that message is simply "Killed".

So, I believe it would be worth while understanding whether it's the memory limit on my instance that's the issue, or whether it's Uvicorn failing because the script is taking a while before it can accept a route due to pre-loading the data into indexed memory.

The challenge in identifying this is that I've had it running in a smaller container, and it hasn't had an issue.

Moving to using the startup event might solve the issue.

@app.on_event("startup")
async def startup_event():
    items["foo"] = {"name": "Fighters"}
    items["bar"] = {"name": "Tenders"}

https://fastapi.tiangolo.com/advanced/events/?h=startup#startup-event

  • Related