Skip to content

Commit 02b3338

Browse files
committed
Make Protocol.receive_eof idempotent.
This removes the need for keeping track of whether you called it or not, especially in an asyncio context where it may be called in eof_received or in connection_lost.
1 parent 8c4fd9c commit 02b3338

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/websockets/protocol.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,11 @@ def receive_eof(self) -> None:
270270
- You aren't expected to call :meth:`events_received`; it won't return
271271
any new events.
272272
273-
Raises:
274-
EOFError: If :meth:`receive_eof` was called earlier.
273+
:meth:`receive_eof` is idempotent.
275274
276275
"""
276+
if self.reader.eof:
277+
return
277278
self.reader.feed_eof()
278279
next(self.parser)
279280

tests/test_protocol.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,18 +1590,14 @@ def test_client_receives_eof_after_eof(self):
15901590
client.receive_data(b"\x88\x00")
15911591
self.assertConnectionClosing(client)
15921592
client.receive_eof()
1593-
with self.assertRaises(EOFError) as raised:
1594-
client.receive_eof()
1595-
self.assertEqual(str(raised.exception), "stream ended")
1593+
client.receive_eof() # this is idempotent
15961594

15971595
def test_server_receives_eof_after_eof(self):
15981596
server = Protocol(SERVER)
15991597
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c")
16001598
self.assertConnectionClosing(server)
16011599
server.receive_eof()
1602-
with self.assertRaises(EOFError) as raised:
1603-
server.receive_eof()
1604-
self.assertEqual(str(raised.exception), "stream ended")
1600+
server.receive_eof() # this is idempotent
16051601

16061602

16071603
class TCPCloseTests(ProtocolTestCase):

0 commit comments

Comments
 (0)