Skip to content

Clean up websocket closing code #7860

Closed

Description

@jdm

The code in websocket.rs related to closing the connection is difficult to read and reason about for a number of reasons. I propose the following:

  • we add a method named close_the_websocket_connection which creates a CloseTask and dispatches it to the script event loop
  • document that CloseTask implements the steps described in the spec
  • move the fields of WebSocket that are only used by CloseTask into CloseTask (such as reason, clean_close, code, etc.) and add constructors with meaningful names as appropriate

Activity

  1. added
    A-content/dom
    I-refactor
    E-more-complex
    on Oct 5, 2015
  2. anthgur commented on Oct 6, 2015

    @anthgur
    Contributor

    I'd like to work through this!

  3. jdm commented on Oct 6, 2015

    @jdm
    MemberAuthor

    That would be great!

  4. added
    C-assigned
    on Oct 6, 2015
  5. anthgur commented on Oct 18, 2015

    @anthgur
    Contributor

    Are there docs somewhere about how the task system interacts with the WebIDL bindings? I’m trying to understand when WebSocket::Close would be invoked in the current implementation.

    • The WebSocket::Constructor function fires CloseTasks (here and here) for different failure conditions.
    • Within WebSocket::Close, there's send_close function, which seems to interact with the underlying websocket client library to manage the close handshake.
    • Within the handler for CloseTask, it fires a CloseEvent.

    There don't seem to be any other references to CloseTask, but it has some necessary logic from the spec. Would a CloseTask ever be fired through the JavaScript bindings? Or does that only trigger WebSocket::Close?

  6. jdm commented on Oct 18, 2015

    @jdm
    MemberAuthor

    Those are great questions! I think the docs you're looking for are here. Specifically, JS code will only invoke methods that are part of the WebSocketMethods trait in this case. WebSocket::Close corresponds to ws.close(...) from JS.

  7. ghost removed
    C-assigned
    on Nov 26, 2015
  8. jsanders commented on Nov 30, 2015

    @jsanders
    Contributor

    @anthgur / @Wafflespeanut / @jdm: Am I understanding the removal of C-assigned correctly, as meaning that this is no longer being worked on? If so, I'd like to take a crack at this.

  9. KiChjang commented on Nov 30, 2015

    @KiChjang
    Contributor

    @jsanders Yes that is correct, this issue is up for grabs. I'll mark this as assigned.

  10. added
    C-assigned
    on Nov 30, 2015
  11. KiChjang commented on Dec 17, 2015

    @KiChjang
    Contributor

    @jsanders Any updates on this?

  12. jsanders commented on Dec 18, 2015

    @jsanders
    Contributor

    @KiChjang: Yeah, apologies for the slowness, I got a bit blocked trying to figure out some wpt tests that I either broke or fixed, and then #8867 happened, so I'm currently re-working stuff to work on top of that. Planning on pushing something up this weekend!

  13. jsanders commented on Jan 11, 2016

    @jsanders
    Contributor

    @KiChjang / @jdm: I'm finally getting back to this, but I've run into something I could use some guidance on.

    When I looked at this before, a tricky part of it was communicating the close code and reason between when the client starts the closing handshake, and when the server acknowledges it. But upon further reading of RFC 6455, section 7.1.5 states: "The WebSocket Connection Close Code is defined as the status code contained in the first Close control frame received by the application implementing this protocol." Section 7.1.6 makes a similar statement about close reason. My interpretation of this is that the code and reason used should be those received in a Close frame, rather than those sent.

    The current implementation doesn't work that way, and my changes ran afoul of some of the WPT tests that check status codes, for instance this one. I tracked it down to this code in pywebsocket, which unconditionally sets the server's close code to 1000 and reason to "".

    Am I misinterpreting the spec, or is the test implementation incorrect?

  14. jsanders commented on Jan 11, 2016

    @jsanders
    Contributor

    D'oh, my reading of the pywebsocket code was very poor – I see now that it's possible to override the close handler to make it echo code and reason from the client. But I'm still unsure whether doing so would make sense.

  15. jdm commented on Jan 11, 2016

    @jdm
    MemberAuthor

    @jsanders Your reading of the spec looks correct to me. I'd be curious if it passes as written in Firefox and Chrome today, and tempted to modify the test server to echo the provided code and reason.

  16. jsanders commented on Jan 11, 2016

    @jsanders
    Contributor

    @jdm: Ok I'm glad I'm not crazy. Unfortunately, I get some sort of ssl cert error when running the test in Firefox locally, but on w3c-test.org, it fails in the way I expect:

    screenshot 2016-01-10 23 23 11

    That's exactly what I get with my patch to the behavior, so it seems like Firefox is interpreting it the same way, though I haven't dug into the code to be completely sure.

    Chrome's behavior is wonkier – the close event ends up with code 1006 ("abnormal"). I thought maybe it was comparing the client and server codes and flagging it as abnormal if they don't match, but I hardcoded the server side to return 1005, and got the same results, so I'm not sure what to think.

    I'll start looking into modifying the test server behavior.

  17. jdm commented on Jan 11, 2016

    @jdm
    MemberAuthor

    FWIW, I'd probably want to submit the changes to the test server upstream independently to ensure that the right eyes see those changes.

  18. jsanders commented on Jan 12, 2016

    @jsanders
    Contributor

    @jdm: Alright, I think I figured this out. The confusion is that section 7.4.1 states that code 1005 is reserved to communicate that there was no code and should never actually be sent across the wire. So the right answer is to send no code in this case, expect no code to be echoed back, and set the code of the event to 1005 to communicate that this is what happened. The WPT websocket echo handler still needs to be updated to work that way, though – currently it sends back 1000 instead of nothing. Do you think I should send that change upstream first, or include it in a PR here first?

  19. jsanders commented on Jan 12, 2016

    @jsanders
    Contributor

    @jdm: I went ahead and pushed that change, so you can take a look. It fixes my tests, and I believe it would make firefox pass more of these tests.

  20. jdm commented on Jan 12, 2016

    @jdm
    MemberAuthor

    I think I'd need to see how that change interacts with the other changes, since I still feel like I'm missing part of the picture here.

  21. jsanders commented on Jan 18, 2016

    @jsanders
    Contributor

    @jdm: Just pushed #9358, with all the changes for this.

  22. added a commit that references this issue on Jan 19, 2016
    6663f28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/domC-assignedE-more-complexI-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions