Skip to content

Commit 2d75df7

Browse files
committed
Disallow fetching from stopped LatestValueCaches
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent ea1ebb0 commit 2d75df7

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

src/frequenz/channels/_latest_value_cache.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(
7575
self._task: asyncio.Task[None] = asyncio.create_task(
7676
self._run(), name=f"LatestValueCache«{self._unique_id}»"
7777
)
78+
self._stopped: bool = False
7879

7980
@property
8081
def unique_id(self) -> str:
@@ -96,6 +97,8 @@ def get(self) -> T_co:
9697
"""
9798
if isinstance(self._latest_value, _Sentinel):
9899
raise ValueError("No value has been received yet.")
100+
if self._stopped:
101+
raise ValueError("Cache has been stopped.")
99102
return self._latest_value
100103

101104
def has_value(self) -> bool:
@@ -113,6 +116,7 @@ async def _run(self) -> None:
113116
async def stop(self) -> None:
114117
"""Stop the cache and close the owned receiver."""
115118
self._receiver.close()
119+
self._stopped = True
116120
if not self._task.done():
117121
self._task.cancel()
118122
try:

0 commit comments

Comments
 (0)