question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AsyncHttpConsumer regression and disconnect handling

See original GitHub issue

Hi folks,

I just discovered Channels >=2.1.6 introduced an incompatibility with the django-eventstream library. Client requests hang, due to what I believe is the consumer task ending too quickly.

Our library implements handle, which returns quickly and spawns a subtask to handle the ongoing stream. At the time the code was written, my understanding was that spawning a task was the proper way to handle streaming responses as it allowed the original task to keep running in order to detect disconnects.

However, as of commit a2561396cd3a508c8475211cbe7b1618985c1eb9, disconnect is called immediately after handle returns. This breaks spawn-style streaming implementations.

I’m attempting to adjust our code to work with the latest Channels behavior, essentially by not spawning a task (just run the streaming logic within handle without returning), but now I run into the problem that I can’t cleanly handle disconnects. Instead, after the client disconnects, the task is eventually killed:

Application instance <Task pending coro=<SessionMiddlewareInstance.__call__() running at /home/justin/dev/django-eventstream/examples/chat2/venv/lib/python3.6/site-packages/channels/sessions.py:179> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fafd82f6ac8>()]>> for connection <WebRequest at 0x7fafd934e9b0 method=GET uri=/rooms/default/events/ clientproto=HTTP/1.1> took too long to shut down and was killed.

Unfortunately this doesn’t give the task a chance to clean up properly.

This issue is also oddly familiar: #1051

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jkarnegescommented, Feb 21, 2019

Okay here we go: #1250

1reaction
carltongibsoncommented, Feb 18, 2019

Hey @jkarneges. What I was initially interested in is exactly what changed. i.e. a test (for your use-case) that passes in 2.1.5 and fails in 2.1.6. It would help to be able to play with what you were doing.

I agree you’ve diagnosed the question here. Even if handle() does not return I’m expecting http_disconnect() to receive the http.disconnect event and call your disconnect() method:

https://github.com/django/channels/blob/a660d81899e1cfd030f20a499cdeae1cea3e0646/channels/generic/http.py#L87-L92

Read more comments on GitHub >

github_iconTop Results From Across the Web

Consumers — Channels 4.0.0 documentation
Allow you to write synchronous or async code, and deal with handoffs and threading for you. Of course, you are free to ignore...
Read more >
django-channels - Bountysource
Multiple requests to synchronous views are handled sequentially, not concurrently. ... Disconnected and re-connect may resolve the issue.
Read more >
Django channels, disconnect - Stack Overflow
I refresh the page. it calls the disconnect. I do this. but the refreshed page receive the same message twice. the old one...
Read more >
How to subclass AsyncHttpConsumer for a GET request?
Newbie to Channels. I am trying to write a Async consumer to handle a http GET request. How to write a subclass MyAsynHttpConsumer(AsyncHttpConsumer)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found