django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.09k stars 799 forks source link

Daphne not cleaning up unix socket and does not bind to unix socket if previous file exists #642

Closed robdoesstuff closed 6 years ago

robdoesstuff commented 7 years ago

In our deployed environment, we find that daphne does not always bind to the unix socket correctly. Cases where I've seen it not bind:

For now we are reverting to using ports, but if unix sockets would work more reliably, we'd switch back (and prefer to switch back) in the future.

We are running daphne via supervisord in a debian-based docker container (python-2.7).

andrewgodwin commented 7 years ago

Daphne just uses the Twisted endpoint syntax to listen to a UNIX socket, so it's possible there's a cleanup step from that we're missing, or that's it's a bug in the Twisted endpoint core. This will need some investigation.

andrewgodwin commented 6 years ago

Closing due to age and rewrite of the code involved - if this is still an issue, please re-open it on the Daphne issue tracker.

Herst commented 6 years ago

Just came across this problem, also in docker. Deleting the files is a workaround.

sevdog commented 3 years ago

This is still an issue, when using docker this could happen when launching docker restart daphne-container, container keeps to report the error:

2021-10-05 06:41:41,994 CRITICAL Listen failure: Couldn't listen on any:b'/var/run/nginx-sockets/websocket.sock': Cannot acquire lock.
2021-10-05 06:42:44,676 INFO     Starting server at unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:42:44,676 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-10-05 06:42:44,677 INFO     Configuring endpoint unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:42:44,677 CRITICAL Listen failure: Couldn't listen on any:b'/var/run/nginx-sockets/websocket.sock': Cannot acquire lock.
2021-10-05 06:43:47,511 INFO     Starting server at unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:43:47,512 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-10-05 06:43:47,512 INFO     Configuring endpoint unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:43:47,513 CRITICAL Listen failure: Couldn't listen on any:b'/var/run/nginx-sockets/websocket.sock': Cannot acquire lock.
2021-10-05 06:44:50,241 INFO     Starting server at unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:44:50,241 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-10-05 06:44:50,241 INFO     Configuring endpoint unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:44:50,242 CRITICAL Listen failure: Couldn't listen on any:b'/var/run/nginx-sockets/websocket.sock': Cannot acquire lock.
2021-10-05 06:45:53,262 INFO     Starting server at unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:45:53,262 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-10-05 06:45:53,263 INFO     Configuring endpoint unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:45:53,264 CRITICAL Listen failure: Couldn't listen on any:b'/var/run/nginx-sockets/websocket.sock': Cannot acquire lock.
2021-10-05 06:46:56,252 INFO     Starting server at unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:46:56,253 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-10-05 06:46:56,253 INFO     Configuring endpoint unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:46:56,254 CRITICAL Listen failure: Couldn't listen on any:b'/var/run/nginx-sockets/websocket.sock': Cannot acquire lock.
2021-10-05 06:47:20,087 INFO     Starting server at unix:/var/run/nginx-sockets/websocket.sock
2021-10-05 06:47:20,088 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-10-05 06:47:20,088 INFO     Configuring endpoint unix:/var/run/nginx-sockets/websocket.sock
...

This cannot be avoided with an auto-restart policy since the lock file persists.

A graceful shutdown (when receiving SIGTERM) when using the unix socket should be used.

ananasn commented 2 years ago

Have the same issue. Daphne doesn't delete daphne.sock.lock after emergence shutdown.

codeKing2020 commented 1 year ago

How could I solve the following issue? 2021-10-05 06:46:56,254 CRITICAL Listen failure: Couldn't listen on any:b'/var/run/nginx-sockets/websocket.sock': Cannot acquire lock.

@sevdog did you solve this issue? If so, how?

sevdog commented 1 year ago

@codeKing2020 the answer is just mentioned above: https://github.com/django/channels/issues/642#issuecomment-372426179

Deleting the files is a workaround.

In your case the files which should be removed (with daphne and reverse-proxy turned off) are:

rm /var/run/nginx-sockets/websocket.sock /var/run/nginx-sockets/websocket.sock.lock

At this point we may open an issue in https://github.com/django/daphne with more info regarding this issue.

Nathan-Yinka commented 7 months ago

Listen failure: Couldn't listen on any:b'/code/educa/daphne.sock': Cannot acquire lock.

how do i solve this please

lemon1964 commented 6 months ago

Listen failure: Couldn't listen on any:b'/code/educa/daphne.sock': Cannot acquire lock.

how do i solve this please

Did you manage to find a solution? When this error occurred, creating a new folder /code/educa/tmp/ helped:

daphne: build: . working_dir: /code/educa/ command: ["../wait-for-it.sh", "db:5432", "--", "daphne", "-u", "/code/educa/tmp/daphne.sock", "educa.asgi:application"]

input stream for Daphne

upstream daphne { server unix:/code/educa/tmp/daphne.sock; }

The chat is working. But the next time you open the project, the error is again along the path of the new folder: Couldn't listen on any:b'/code/tmp/educa/daphne.sock': Cannot acquire lock.

meirm commented 6 months ago

I used a workaround. this is my nginx default.conf

    upstream django_backend {
        #server backend:8000;
        server unix:///app/uwsgi_app.sock; # uwsgi for a file socket

    }
     server {
        listen      80;
        server_name www.educaproject.com educaproject.com;
        return 301 https://$host$request_uri;
    }
    server {
        listen               443 ssl;
        ssl_certificate      /app/ssl/educa.crt;
        ssl_certificate_key  /app/ssl/educa.key;
        #listen [::]:80 default_server;
        server_name www.educaproject.com educaproject.com;

        # serve favicon.ico from the static directory
        location = /favicon.ico { access_log off; log_not_found off; alias /app/static/favicon.ico; }

        location / {
            # Pass requests to uWSGI
            uwsgi_pass django_backend;
            include /etc/nginx/uwsgi_params; # Ensure this file is in place
        }

        location /ws/ {
            proxy_pass http://daphne:8000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

        }

        location /static {
            alias /app/static; # Correct path to your Django static files
        }

        location /media {
            alias /app/media; # Correct path to your Django media files
        }

        # Optionally, you can handle 404 and 500 errors within Nginx
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }

and this is how do I run daphne:

command: ["./wait-for-it.sh", "database_default:5432", "--",
              "daphne", "-b", "0.0.0.0", "-p", "8000", "mysite.asgi:application"]