django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.11k stars 800 forks source link

Ensure text message exists before handling on `WebsocketConsumer` #2097

Closed cacosandon closed 2 months ago

cacosandon commented 6 months ago

When using uvicorn or daphne, only one of text or bytes is sent.

But on hypercorn, both of them are sent, but with None value: https://github.com/pgjones/hypercorn/blob/31639ec2f4d03aa920b95c84686163901224c6cf/src/hypercorn/protocol/ws_stream.py#L159

So if you just send bytes, text will be None and you won't be able to handle the message.

I just add an extra check.

bigfootjon commented 4 months ago

Hey @cacosandon, this looks reasonable to me. Can you add a regression test?

cacosandon commented 2 months ago

Sorry for the super late reaction @bigfootjon, I've added a regression test! Had to use send_input and send_output because WebsocketCommunicator has some safe checks that hypercorn is skipping.

carltongibson commented 2 months ago

(Sorry, @cacosandon : can you fix the lint error? Thanks.)