← index #1037PR #1038
Duplicate · high · value 0.197
QUERY · ISSUE

aiohttp: ws: frame not fully received/read if large

openby mirkoopened 2025-07-31updated 2025-08-01

I noticed how large payload are cut-off when using the high-level receive-functions provided by the websocket implementation of aiohttp.

The following debug statements show the observed truncation:

diff --git a/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py b/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py
index 6e0818c..34c7b08 100644
--- a/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py
+++ b/python-ecosys/aiohttp/aiohttp/aiohttp_ws.py
@@ -203,7 +203,9 @@ class WebSocketClient:
 
         if has_mask:  # pragma: no cover
             mask = await self.reader.read(4)
+        print("READING BYTES:", length)
         payload = await self.reader.read(length)
+        print("READ BYTES:", len(payload))
         if has_mask:  # pragma: no cover
             payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
         return opcode, payload

Output from test run supposed to receive a >1.5KiB payload:

READING BYTES: 1594
READ BYTES: 1436
3 comments
mirko · 2025-07-31

The issue probably lies in that read(n) does not guarantee to actually read n bytes (all at once). Normally I'd iterate over reading until result is null/None or read and count received bytes in a loop until everything is received as expected.
Neither approach appears to work in this scenario, though.

mirko · 2025-07-31

Attempted fix: https://github.com/micropython/micropython-lib/pull/1038

dpgeorge · 2025-08-01

Should be fixed by #1034 (which is merged).

CANDIDATE · PULL REQUEST

aiohttp: ws: fix truncated read of payload

closedby mirkoopened 2025-07-31updated 2025-12-02

aiohttp: fix partial read on websocket resulting in truncated payload

Attempting to read N bytes from a socket does not guarantee to actually
read N bytes, even if >= N bytes were written onto the peer socket.

This especially becomes an issues when attempting to read larger
messages at once, which then potentially can't be read all at once,
resulting in truncated payloads.

Fix that by reading as long / often until expected length was actually
received.

1 comment
dpgeorge · 2025-08-01

This should be fixed by #1034 (that was merged yesterday).

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied