You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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.
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.
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.
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.
@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.
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.
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.
Expose a read-only connection state property. The caller can check if the connection is open before calling close().
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.
Activity
njsmith commentedon Aug 13, 2018
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 aCLOSEframe at that stage...What would be most convenient? Raise a
HandshakeNotCompletedError? Provide a.handshake_completedproperty? Silently ignore the call toclose?belm0 commentedon Aug 13, 2018
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.njsmith commentedon Aug 13, 2018
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,closeactually sends a message, which has an attached error code and a free-form text field. So what if someone callsclosetwice with two different error codes? We can't send two close packets.Though... right now if someone calls
closetwice, it does try to send two close packets, because there's nothing enforcing that you can't callclosewhen already in stateCLOSED. I guess that's a bug too.This also makes me a bit nervous about making it so that
closesilently 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
closeas best-effort.belm0 commentedon Aug 13, 2018
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_handshakevariable to be used in finalization.Kriechi commentedon Aug 13, 2018
FYI: a related PR trying to improve the close-handshake: #50
njsmith commentedon Aug 13, 2018
Since
WSConnectionis 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 callclose: I think the rule is "it happens in whichever call toreceive_byteschanges the internal state such that you could immediately calleventsand get back aConnectionEstablishedevent". Technically it's possible to detect this so long as you always calleventsafterreceive_bytes(which most people probably do!), but it does seem a bit unreasonable.Kriechi commentedon Sep 6, 2018
@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.mehaase commentedon Oct 18, 2018
If closing can fail silently, then the API creates the following trap:
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.
Kriechi commentedon Oct 19, 2018
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.
mehaase commentedon Nov 13, 2018
The two simplest options:
close().AttributeError, create a more specific exception class, likeConnectionStateError(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.
Improve CloseConnection behavior (close python-hyper#59)
touilleMan commentedon Dec 29, 2018
After some time figuring out this
AttributeErrorexception, I've ended up here 😄So here is PR #92 implementing the solution exposed by @mehaase
pgjones commentedon Feb 23, 2019
I think this is complete with 2b38d3a.