Skip to content

Commit

Permalink
Don't treat close code 1005 as an error.
Browse files Browse the repository at this point in the history
Fix #1260.
  • Loading branch information
aaugustin committed Nov 20, 2022
1 parent 26e1946 commit 0f4ecfc
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
14 changes: 14 additions & 0 deletions docs/project/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ They may change at any time.

*In development*

Backwards-incompatible changes
..............................

.. admonition:: Closing a connection without an empty close frame is OK.
:class: note

Receiving an empty close frame now results in
:exc:`~exceptions.ConnectionClosedOK` instead of
:exc:`~exceptions.ConnectionClosedError`.

As a consequence, calling ``WebSocket.close()`` without arguments in a
browser isn't reported as an error anymore.


New features
............

Expand Down
7 changes: 4 additions & 3 deletions src/websockets/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class ConnectionClosedError(ConnectionClosed):
"""
Like :exc:`ConnectionClosed`, when the connection terminated with an error.
A close code other than 1000 (OK) or 1001 (going away) was received or
sent, or the closing handshake didn't complete properly.
A close frame with a code other than 1000 (OK) or 1001 (going away) was
received or sent, or the closing handshake didn't complete properly.
"""

Expand All @@ -145,7 +145,8 @@ class ConnectionClosedOK(ConnectionClosed):
"""
Like :exc:`ConnectionClosed`, when the connection terminated properly.
A close code 1000 (OK) or 1001 (going away) was received and sent.
A close code with code 1000 (OK) or 1001 (going away) or without a code was
received and sent.
"""

Expand Down
6 changes: 5 additions & 1 deletion src/websockets/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ class Opcode(enum.IntEnum):
1014,
}

OK_CLOSE_CODES = {1000, 1001}
OK_CLOSE_CODES = {
1000,
1001,
1005,
}


BytesLike = bytes, bytearray, memoryview
Expand Down
2 changes: 1 addition & 1 deletion src/websockets/legacy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class WebSocketClientProtocol(WebSocketCommonProtocol):
await process(message)
The iterator exits normally when the connection is closed with close code
1000 (OK) or 1001 (going away). It raises
1000 (OK) or 1001 (going away) or without a close code. It raises
a :exc:`~websockets.exceptions.ConnectionClosedError` when the connection
is closed with any other code.
Expand Down
6 changes: 3 additions & 3 deletions src/websockets/legacy/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ async def __aiter__(self) -> AsyncIterator[Data]:
Iterate on incoming messages.
The iterator exits normally when the connection is closed with the
close code 1000 (OK) or 1001(going away). It raises
a :exc:`~websockets.exceptions.ConnectionClosedError` exception when
the connection is closed with any other code.
close code 1000 (OK) or 1001(going away) or without a close code. It
raises a :exc:`~websockets.exceptions.ConnectionClosedError` exception
when the connection is closed with any other code.
"""
try:
Expand Down
2 changes: 1 addition & 1 deletion src/websockets/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class WebSocketServerProtocol(WebSocketCommonProtocol):
await process(message)
The iterator exits normally when the connection is closed with close code
1000 (OK) or 1001 (going away). It raises
1000 (OK) or 1001 (going away) or without a close code. It raises
a :exc:`~websockets.exceptions.ConnectionClosedError` when the connection
is closed with any other code.
Expand Down

0 comments on commit 0f4ecfc

Please sign in to comment.