Visitar URL original
Fix byte-length handling for stream memoryviews by 0718lol · Pull Request #14327 · aio-libs/aiohttp · GitHub
Skip to content

Fix byte-length handling for stream memoryviews - #14327

Draft
0718lol wants to merge 3 commits into
aio-libs:masterfrom
0718lol:fix/stream-memoryview-byte-length
Draft

0718lol wants to merge 3 commits into
aio-libs:masterfrom
0718lol:fix/stream-memoryview-byte-length

Conversation

@0718lol

@0718lol 0718lol commented Nov 9, 2026

Copy link
Copy Markdown

What do these changes do?

Fix direct stream writes of C-contiguous memoryviews. write() now checks dimensions and element size without calling len() on scalar views. write_eof() performs the same byte-view normalization before framing or transport writes, and uses nbytes for progress accounting. Empty multidimensional views become an empty chunk without attempting an invalid cast.

For example, an 8-byte view cast to H previously produced a final HTTP chunk advertised as 4 bytes, which the client rejected. It now produces an 8-byte chunk containing the original bytes. Scalar views no longer raise TypeError.

Are there changes in behavior for the user?

Direct StreamResponse writes send the raw buffer bytes independently of element size and shape. The writer preserves the original view for the chunk callback, and casts nonempty contiguous views without copying. This is separate from #14019's BytesPayload change: the new HTTP tests use StreamResponse directly.

Is it a substantial burden for the maintainers to support this?

The change extends the existing normalization in the writer to its final-chunk path, with focused regressions and a short reference-doc update. It adds no dependency or public API.

Related issue number

Fixes #14326.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • Added the contributor handle 0718lol to CONTRIBUTORS.txt in sorted order (the account has no public full name).
  • Added a news fragment under CHANGES/.
Local validation

Base: 8f51d5be21bbd021160079d9d6dae739814bd0cf. macOS arm64, CPython 3.12.15, pure Python mode.

The same 38 regression cases produced 19 failures / 19 passes with baseline production code and 38 passes with the fix. They cover chunk framing with buffered and unbuffered headers, output/progress byte counts, callback inputs, byte limits, and 12 real HTTP round trips with and without compression.

AIOHTTP_NO_EXTENSIONS=1 python -X dev -m pytest -p no:aiohttp -o addopts='' \
  -m 'not autobahn and not internal' \
  tests/test_http_writer.py tests/test_client_functional.py \
  tests/test_web_response.py tests/test_payload.py tests/test_client_request.py \
  -q --tb=short --timeout=15
994 passed, 71 skipped, 11 deselected, 1 xfailed

mypy --follow-imports=silent aiohttp/http_writer.py \
  tests/test_http_writer.py tests/test_client_functional.py
Success: no issues found in 3 source files

All applicable repository pre-commit hooks passed, as did git diff --check. The full repository suite, other interpreters, and compiled extensions were not run. No parser or WebSocket code changes are included.

Drafted with OpenAI Codex (GPT-6); human review by 0718lol pending. This PR remains a draft until that review occurs, as required by AGENTS.md.

@agentscanapp

agentscanapp Bot commented Nov 9, 2026

Copy link
Copy Markdown

Automation signals

Activity patterns show signs of automation.

View full analysis →

Report this account →

Evidence
  • Multiple forks: 7 repositories forked in a single 24-hour window
  • Issue comment and PR within minutes: Commented on an issue and opened a PR to the same repo within 5 minutes, in 2 repositories (the fastest took 1s)

Last 2 PRs:

This is an automated analysis by AgentScan

@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.11%. Comparing base (8f51d5b) to head (e3c6d19).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14327   +/-   ##
=======================================
  Coverage   99.11%   99.11%           
=======================================
  Files         135      135           
  Lines       53439    53494   +55     
  Branches     2723     2728    +5     
=======================================
+ Hits        52967    53022   +55     
  Misses        358      358           
  Partials      114      114           
Flag Coverage Δ
Autobahn 21.67% <19.67%> (-0.01%) ⬇️
CI-GHA 99.05% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.69% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.48% <100.00%> (-0.01%) ⬇️
OS-macOS 98.35% <100.00%> (+<0.01%) ⬆️
Py-3.11 98.29% <100.00%> (-0.01%) ⬇️
Py-3.12 98.38% <100.00%> (+<0.01%) ⬆️
Py-3.13 98.36% <100.00%> (+<0.01%) ⬆️
Py-3.14 98.40% <100.00%> (+<0.01%) ⬆️
Py-3.15 98.39% <100.00%> (+<0.01%) ⬆️
Py-3.15t 98.24% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.12 96.96% <100.00%> (+0.01%) ⬆️
VM-macos-latest 98.35% <100.00%> (+<0.01%) ⬆️
VM-ubuntu-latest 98.69% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 97.48% <100.00%> (-0.01%) ⬇️
cython-coverage 83.84% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@codspeed

codspeed Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 48 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 102 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing 0718lol:fix/stream-memoryview-byte-length (e3c6d19) with master (8f51d5b)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@0718lol

0718lol commented Nov 9, 2026

Copy link
Copy Markdown
Author

CI is now passing except for “Backport label added”. The affected code exists in both 3.14 and 3.15. Could a maintainer add the appropriate backport labels? I don’t have permission to set upstream labels.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Direct stream writes mishandle scalar and final typed memoryviews

1 participant