Skip to content

Commit d1bed5b

Browse files
bdracopatchback[bot]
authored andcommitted
Disable zero copy writes in the StreamWriter (#10125)
(cherry picked from commit d58d2c3)
1 parent db5e6bb commit d1bed5b

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

CHANGES/10125.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disabled zero copy writes in the ``StreamWriter`` -- by :user:`bdraco`.

aiohttp/http_writer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _writelines(self, chunks: Iterable[bytes]) -> None:
9090
transport = self._protocol.transport
9191
if transport is None or transport.is_closing():
9292
raise ClientConnectionResetError("Cannot write to closing transport")
93-
transport.writelines(chunks)
93+
transport.write(b"".join(chunks))
9494

9595
async def write(
9696
self, chunk: bytes, *, drain: bool = True, LIMIT: int = 0x10000

tests/test_http_writer.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,15 @@ async def test_write_large_payload_deflate_compression_data_in_eof(
104104
assert transport.write.called # type: ignore[attr-defined]
105105
chunks = [c[1][0] for c in list(transport.write.mock_calls)] # type: ignore[attr-defined]
106106
transport.write.reset_mock() # type: ignore[attr-defined]
107-
assert not transport.writelines.called # type: ignore[attr-defined]
108107

109108
# This payload compresses to 20447 bytes
110109
payload = b"".join(
111110
[bytes((*range(0, i), *range(i, 0, -1))) for i in range(255) for _ in range(64)]
112111
)
113112
await msg.write_eof(payload)
114-
assert not transport.write.called # type: ignore[attr-defined]
115-
assert transport.writelines.called # type: ignore[attr-defined]
116-
chunks.extend(transport.writelines.mock_calls[0][1][0]) # type: ignore[attr-defined]
113+
chunks.extend([c[1][0] for c in list(transport.write.mock_calls)]) # type: ignore[attr-defined]
114+
115+
assert all(chunks)
117116
content = b"".join(chunks)
118117
assert zlib.decompress(content) == (b"data" * 4096) + payload
119118

@@ -180,7 +179,7 @@ async def test_write_payload_deflate_compression_chunked(
180179
await msg.write(b"data")
181180
await msg.write_eof()
182181

183-
chunks = [b"".join(c[1][0]) for c in list(transport.writelines.mock_calls)] # type: ignore[attr-defined]
182+
chunks = [c[1][0] for c in list(transport.write.mock_calls)] # type: ignore[attr-defined]
184183
assert all(chunks)
185184
content = b"".join(chunks)
186185
assert content == expected
@@ -216,7 +215,7 @@ async def test_write_payload_deflate_compression_chunked_data_in_eof(
216215
await msg.write(b"data")
217216
await msg.write_eof(b"end")
218217

219-
chunks = [b"".join(c[1][0]) for c in list(transport.writelines.mock_calls)] # type: ignore[attr-defined]
218+
chunks = [c[1][0] for c in list(transport.write.mock_calls)] # type: ignore[attr-defined]
220219
assert all(chunks)
221220
content = b"".join(chunks)
222221
assert content == expected
@@ -235,16 +234,16 @@ async def test_write_large_payload_deflate_compression_chunked_data_in_eof(
235234
# This payload compresses to 1111 bytes
236235
payload = b"".join([bytes((*range(0, i), *range(i, 0, -1))) for i in range(255)])
237236
await msg.write_eof(payload)
238-
assert not transport.write.called # type: ignore[attr-defined]
239237

240-
chunks = []
241-
for write_lines_call in transport.writelines.mock_calls: # type: ignore[attr-defined]
242-
chunked_payload = list(write_lines_call[1][0])[1:]
243-
chunked_payload.pop()
244-
chunks.extend(chunked_payload)
238+
compressed = []
239+
chunks = [c[1][0] for c in list(transport.write.mock_calls)] # type: ignore[attr-defined]
240+
chunked_body = b"".join(chunks)
241+
split_body = chunked_body.split(b"\r\n")
242+
while split_body:
243+
if split_body.pop(0):
244+
compressed.append(split_body.pop(0))
245245

246-
assert all(chunks)
247-
content = b"".join(chunks)
246+
content = b"".join(compressed)
248247
assert zlib.decompress(content) == (b"data" * 4096) + payload
249248

250249

0 commit comments

Comments
 (0)