why do i have error "Address already in use"?

17,899

Solution 1

i have same issue, but the problem was in sqlalchemy, try to add this:

@app.teardown_request
def shutdown_session(exception=None):
    from extension import db
    db.session.remove()

Solution 2

This error means that port 9002 is already in use by another process. As per your logs that process is uwsgi probably another instance of uWSGI is running on the same address (127.0.0.1:9002). May be the port was not released while you stop flask app and your wsgi server is restarted while you run touch touch_reload. You may try the following command to release the port.

sudo fuser -k 9002/tcp

If that is a tcp process and restart your wsgi server again to see if the port is already in use.

Solution 3

maybe you stop uwsgi by crtl + z:

  1. find the pid of process which take 8000

$ lsof -i:8000

result maybe is:

COMMAND  PID       USER   FD   TYPE ...
uwsgi   9196       xxx    4u  xxx   ...

then

$ kill 9196

Solution 4

FWIW, I had a similar issue and discovered that I was running uwsgi --http when I should have been running uwsgi --socket.

Share:
17,899
comalex3
Author by

comalex3

Python/java developer

Updated on July 22, 2022

Comments

  • comalex3
    comalex3 almost 2 years

    i run my flask app, and it works good, but by the time the app is stopped and in my uwsgi log

    probably another instance of uWSGI is running on the same address (127.0.0.1:9002).
        bind(): Address already in use [core/socket.c line 764]
    

    when i run touch touch_reload, app is working again. I run anything else on the server which may take the socket.

    my conf:

    nginx
    server {
        listen 80;
        ....
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:9001;
        }
        ....
    }
    server {
        listen 80;
        ....
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:9003;
        }
        ....
    }
    
    uwsgi:
    chdir = /var/www/../
    module = wsgihandler
    socket = 127.0.0.1:9003
    wsgi-file = app/__init__.py
    callable = app
    master = true
    chmod-socket = 664
    uid = root
    gid = root
    processes = 4
    socket-timeout = 180
    post-buffering = 8192
    max-requests = 1000
    buffer-size = 32768
    logto = /var/www/.../log/uwsgi.log
    touch-reload = /var/www/.../touch_reload
    
    • Havenard
      Havenard almost 9 years
      It means port 9002 is already in use.
    • John La Rooy
      John La Rooy almost 9 years
      How are you stopping the process?
    • therealprashant
      therealprashant almost 9 years
      You probably must be using ctrl+z to stop the process but that is actually just hiding it. use ctrl+c to entirely stop
    • therealprashant
      therealprashant almost 9 years
      additionally you can use ps aux | grep 9002 to see what are using 9002 for
    • comalex3
      comalex3 almost 9 years
      I did not stop the server, it is production
  • Lucas Mendes Mota Da Fonseca
    Lucas Mendes Mota Da Fonseca almost 5 years
    It could also be useful(necessary) to use -9: ` kill -9 9196`