Skip to content

Fix exceptions in websocket receive_* methods#9511

Merged
bdraco merged 22 commits intoaio-libs:masterfrom
ara-25:fix_exceptions_ws_receive_content
Nov 5, 2024
Merged

Fix exceptions in websocket receive_* methods#9511
bdraco merged 22 commits intoaio-libs:masterfrom
ara-25:fix_exceptions_ws_receive_content

Conversation

@ara-25
Copy link
Copy Markdown
Contributor

@ara-25 ara-25 commented Oct 20, 2024

What do these changes do?

  • Add new WSMessageTypeError exception
  • Raise WSMessageTypeError exception in receive_* methods in WebSocketResponse and ClientWebSocketResponse, when message without any content is received.

Are there changes in behavior for the user?

Websocket receive_str and receive_bytes methods will now raise WSMessageTypeError, instead of TypeError, when websocket messages without content are received.

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

No

Related issue number

Fixes #6800

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 20, 2024
@codecov
Copy link
Copy Markdown

codecov bot commented Oct 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.66%. Comparing base (5b654d5) to head (3b85fcb).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9511      +/-   ##
==========================================
+ Coverage   98.65%   98.66%   +0.01%     
==========================================
  Files         116      116              
  Lines       35482    35526      +44     
  Branches     4211     4215       +4     
==========================================
+ Hits        35003    35051      +48     
+ Misses        321      319       -2     
+ Partials      158      156       -2     
Flag Coverage Δ
CI-GHA 98.54% <100.00%> (+0.01%) ⬆️
OS-Linux 98.22% <100.00%> (+0.01%) ⬆️
OS-Windows 96.10% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.41% <100.00%> (+0.01%) ⬆️
Py-3.10.11 97.27% <96.29%> (+<0.01%) ⬆️
Py-3.10.15 97.70% <96.29%> (+<0.01%) ⬆️
Py-3.11.10 97.76% <96.29%> (+<0.01%) ⬆️
Py-3.11.9 97.34% <96.29%> (-0.01%) ⬇️
Py-3.12.7 98.24% <96.29%> (+<0.01%) ⬆️
Py-3.13.0 98.23% <96.29%> (+<0.01%) ⬆️
Py-3.9.13 97.19% <100.00%> (+0.01%) ⬆️
Py-3.9.20 97.61% <100.00%> (+0.01%) ⬆️
Py-pypy7.3.16 97.23% <100.00%> (+0.01%) ⬆️
VM-macos 97.41% <100.00%> (+0.01%) ⬆️
VM-ubuntu 98.22% <100.00%> (+0.01%) ⬆️
VM-windows 96.10% <100.00%> (+<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.

Comment thread aiohttp/client_ws.py Outdated
@Dreamsorcerer
Copy link
Copy Markdown
Member

But, should we use a connection error instead of RuntimeError as suggested by someone in that issue? That would make it easier for someone to catch the exception, as TypeError/RuntimeError is not really obvious. @bdraco Any opinions on the exception?

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Oct 20, 2024

Changing it from TypeError to RuntimeError is a breaking change. I think it would be safer to make a new exception that is derived from TypeError

Comment thread aiohttp/client_ws.py Outdated
@ara-25
Copy link
Copy Markdown
Contributor Author

ara-25 commented Oct 21, 2024

Changing it from TypeError to RuntimeError is a breaking change. I think it would be safer to make a new exception that is derived from TypeError

Should the new exception be included in the client_exceptions.py module?

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Oct 21, 2024

Changing it from TypeError to RuntimeError is a breaking change. I think it would be safer to make a new exception that is derived from TypeError

Should the new exception be included in the client_exceptions.py module?

I think one in web_exceptions for web_ws, and one in client_exceptions for client_ws

@ara-25
Copy link
Copy Markdown
Contributor Author

ara-25 commented Oct 22, 2024

@bdraco updated with the suggested changes.

I have added the exception in just client_exceptions.py as web_exceptions.py seemed to contain http error related exceptions.

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Oct 28, 2024

Would you please update the change log message and the opening text as well to reflect the most recent changes

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Oct 28, 2024

CodSpeed Performance Report

Merging #9511 will not alter performance

Comparing ara-25:fix_exceptions_ws_receive_content (3b85fcb) with master (5b654d5)

Summary

✅ 13 untouched benchmarks

@ara-25
Copy link
Copy Markdown
Contributor Author

ara-25 commented Oct 28, 2024

@bdraco updated. Please have a look

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Oct 28, 2024

It looks like there are 4 lines that need test coverage. Thanks

Comment thread tests/test_client_ws_functional.py Outdated
@ara-25
Copy link
Copy Markdown
Contributor Author

ara-25 commented Oct 29, 2024

@bdraco I have completed tests for these changes

@ara-25
Copy link
Copy Markdown
Contributor Author

ara-25 commented Oct 29, 2024

There might still be 2 uncovered lines showing up in the report, but they are not a part of the changes done in this PR and just include formatting changes.

Let me know if you need me to add tests for those as well. Thanks

Comment thread aiohttp/web_ws.py Outdated
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Oct 30, 2024

The object paths can be a bit tricky. Check CHANGES.rst for previous usage to figure them out

https://github.com/aio-libs/aiohttp/actions/runs/11586225078/job/32256466974#step:13:1

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Oct 30, 2024

Once it's all passing check the read the docs preview as it will show how it will appear in the changelog

@ara-25
Copy link
Copy Markdown
Contributor Author

ara-25 commented Oct 30, 2024

Fixed the changelog refs and also made some changes to the docs.

I had to add documentation for the WSMessageTypeError in docs/client_reference.rst for its reference to correctly show up in changelog.

Additionally, can you please guide me about how where this exception should be added in the Hierarchy of exceptions section:

Hierarchy of exceptions
^^^^^^^^^^^^^^^^^^^^^^^
* :exc:`ClientError`
* :exc:`ClientConnectionError`
* :exc:`ClientConnectionResetError`
* :exc:`ClientOSError`

Comment thread docs/client_reference.rst Outdated
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 2, 2024

Preview of the changelog message (ignore highlighting)

Screenshot 2024-11-02 at 1 46 52 PM

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 2, 2024

Additionally, can you please guide me about how where this exception should be added in the Hierarchy of exceptions section:

I will take a look as soon as I can. Ran out of time for today.

Copy link
Copy Markdown
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

Please don't merge until tomorrow. I need more time to review the PR. Thanks

@asvetlov
Copy link
Copy Markdown
Member

asvetlov commented Nov 4, 2024

Not sure if WSMessageTypeError should be mentioned in exceptions hierarchy because it is the standalone class which is inherited from TypeError only; it doesn't belong to exceptions tree.

@asvetlov
Copy link
Copy Markdown
Member

asvetlov commented Nov 4, 2024

@bdraco please feel free to merge if you have no objections.

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 4, 2024

Will run through it later today. Thanks

@ara-25
Copy link
Copy Markdown
Contributor Author

ara-25 commented Nov 5, 2024

Also @bdraco, I was thinking if it would be a good idea to add the websocket message-type and optionally data information in the exception class so that the end users can use those if required:

raise WSMessageTypeError(
                f"Received message {msg.type}:{msg.data!r} is not WSMsgType.TEXT", received_msg_type=msg.type, received_msg_data=data, 
    

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 5, 2024

I'd worry a bit about storing a strong reference the whole payload in the Exception as Exceptions tend to get logged and collected by various systems which may have some undesirable memory impact

Comment thread aiohttp/client_exceptions.py Outdated
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 5, 2024

Let's not expand the scope of this PR. We can consider other changes in a followup

@bdraco bdraco merged commit 75ae623 into aio-libs:master Nov 5, 2024
@patchback
Copy link
Copy Markdown
Contributor

patchback bot commented Nov 5, 2024

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

❌ Failed to cleanly apply 75ae623 on top of patchback/backports/3.11/75ae6236547f78fce9debb3788407311a7b8ecbb/pr-9511

Backporting merged PR #9511 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.11/75ae6236547f78fce9debb3788407311a7b8ecbb/pr-9511 upstream/3.11
  4. Now, cherry-pick PR Fix exceptions in websocket receive_* methods #9511 contents into that branch:
    $ git cherry-pick -x 75ae6236547f78fce9debb3788407311a7b8ecbb
    If it'll yell at you with something like fatal: Commit 75ae6236547f78fce9debb3788407311a7b8ecbb is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 75ae6236547f78fce9debb3788407311a7b8ecbb
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Fix exceptions in websocket receive_* methods #9511 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.11/75ae6236547f78fce9debb3788407311a7b8ecbb/pr-9511
  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 Nov 5, 2024
Co-authored-by: J. Nick Koston <[email protected]>
(cherry picked from commit 75ae623)
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 5, 2024

backport #9676

bdraco added a commit that referenced this pull request Nov 5, 2024
…_* methods (#9676)

Co-authored-by: J. Nick Koston <[email protected]>
Co-authored-by: ara-25 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

EofStream and websocket.receive_* functions

4 participants