Proposal: Add is_disconnected
property to WebSocket
#2762
-
MotivationWhen handling a WebSocket connection, it’s common to have multiple coroutines interacting with the same connection. For example, one coroutine may handle incoming messages, while another handles error recovery or cleanup. In such scenarios, it's possible for multiple coroutines to attempt to close the WebSocket. This can lead to a To prevent this, I propose adding an Benefits
Consider this scenario (that already happened to me):
if not websocket.is_disconnected:
await websocket.close() ImplementationHere’s a simple implementation of the @property
def is_disconnected(self) -> bool:
return (
self.application_state == WebSocketState.DISCONNECTED
or self.client_state == WebSocketState.DISCONNECTED
) ConclusionMaybe this is too specific or maybe even based on a wrong principle or a problem with my app or thinking. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
I feel like this may not be the right answer. For example, we could add |
Beta Was this translation helpful? Give feedback.
I believe the exception is the way to go.