agronholm / anyio

High level asynchronous concurrency and networking framework that works on top of either trio or asyncio
MIT License
1.82k stars 139 forks source link

`SocketStream.receive` sometimes returns `bytearray` objects on Windows #776

Closed jonathanslenders closed 2 months ago

jonathanslenders commented 2 months ago

Things to check first

AnyIO version

4.4.0

Python version

3.11

What happened?

StreamProtocol, here: https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_asyncio.py#L1075 incorrectly assumes that it will only get bytes objects as an argument. Unfortunately, on Windows, that's not the case. For the ProactorEventLoop, we'll get bytearray objects here.

See:

I think the quickest fix for anyio is to accept bytearray as well, and convert it into bytes, regardless of what typeshed/cpython do.

How can we reproduce the bug?

(Sorry, I don't have access to a Windows machine right now to create an example, but please let me know if an example script is needed.)

jonathanslenders commented 2 months ago

It looks like it was fixed in cpython v3.12.0a4 thanks to this commit: https://github.com/python/cpython/commit/1bb68ba6d9de6bb7f00aee11d135123163f15887

However, it would still be good to handle the issue here for Python 3.10 and 3.11. (3.9 doesn't have the issue).

agronholm commented 2 months ago

Review welcome. As I mentioned in the CPython issue, I was able to locally reproduce this even on the latest 3.12 release.