core[patch]: Check if event loop is closed in memory stream (#21841)

Check if event stream is closed in memory loop.

Using try/except here to avoid race condition, but this may incur a
small overhead in versions prios to 3.11
This commit is contained in:
Eugene Yurtsev
2024-05-17 17:53:59 -04:00
committed by GitHub
parent d8f89a5e9b
commit 67b6f6c82a
2 changed files with 28 additions and 2 deletions

View File

@@ -112,6 +112,24 @@ async def test_queue_for_streaming_via_sync_call() -> None:
), f"delta_time: {delta_time}"
def test_send_to_closed_stream() -> None:
"""Test that sending to a closed stream doesn't raise an error.
We may want to handle this in a better way in the future.
"""
event_loop = asyncio.get_event_loop()
channel = _MemoryStream[str](event_loop)
writer = channel.get_send_stream()
# send with an open even loop
writer.send_nowait("hello")
event_loop.close()
writer.send_nowait("hello")
# now close the loop
event_loop.close()
writer.close()
writer.send_nowait("hello")
async def test_closed_stream() -> None:
reader_loop = asyncio.get_event_loop()
channel = _MemoryStream[str](reader_loop)