Skip to content

safe close()? #59

Closed
Closed

Description

@belm0

WSConnection.close() calls a method on self._proto, but that attribute is only non-null in certain states.

self._outgoing += self._proto.close(code, reason)

I don't see a public API to test for other than CLOSED state, so caller of close() doesn't have a straightforward way to safeguard. Probably best to have close() return early if not in a suitable state.

Activity

  1. njsmith commented on Aug 13, 2018

    @njsmith
    Member

    So I guess the issue is... you have a WSConnection, and you're calling .close() on it before it's completed the initial handshake? That's an interesting problem. We obviously can't send a CLOSE frame at that stage...

    What would be most convenient? Raise a HandshakeNotCompletedError? Provide a .handshake_completed property? Silently ignore the call to close?

  2. belm0 commented on Aug 13, 2018

    @belm0
    ContributorAuthor

    This happens routinely, such as when the WSConnection is managed by a context manager and you ctrl-C before the handshake.

    Reiterating, but my vote is for close() to return early if not in a suitable state, no exceptions involved. It seems the least error-prone for users, and would also provide idempotentcy.

  3. njsmith commented on Aug 13, 2018

    @njsmith
    Member

    This happens routinely, such as when the WSConnection is managed by a context manager and you ctrl-C before the handshake.

    I assume you mean in the asyncwebsockets project specifically? (Fuyukai/asyncwebsockets#9) You may be right that wsproto should fix this on its side, but I like to understand the problem before committing to things like that :-). For example, maybe it would be better for asyncwebsockets to be careful not to call WSConnection.close() until after the handshake.

    It's not entirely clear that idempotency makes sense for websocket close (at least at the wsproto level), because in the websocket protocol, close actually sends a message, which has an attached error code and a free-form text field. So what if someone calls close twice with two different error codes? We can't send two close packets.

    Though... right now if someone calls close twice, it does try to send two close packets, because there's nothing enforcing that you can't call close when already in state CLOSED. I guess that's a bug too.

    This also makes me a bit nervous about making it so that close silently succeed when you call it before the handshake completes – if you gave us an error code and reason to send, and we didn't actually send them... maybe we shouldn't just silently pretend we did?

    OTOH, the whole clean shutdown machinery for websockets is intrinsically kind of unreliable, because you have to be prepared to handle a network drop at any moment. There's no guarantee that a close packet actually arrives anyway – the network might eat it. And IIRC there are popular websocket implementations that don't even bother with sending close packets and just kill the connection when they're done. So maybe this is over-fussy and we should just treat close as best-effort.

  4. belm0 commented on Aug 13, 2018

    @belm0
    ContributorAuthor

    For example, maybe it would be better for asyncwebsockets to be careful not to call WSConnection.close() until after the handshake.

    I don't think it can be implemented on the current WSConnection API without race conditions. E.g. if there is a cancel between when WSConnection gets the handshake internally and the context manager sets a got_handshake variable to be used in finalization.

  5. Kriechi commented on Aug 13, 2018

    @Kriechi
    Member

    FYI: a related PR trying to improve the close-handshake: #50

  6. njsmith commented on Aug 13, 2018

    @njsmith
    Member

    Since WSConnection is a pure sans-io object, it doesn't have cancellation, and race-conditions aren't really a thing. However, it does look like for clients it's pretty opaque from the outside when exactly a connection has transitioned into the state where you can call close: I think the rule is "it happens in whichever call to receive_bytes changes the internal state such that you could immediately call events and get back a ConnectionEstablished event". Technically it's possible to detect this so long as you always call events after receive_bytes (which most people probably do!), but it does seem a bit unreasonable.

  7. Kriechi commented on Sep 6, 2018

    @Kriechi
    Member

    @belm0 does the recently merged #50 help you?
    One alternative would be to make close() simply do nothing if it is not yet in a state where it can send a close frame.

  8. mehaase commented on Oct 18, 2018

    @mehaase
    Member

    If closing can fail silently, then the API creates the following trap:

    conn.close()
    wait_for_closing_handshake()
    

    The user wants to initiate a closing handshake and then wait for the other side to complete the handshake. If the opening handshake isn't complete and this code fails silently, then this code hangs forever.

    Raising an exception sounds like the best option to me.

  9. Kriechi commented on Oct 19, 2018

    @Kriechi
    Member

    So if anyone wants to put send a PR that addresses this somehow - I think we are happy to look at it an merge if it makes sense. Right now, I'm not really sure what the best approach would be...
    #50 did a little bit of work in this area - but doesn't address this yet.

  10. mehaase commented on Nov 13, 2018

    @mehaase
    Member

    The two simplest options:

    1. Expose a read-only connection state property. The caller can check if the connection is open before calling close().
    2. Raise a better exception. Instead of AttributeError, create a more specific exception class, like ConnectionStateError (possibly includes the current connection state, if there is no public state property).

    These aren't mutually exclusive, of course. I'd prefer to have both of them, but either one is enough to solve this issue.

  11. added a commit that references this issue on Dec 29, 2018
    1fb4325
  12. touilleMan commented on Dec 29, 2018

    @touilleMan
    Contributor

    After some time figuring out this AttributeError exception, I've ended up here 😄

    So here is PR #92 implementing the solution exposed by @mehaase

  13. pgjones commented on Feb 23, 2019

    @pgjones
    Member

    I think this is complete with 2b38d3a.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions