channels: warnings/errors logged after disconnecting async http request

Hi,

I’m using version 2.1.1. I set up a simple consumer (almost the same as the SSE example from the docs). If I use runserver, and connect a client and then disconnect, some ugly logging occurs some time after.

10 seconds after disconnecting, there’s a warning:

2018-05-19 21:20:20,065 - WARNING - server - Application instance <Task pending coro=<AsyncHttpConsumer.__call__() running at /home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/channels/generic/http.py:25> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fb79ead45b8>()]>> for connection <WebRequest at 0x7fb79fb2ff28 method=GET uri=/events/ clientproto=HTTP/1.1> took too long to shut down and was killed.

A client disconnecting from a stream is something I would consider to be normal behavior, and should probably not cause noisy logging.

60 seconds after disconnecting, there’s an exception:

Exception in callback AsyncioSelectorReactor.callLater.<locals>.run() at /home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/twisted/internet/asyncioreactor.py:287
handle: <TimerHandle when=275136.787314338 AsyncioSelectorReactor.callLater.<locals>.run() at /home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/twisted/internet/asyncioreactor.py:287>
Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/twisted/internet/asyncioreactor.py", line 290, in run
    f(*args, **kwargs)
  File "/home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/daphne/server.py", line 276, in timeout_checker
    protocol.check_timeouts()
  File "/home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/daphne/http_protocol.py", line 260, in check_timeouts
    self.basic_error(503, b"Service Unavailable", "Application failed to respond within time limit.")
  File "/home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/daphne/http_protocol.py", line 293, in basic_error
    (b"Content-Type", b"text/html; charset=utf-8"),
  File "/home/justin/dev/django-eventstream/examples/chat/venv3/lib/python3.6/site-packages/daphne/http_protocol.py", line 208, in handle_reply
    raise ValueError("HTTP response has already been started")
ValueError: HTTP response has already been started

That said, the warnings and errors appear to be harmless. New connections work as expected.

Here’s the consumer:

class EventsConsumer(AsyncHttpConsumer):
    async def handle(self, body):
        await self.send_headers(headers=[
	    ("Cache-Control", "no-cache"),
            ("Content-Type", "text/event-stream"),
        ])
        while True:
            payload = "data: %s\n\n" % datetime.now().isoformat()
            await self.send_body(payload.encode("utf-8"), more_body=True)
            await asyncio.sleep(1)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 26 (17 by maintainers)

Most upvoted comments

@andrewgodwin @erm Hello! It seems I have similar problem. I’m also using nice tutorial at hhttps://channels.readthedocs.io/en/latest/introduction.html about DjangoChannels. All understood, wrote my own one (suppose the same as there), everything is working, thanks for clear explanation on the site! However, I ran into a problem: when updating the chat page, the server throws this exception, moreover not in 60 seconds, but immediately (and after that I can’t send messages, so that’s quite harmful):

Exception in callback AsyncioSelectorReactor.callLater…run() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287 handle: .run() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287> Traceback (most recent call last): File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/events.py”, line 81, in _run self._context.run(self._callback, *self._args) File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py”, line 290, in run f(*args, **kwargs) File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/daphne/server.py”, line 229, in application_checker exception = application_instance.exception() asyncio.exceptions.CancelledError

(I am using WebsocketConsumer, python 3.8.2)

Actually, I suppose there is a problem with the disconnection with the server when updating the page. I tried to solve it myself but unfortunately didn’t succeed 😦 Could anybody please help me?