How to Fix Gunicorn Connection In Use (‘127.0.0.1’, 8000) Error

In deploying web applications on a server specifically flask application, we use gunicorn, a wsgi software or server to run our flask app. However you may face certain issues and one of the commonest issue is the connection in use error.

This occurs when there is something listening on the port that gunicorn is bound to mostly port 8000. You can find this from the logs, in our case we were using supervisor so by checking /var/log/your_flask_project/your_flask_app.err.log with nano you can see this error as above.

sudo cat /var/log/your_flask_project/your_flask_app.err.log

So how do you fix this issue.

First of all you need to identify the gunicorn process and its listening port and you can use the following method to identify them

pgrep gunicorn

or grep the gunicorn from the entire process manager via

ps -aux | grep gunicorn

You can then kill the gunicorn process using pkill like this

pkill gunicorn

Quick Solution

Alternatively you can simply kill the port 8000 which in most cases will fix the problem. First via netstat you can check what is listening on port 80 or 8000 and then kill is. In case you don’t have netstat you can install it with sudo apt-get install net-tools

sudo netstat -nlp | grep:80

To fix it you can use

sudo fuser -k 8000/tcp

. If you are using supervisor it will automatically start the service and your web application would be fine. I hope this helps

Thanks For Your Time

Jesus Saves

By Jesse E.Agbe(JCharis)

Leave a Comment

Your email address will not be published. Required fields are marked *