Skip to content

Commit 3843e3f

Browse files
committed
Close underlying channel when all receivers are gone
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 5351d41 commit 3843e3f

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/frequenz/channels/_broadcast.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ async def send(self, message: _T, /) -> None:
391391
for _hash in stale_refs:
392392
del self._channel._receivers[_hash]
393393
if self._channel._auto_close_enabled and len(self._channel._receivers) == 0:
394+
await self._channel.aclose()
394395
raise SenderError("The channel was closed", self) from ChannelClosedError(
395396
self._channel
396397
)

tests/test_broadcast.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,18 @@ async def test_broadcast_auto_close_2() -> None:
473473
with pytest.raises(ReceiverStoppedError) as excinfo:
474474
await receiver.receive()
475475
assert isinstance(excinfo.value.__cause__, ChannelClosedError)
476+
477+
478+
async def test_broadcast_closed_channels_remain_closed() -> None:
479+
"""Ensure that a closed channel can't be resurrected."""
480+
sender, receiver = BroadcastChannel[int](name="auto-close-test")
481+
482+
receiver.close()
483+
484+
with pytest.raises(SenderError) as excinfo:
485+
await sender.send(1)
486+
assert isinstance(excinfo.value.__cause__, ChannelClosedError)
487+
488+
with pytest.raises(SenderError) as excinfo:
489+
_ = sender.subscribe()
490+
assert isinstance(excinfo.value.__cause__, ChannelClosedError)

0 commit comments

Comments
 (0)