skip to Main Content

I am using Django with Daphne and Nginx to run my websockets. Everything was working fine for months in production, however just a couple days ago my websockets wouldn’t connect saying "WebSocket connection to ‘wss://mydomain:8001/clips/509/’ failed: ".

I checked the server logs and this is in the output

2022-04-07 16:57:22,970 ERROR    [Failure instance: Traceback: <class 'AttributeError'>: 'NoneType' object has no attribute 'replace'
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/autobahn/websocket/protocol.py:2839:processHandshake
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/txaio/tx.py:366:as_future
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/ws_protocol.py:72:onConnect
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: --- <exception caught here> ---
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/server.py:200:create_application
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/routing.py:54:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:35:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:53:valid_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:72:validate_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:73:<genexpr>
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:97:match_allowed_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:376:urlparse
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:430:urlsplit
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: ]

I don’t understand why this happened all of the sudden, is it maybe a dependency got updated or deprecated?

2

Answers


  1. I solved this by editing the redis.service file

    nano /etc/systemd/system/redis.service

    ExecStop=/bin/kill -s TERM $MAINPID

    ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"

    then type

    sudo systemctl daemon-reload

    sudo systemctl enable redis-server

    sudo systemctl restart redis.service

    Login or Signup to reply.
  2. This is because a fix in urllib3. AllowedHostsOriginValidator is broked. I solved this editing my asgi.py file.

    I changed my "application" definition from this:

    import os
    
    from .wsgi import *
    from channels.auth import AuthMiddlewareStack
    from channels.security.websocket import AllowedHostsOriginValidator
    from channels.routing import ProtocolTypeRouter, URLRouter
    from django.core.asgi import get_asgi_application
    
    os.environ.setdefault(
        'DJANGO_SETTINGS_MODULE',
        'manager.settings.production'
    )
    
    from .routing import websocket_urlpatterns
    application = ProtocolTypeRouter({
        "websocket": AllowedHostsOriginValidator(
            AuthMiddlewareStack(
                URLRouter(
                    websocket_urlpatterns
                )
            )
        ) 
    })
    

    to this:

    import os
    
    from .wsgi import *
    from channels.auth import AuthMiddlewareStack
    from channels.security.websocket import AllowedHostsOriginValidator
    from channels.routing import ProtocolTypeRouter, URLRouter
    from django.core.asgi import get_asgi_application
    
    os.environ.setdefault(
        'DJANGO_SETTINGS_MODULE',
        'manager.settings.production'
    )
    
    from .routing import websocket_urlpatterns
    application = ProtocolTypeRouter({
        "websocket": AuthMiddlewareStack(
            URLRouter(
                websocket_urlpatterns
            ) 
        )
    })
    

    More info here: https://github.com/django/channels/issues/1716

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search