Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MemoryObjectSendStream.send(falsey) not raising BrokenResourceError when the last receive stream is closed #732

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
- Added the ``BlockingPortalProvider`` class to aid with constructing synchronous
counterparts to asynchronous interfaces that would otherwise require multiple blocking
portals
- Added ``__slots__`` to ``AsyncResource`` so that child classes can use ``__slots__``
(`#733 <https://github.com/agronholm/anyio/pull/733>`_; PR by Justin Su)
- Fixed erroneous ``RuntimeError: called 'started' twice on the same task status``
when cancelling a task in a TaskGroup created with the ``start()`` method before
the first checkpoint is reached after calling ``task_status.started()``
Expand All @@ -28,8 +30,10 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
- Emit a ``ResourceWarning`` for ``MemoryObjectReceiveStream`` and
``MemoryObjectSendStream`` that were garbage collected without being closed (PR by
Andrey Kazantcev)
- Added ``__slots__`` to ``AsyncResource`` so that child classes can use ``__slots__``
(`#733 <https://github.com/agronholm/anyio/pull/733>`_; PR by Justin Su)
- Fixed ``MemoryObjectSendStream.send()`` not raising ``BrokenResourceError`` when the
last corresponding ``MemoryObjectReceiveStream`` is closed while waiting to send a
falsey item (`#731 <https://github.com/agronholm/anyio/issues/731>`_; PR by Ganden
Schaffner)

**4.3.0**

Expand Down
3 changes: 2 additions & 1 deletion src/anyio/streams/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ async def send(self, item: T_contra) -> None:
self._state.waiting_senders.pop(send_event, None)
raise

if self._state.waiting_senders.pop(send_event, None):
if send_event in self._state.waiting_senders:
del self._state.waiting_senders[send_event]
raise BrokenResourceError from None

def clone(self) -> MemoryObjectSendStream[T_contra]:
Expand Down
5 changes: 3 additions & 2 deletions tests/streams/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,11 @@ async def test_close_send_while_receiving() -> None:


async def test_close_receive_while_sending() -> None:
send, receive = create_memory_object_stream[str](0)
# We send None here as a regression test for #731
send, receive = create_memory_object_stream[None](0)
with pytest.raises(ExceptionGroup) as exc:
async with create_task_group() as tg:
tg.start_soon(send.send, "hello")
tg.start_soon(send.send, None)
await wait_all_tasks_blocked()
await receive.aclose()

Expand Down
Loading