Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncHttpConsumer example from documentation hangs in send_response() #1331

Closed
yuriiz opened this issue Aug 7, 2019 · 9 comments
Closed

Comments

@yuriiz
Copy link

yuriiz commented Aug 7, 2019

I've tried running example from https://channels.readthedocs.io/en/latest/topics/consumers.html#asynchttpconsumer but it does not work. It simply hangs up without sending any response body.
I've followed https://channels.readthedocs.io/en/latest/tutorial/index.html exactly step by step. Added channels to INSTALLED_APPS, adjusted settings, added routing for http requests only and 1 single AsyncHTTPConsumer.
Doing verification step from https://channels.readthedocs.io/en/latest/tutorial/part_2.html "Let’s make sure that the channel layer can communicate with Redis..." also was performed successfully.

Example project: https://github.com/yuriiz/myproject/
Steps to reproduce:

git clone https://github.com/yuriiz/myproject.git
cd myproject
pip install -r requirements.txt
./manage.py migrate
./manage.py runserver

In another terminal:

curl localhost:8000/test

But requests hangs indefinitely. In runserver output there is no error message or any other ouput.
After adding logging to https://github.com/yuriiz/myproject/blob/master/myproject/consumers.py and retrying only log message before send_response() is shown i.e. send_response() never returns.

Is there anything else which is not mentioned in documentation required to use AsyncHTTPConsumer?

Please also try and include, if you can:

  • Your OS and runtime environment, and browser if applicable
    Tried on Ubuntu 19.04, Python3.7 and FreeBSD-11.3, Python3.6 with same result.
  • A pip freeze output showing your package versions
    aioredis==1.2.0
    asgiref==3.2.1
    asn1crypto==0.24.0
    async-timeout==3.0.1
    attrs==19.1.0
    autobahn==19.8.1
    Automat==0.7.0
    cffi==1.12.3
    channels==2.2.0
    channels-redis==2.4.0
    constantly==15.1.0
    cryptography==2.7
    daphne==2.3.0
    Django==2.2.4
    hiredis==1.0.0
    hyperlink==19.0.0
    idna==2.8
    incremental==17.5.0
    msgpack==0.6.1
    pkg-resources==0.0.0
    pycparser==2.19
    PyHamcrest==1.9.0
    pytz==2019.2
    six==1.12.0
    sqlparse==0.3.0
    Twisted==19.7.0
    txaio==18.8.1
    zope.interface==4.6.0
  • What you expected to happen vs. what actually happened
    Server should return response. It hang up.
  • How you're running Channels (runserver? daphne/runworker? Nginx/Apache in front?)
    runserver
  • Console logs and full tracebacks of any errors
$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 07, 2019 - 07:03:47
Django version 2.2.4, using settings 'myproject.settings'
Starting ASGI/Channels version 2.2.0 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
***before send_response()
(hangs)
@carltongibson
Copy link
Member

Sorry, but we can't help out with general support requests here - the issue tracker is for reproduceable bugs and
concrete feature requests only! Please see our support documentation (http://channels.readthedocs.io/en/latest/support.html)
for more information about where you can get general help.

All I can really suggest with the information you've given is to enable Asyncio Debug Mode and see if that reveals your problem.

@yuriiz
Copy link
Author

yuriiz commented Aug 7, 2019

@carltongibson If you can reduce the problem to a clear set of steps to reproduce or an example project that fails in a fresh environment, please re-open the ticket with that information. - I've done precisely that.

@carltongibson
Copy link
Member

OK, despite all that, I had a look. The error is that headers must be bytes.

Change it to this:

        await self.send_response(200, b"Your response bytes", headers=[
            (b"Content-Type", b"text/plain"),
        ])

The docs here should have this. OR Channels/Daphne should handle it better.

@yuriiz
Copy link
Author

yuriiz commented Aug 7, 2019

Thanks! I confirm it fixes the problem.
It would be great to correct mistake at https://channels.readthedocs.io/en/latest/topics/consumers.html#asynchttpconsumer to avoid confusion.

@carltongibson
Copy link
Member

Yes! At the least, all examples should pass bytes. Can you make a PR whilst you've got the files open?

@yuriiz
Copy link
Author

yuriiz commented Aug 7, 2019

It looks like example in docs is already corrected in master branch:
https://github.com/django/channels/blob/master/docs/topics/consumers.rst
It is just not live yet.

@carltongibson
Copy link
Member

Ok, I’ll look at that. Thanks.

@carltongibson
Copy link
Member

Hi @yuriiz. The docs were not automatically rebuild. I've rebuilt both versions so they now carry the correction. Sorry you ran into that.

Thank you for the sample project. I've opened an issue on Daphne for that. django/daphne#276 — Daphne is meant to trigger an informative ValueError there, but is hanging instead so I'll have a look at what's going on there. (Now where's my razor... :)

@yuriiz
Copy link
Author

yuriiz commented Aug 8, 2019

Problem seems to be in AsyncHTTPConsumer.http_request(). Raising any exception inside AsyncHTTPConsumer.handle() (including parameters misuse) will lead to finally block of AsyncHTTPConsumer.http_request() which will replace original exception with raise StopConsumer() which will mask original exception and prevent user from seeing real cause of the problem neither through logs nor stderr which makes debugging such cases difficult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants