Daphne + Channel v3 Deployment, RuntimeError: no running event loop

When I run systemctl start daphne I get the following error

Traceback (most recent call last):
  File "/srv/www/portal/bin/daphne", line 8, in <module>
    sys.exit(CommandLineInterface.entrypoint()) 
  File "/srv/www/portal/lib/python3.8/site-packages/daphne/cli.py", line 170, in entrypoint
    cls().run(sys.argv[1:])
  File "/srv/www/portal/lib/python3.8/site-packages/daphne/cli.py", line 232, in run
    application = import_by_path(args.application)
  File "/srv/www/portal/lib/python3.8/site-packages/daphne/utils.py", line 12, in import_by_path
    target = importlib.import_module(module_path)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/srv/www/portal/portal/./portal/asgi.py", line 3, in <module>
    from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter
  File "/srv/www/portal/lib/python3.8/site-packages/channels/routing.py", line 10, in <module>
    from channels.http import AsgiHandler
  File "/srv/www/portal/lib/python3.8/site-packages/channels/http.py", line 9, in <module>
    from asgiref.sync import async_to_sync, sync_to_async
  File "/srv/www/portal/lib/python3.8/site-packages/asgiref/sync.py", line 304, in <module>
    class SyncToAsync:
  File "/srv/www/portal/lib/python3.8/site-packages/asgiref/sync.py", line 328, in SyncToAsync
    loop = get_running_loop()
RuntimeError: no running event loop

The service definition:

[Unit]
Description=daphne service
PartOf=postgresql.service
After=postgresql.service

[Service]
WorkingDirectory=/srv/www/portal/portal/
Environment=JSON_SETTINGS=/srv/www/portal/settings.json
Environment=ASGI_THREADS=10
ExecStart=/srv/www/portal/bin/daphne -b 0.0.0.0 -p 8000 portal.asgi:application
Restart=always
KillSignal=SIGTERM
NotifyAccess=all

[Install]
WantedBy=multi-user.target

This is my asgi.py

import os
import django
from channels.http import AsgiHandler
from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "portal.settings")
django.setup()

from api.routing import websocket_urlpatterns
from channels.auth import AuthMiddlewareStack
from api.consumers import MultiplexConsumer


application = ProtocolTypeRouter({
  "http": AsgiHandler(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns
        )
    ),
})

I am have the following versions on Python 3.8:

Django==2.2.24
channels==3.0.4
daphne==3.0.2
asgiref==3.4.1

Answer

Looks like a channels issue: https://github.com/django/channels/issues/1713

Same issue here as I am going through the tutorial of Channels v3 at https://channels.readthedocs.io/en/stable/tutorial/index.html. Using asgiref==3.3.4 is okay.

Pinning to asgiref==3.3.4 is the current workaround