3

I'm deploying a website using Django and Django-Channels, with Channel's daphne ASGI server substituting for the typical Gunicorn WSGI setup. Using this Gunicorn WSGI tutorial as a jumping off guide, I attempted to write a systemctl service for my daphne server, when I hit the below error:

CRITICAL Listen failure: [Errno 13] Permission denied: '27646' -> b'/run/daphne.sock.lock'

I was unfortunately unable to find any answers to why permissions would be denied to the .sock file, (in context to Daphne) so I was hoping I could get some hints on where to begin debugging this problem. Below are my daphne.socket and my daphne.service files.

daphne.service

[Unit]
Description=daphne daemon
Requires=daphne.socket
After=network.target

[Service]
User=brianl
Group=www-data
WorkingDirectory=/home/brianl/autoXMD
ExecStart=/home/brianl/autoXMD/env/bin/daphne -u /run/daphne.sock -b 0.0.0.0 -p 8000 autoXMD.asgi:application
[Install]
WantedBy=multi-user.target

daphne.socket

[Unit]
Description=daphne socket

[Socket]
ListenStream=/run/daphne.sock

[Install]
WantedBy=sockets.target

Based off the linked DigitalOcean tutorial, I start my service with sudo systemctl start daphne.socket.

My guess is that there's some kind of discrepancy between setting up systemctl services for Gunicorn and Daphne that I missed, but I don't know for sure.

(If it helps, I'm planning on using Nginx as the main server, but I haven't reached that point yet)

EDIT:

It would help if I also attached the full output systemd gives:

● daphne.service - daphne daemon
   Loaded: loaded (/etc/systemd/system/daphne.service; enabled; vendor preset: enabled)
   Active: failed (Result: start-limit-hit) since Thu 2019-09-05 22:00:43 UTC; 1min 51s ago
  Process: 22041 ExecStart=/home/brianl/autoXMD/env/bin/daphne -u /run/daphne.sock -b 0.0.0.0 -p 8000 autoXMD.asgi:application (code=exited, status=0/SUCCESS)
 Main PID: 22041 (code=exited, status=0/SUCCESS)

Sep 05 22:00:43 autoxmd daphne[22041]:   warnings.warn('%s.  joblib will operate in serial mode' % (e,))
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,013 INFO     Starting server at tcp:port=8000:interface=0.0.0.0, unix:/run/daphne.sock
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,017 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,020 INFO     Configuring endpoint tcp:port=8000:interface=0.0.0.0
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,022 INFO     Listening on TCP address 0.0.0.0:8000
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,022 INFO     Configuring endpoint unix:/run/daphne.sock
Sep 05 22:00:43 autoxmd daphne[22041]: 2019-09-05 22:00:43,022 CRITICAL Listen failure: [Errno 13] Permission denied: '22041' -> b'/run/daphne.sock.lock'
Sep 05 22:00:43 autoxmd systemd[1]: daphne.service: Start request repeated too quickly.
Sep 05 22:00:43 autoxmd systemd[1]: daphne.service: Failed with result 'start-limit-hit'.
Sep 05 22:00:43 autoxmd systemd[1]: Failed to start daphne daemon.
Brian Lee
  • 41
  • 5
  • Have you change the owner of folder `/run` for user `brianl` yet? – Toan Quoc Ho Sep 03 '19 at 04:00
  • as @ToanQuocHo mentioned it occur because `brianl` user haven't permission to write on `.sock` . you can use `sudo chmod 775 .sock` – mehdi Sep 03 '19 at 04:36
  • Hey, I attempted `sudo chmod 775 /run` (and `sudo chmod 775 /run/*`) but neither worked. Any additional suggestions? – Brian Lee Sep 05 '19 at 21:50
  • Ok, I get this. You have error in Your config. The problem is that, this `daphne -u /run/daphne.sock -b 0.0.0.0 -p 8000` isnt equivalent with `gunicorn -bind ....` the param for daphne `-u` will try to create unix socet and then bind to it. The Solution is to just ommit the part with creating the socket by Yourself. so just remove `Requires=daphne.socket` from service. – Take_Care_ Sep 05 '19 at 22:24
  • Should I also remove the `-u /run/daphne.sock` portion from the `daphne` command as well? – Brian Lee Sep 05 '19 at 22:27
  • If You don't need to have local socket to connect to You app, yes You can. The idea of local socket is that it doesn't occupy any of the network ports, and You dont need to remember the port so other local services can connect by this socket path insted by network device. – Take_Care_ Sep 05 '19 at 22:31
  • It will still listen on the network, so You will be able to access it by localhost calls etc. – Take_Care_ Sep 05 '19 at 22:32
  • I tried removing `Requires=daphne.socket` from `daphne.service`, but I couldn't test the connection with `curl --unix-socket /run/gunicorn.sock localhost`. (Given on the DigitalOcean tutorial) Attempting `curl 0.0.0.0:8000` instead yields the same error. Any additional tips? – Brian Lee Sep 06 '19 at 23:48
  • 1
    EDIT: it appears that additionally removing `-u /run/daphne.sock` finally made `curl` work. How would I make it work using the socket approach? (for people who encounter this problem in the future) – Brian Lee Sep 06 '19 at 23:58
  • The problem is definitely something to do with `daphne -u /run/daphne.sock project.asgi:application` as you get the error just running that command – theEpsilon Aug 08 '20 at 17:50

1 Answers1

2

I think this occurred for the permission issue. By default /run directory is owned by root. So the daphne socket failed to create the daphne.sock.lock file in /run directory.

The solution is that create a folder in /run directory and give permission to your user and group.

For example:

sudo mkdir /run/daphne
sudo chown brianl:www-data /run/daphne

Now change the Unix sock path in the service & socket file.

daphne.service

[Unit]
Description=daphne daemon
Requires=daphne.socket
After=network.target

[Service]
User=brianl
Group=www-data
WorkingDirectory=/home/brianl/autoXMD
ExecStart=/home/brianl/autoXMD/env/bin/daphne -u /run/daphne/daphne.sock -b 0.0.0.0 -p 8000 autoXMD.asgi:application
[Install]
WantedBy=multi-user.target

daphne.socket

[Unit]
Description=daphne socket

[Socket]
ListenStream=/run/daphne/daphne.sock

[Install]
WantedBy=sockets.target

Hopefully, this works for you. For further you can go through a similar issue MySQL Daemon Lock issue