Skip to content

Conversation

@bdraco
Copy link
Member

@bdraco bdraco commented Oct 27, 2025

What do these changes do?

Fixes data corruption in WebSocket compressed messages when send operations are cancelled. WebSocket uses a stateful compressor that persists across the connection lifetime. If a send_frame() call is cancelled during compression, the compressor state becomes corrupted, causing subsequent messages to contain mixed/corrupted data.

The fix wraps compressed sends in asyncio.shield() + lock pattern to ensure atomicity:

  • Shield ensures compression completes even if task is cancelled
  • Lock serializes sends to prevent interleaving
  • Python 3.12+ optimization uses eager task creation

HTTP doesn't need this protection because connections close on error, getting a fresh compressor for the next request.

Are there changes in behavior for the user?

No breaking changes. This is a bug fix that prevents data corruption. Users may notice:

  • Cancelled WebSocket sends now complete in the background (shielded)
  • Slightly different cancellation timing for compressed messages only

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

No. The implementation is straightforward

The fix follows established patterns and includes cancellation tests for both single and multiple concurrent sends across all zlib backends.

Related issue number

Fixes #11725

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 27, 2025
@bdraco bdraco added backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot labels Oct 27, 2025
@bdraco
Copy link
Member Author

bdraco commented Oct 27, 2025

real world performance is awful for small compressed payloads. codspeed will likely show this as well. Our current tests will actually undershow it

@bdraco
Copy link
Member Author

bdraco commented Oct 27, 2025

I need to optimize it for the small payload case which is the majority of cases

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 27, 2025

CodSpeed Performance Report

Merging #11726 will not alter performance

Comparing websocket_send_cancel_safe_large_payloads (7d6154e) with master (0d77d0d)

Summary

✅ 59 untouched

@codecov
Copy link

codecov bot commented Oct 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.73%. Comparing base (1fbb1bb) to head (7d6154e).
⚠️ Report is 3 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #11726    +/-   ##
========================================
  Coverage   98.73%   98.73%            
========================================
  Files         127      127            
  Lines       43546    43663   +117     
  Branches     2320     2325     +5     
========================================
+ Hits        42996    43112   +116     
  Misses        390      390            
- Partials      160      161     +1     
Flag Coverage Δ
CI-GHA 98.60% <99.12%> (-0.01%) ⬇️
OS-Linux 98.34% <99.12%> (+<0.01%) ⬆️
OS-Windows 96.67% <99.12%> (-0.01%) ⬇️
OS-macOS 97.56% <99.12%> (+<0.01%) ⬆️
Py-3.10.11 97.11% <98.24%> (+<0.01%) ⬆️
Py-3.10.18 97.60% <98.24%> (-0.01%) ⬇️
Py-3.11.13 97.81% <98.24%> (+<0.01%) ⬆️
Py-3.11.9 97.32% <98.24%> (-0.01%) ⬇️
Py-3.12.10 97.42% <98.24%> (+<0.01%) ⬆️
Py-3.12.11 97.91% <98.24%> (+<0.01%) ⬆️
Py-3.13.7 97.91% <98.24%> (-0.01%) ⬇️
Py-3.13.9 97.39% <98.24%> (-0.01%) ⬇️
Py-3.14.0 98.12% <98.24%> (+<0.01%) ⬆️
Py-3.14.0t 97.18% <98.24%> (+<0.01%) ⬆️
Py-pypy3.10.16-7.3.19 91.31% <77.19%> (-4.07%) ⬇️
VM-macos 97.56% <99.12%> (+<0.01%) ⬆️
VM-ubuntu 98.34% <99.12%> (+<0.01%) ⬆️
VM-windows 96.67% <99.12%> (-0.01%) ⬇️

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

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

@bdraco bdraco marked this pull request as ready for review October 27, 2025 21:19
@bdraco bdraco requested a review from Copilot October 27, 2025 21:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a data corruption bug in WebSocket compressed message sending when operations are cancelled. The WebSocket connection maintains a stateful compressor that can become corrupted if send_frame() is cancelled during compression, causing subsequent messages to contain mixed data.

Key changes:

  • Implements shield + lock pattern for large compressed sends to ensure atomicity
  • Synchronous compression path for small payloads (≤16KiB) with lock protection
  • Removed lock from ZLibCompressor.compress() and added cancellation safety warnings

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
aiohttp/_websocket/writer.py Refactored send_frame() to use shield+lock pattern for large compressed frames; added sync compression path for small frames
aiohttp/compression_utils.py Removed internal lock from ZLibCompressor.compress(); added documentation warnings about cancellation safety
tests/test_websocket_writer.py Added regression tests for single and multiple cancelled compression operations
tests/conftest.py Added slow_executor fixture to simulate slow operations for testing race conditions
CHANGES/11725.bugfix.rst Added changelog entry for the bug fix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bdraco bdraco marked this pull request as draft October 27, 2025 21:21
@bdraco bdraco marked this pull request as ready for review October 27, 2025 22:08
@bdraco bdraco merged commit 6cffcfd into master Oct 28, 2025
41 checks passed
@bdraco bdraco deleted the websocket_send_cancel_safe_large_payloads branch October 28, 2025 17:10
@patchback
Copy link
Contributor

patchback bot commented Oct 28, 2025

Backport to 3.13: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 6cffcfd on top of patchback/backports/3.13/6cffcfd749734551c0c824658f3a5c9250e9a165/pr-11726

Backporting merged PR #11726 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.13/6cffcfd749734551c0c824658f3a5c9250e9a165/pr-11726 upstream/3.13
  4. Now, cherry-pick PR Fix WebSocket compressed sends to be cancellation safe #11726 contents into that branch:
    $ git cherry-pick -x 6cffcfd749734551c0c824658f3a5c9250e9a165
    If it'll yell at you with something like fatal: Commit 6cffcfd749734551c0c824658f3a5c9250e9a165 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 6cffcfd749734551c0c824658f3a5c9250e9a165
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Fix WebSocket compressed sends to be cancellation safe #11726 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.13/6cffcfd749734551c0c824658f3a5c9250e9a165/pr-11726
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@patchback
Copy link
Contributor

patchback bot commented Oct 28, 2025

Backport to 3.14: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 6cffcfd on top of patchback/backports/3.14/6cffcfd749734551c0c824658f3a5c9250e9a165/pr-11726

Backporting merged PR #11726 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.14/6cffcfd749734551c0c824658f3a5c9250e9a165/pr-11726 upstream/3.14
  4. Now, cherry-pick PR Fix WebSocket compressed sends to be cancellation safe #11726 contents into that branch:
    $ git cherry-pick -x 6cffcfd749734551c0c824658f3a5c9250e9a165
    If it'll yell at you with something like fatal: Commit 6cffcfd749734551c0c824658f3a5c9250e9a165 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 6cffcfd749734551c0c824658f3a5c9250e9a165
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Fix WebSocket compressed sends to be cancellation safe #11726 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.14/6cffcfd749734551c0c824658f3a5c9250e9a165/pr-11726
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

bdraco added a commit that referenced this pull request Oct 28, 2025
bdraco added a commit that referenced this pull request Oct 28, 2025
bdraco added a commit that referenced this pull request Oct 28, 2025
bdraco added a commit that referenced this pull request Oct 28, 2025
bdraco added a commit that referenced this pull request Oct 28, 2025
bdraco added a commit that referenced this pull request Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Websocket send is not cancellation safe when compression is enabled

3 participants