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

[possible bug] 'code' is not labeled as optional #340

Closed
caleb15 opened this issue Aug 16, 2022 · 16 comments
Closed

[possible bug] 'code' is not labeled as optional #340

caleb15 opened this issue Aug 16, 2022 · 16 comments

Comments

@caleb15
Copy link

caleb15 commented Aug 16, 2022

In https://asgi.readthedocs.io/en/latest/specs/www.html#disconnect-receive-event-ws it does not say code is optional, yet it says "as per websocket spec", where code is optional ("may contain"). Later on in Close - send event it explicitly mentions that the code there is optional. In the first link should code be optional or required?

If code is optional I would be happy to submit a PR to this repo to update the docs.

This is not just a theoretical question, as we are facing a bug that may be related to this. In https://github.com/django/channels/blob/38324816c115567e198a573bc294d8345df5e8a9/channels/generic/websocket.py#L105 channels assumes that the code key exists, but in our case it doesn't exist and a KeyError is raised.

@andrewgodwin
Copy link
Member

I believe it was intentional that it was not optional - the WebSocket spec also says:

If this Close control frame contains no status code, The WebSocket Connection Close Code is considered to be 1005

Thus, I would say servers should default that value to 1005 if it is not contained in the frame

@caleb15
Copy link
Author

caleb15 commented Aug 16, 2022

So should channels default the value to 1005, then? Or maybe Uvicorn should specify code (link)? Or both?

@andrewgodwin
Copy link
Member

If I was being strict, I'd say the servers should always populate this field (so uvicorn should probably add it), but in the interests of compatibility it makes sense for channels to default it as well. I'll update the spec to clarify this, if that makes sense?

@caleb15
Copy link
Author

caleb15 commented Aug 16, 2022

Yep, that makes sense. Thanks!

@andrewgodwin
Copy link
Member

Alright, clarified in 5118b70 - thanks for highlighting this!

@Kludex
Copy link
Contributor

Kludex commented Aug 16, 2022

So should channels default the value to 1005, then? Or maybe Uvicorn should specify code (link)? Or both?

Uvicorn team is already aware of this. 👀 There's a PR in WIP adding the code on those. 🙏

@andrewgodwin
Copy link
Member

Great! Hopefully it matches the default 1005 value suggested here?

@Kludex
Copy link
Contributor

Kludex commented Aug 16, 2022

Not really, and now that you've mentioned... We were actually using 1000 on some places as default, see:

https://github.com/encode/uvicorn/blob/6cf6abacd57a96c8f189fdb8d5544cd45a88f35a/uvicorn/protocols/websockets/wsproto_impl.py#L294-L304

@andrewgodwin
Copy link
Member

Yeah, 1000 is the default for the app-initiated close, whereas 1005 is the default for the connection/client-initated close. It's... a confusing system.

@Kludex
Copy link
Contributor

Kludex commented Aug 16, 2022

I see... So on the PR: https://github.com/encode/uvicorn/pull/1441/files

The first one should be 1005. Correct?

@Kludex
Copy link
Contributor

Kludex commented Aug 16, 2022

Actually... Both on that PR should be 1005? 🤔

@andrewgodwin
Copy link
Member

Yeah, both of those should be, since they are disconnect. disconnect = 1005, close = 1000 as defaults.

@Kludex
Copy link
Contributor

Kludex commented Aug 16, 2022

Thanks Andrew :)

@carltongibson
Copy link
Member

@caleb15

...in the interests of compatibility it makes sense for channels to default it as well

If you think there's a change in Channels to be made for this, please do propose it over there. (Thanks 😉)

caleb15 added a commit to caleb15/channels that referenced this issue Aug 19, 2022
@caleb15
Copy link
Author

caleb15 commented Aug 19, 2022

@carltongibson, sure. I figure I might as well make the proposal a PR because the change is so easy to make: django/channels#1905

@Kludex
Copy link
Contributor

Kludex commented Nov 1, 2022

I've created a table on encode/uvicorn#1753 about the websocket codes we should be sending back, jfyk.

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

No branches or pull requests

4 participants