Skip to content

Commit f3ac270

Browse files
committed
sim: call .aclose() on TickTrigger in .until() and .repeat().
Otherwise, when used together with asyncio, the following warning will be printed for each use of `.until()`/`.repeat()`: E: asyncio: Task was destroyed but it is pending! task: <Task pending name='Task-2' coro=<<async_generator_athrow without __name__>()>> /usr/lib/python3.13/asyncio/base_events.py:744: RuntimeWarning: coroutine method 'aclose' of 'TickTrigger.__aiter__' was never awaited
1 parent fd41201 commit f3ac270

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

amaranth/sim/_async.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,15 @@ async def until(self, condition: ValueLike):
362362
raise TypeError(f"The shape of a condition may only be `signed` or `unsigned`, "
363363
f"not {shape!r}")
364364
tick = self.sample(condition).__aiter__()
365-
done = False
366-
while not done:
367-
clk, rst, *values, done = await tick.__anext__()
368-
if rst:
369-
raise DomainReset
370-
return tuple(values)
365+
try:
366+
done = False
367+
while not done:
368+
clk, rst, *values, done = await tick.__anext__()
369+
if rst:
370+
raise DomainReset
371+
return tuple(values)
372+
finally:
373+
await tick.aclose()
371374

372375
async def repeat(self, count: int):
373376
"""Repeat this trigger a specific number of times.
@@ -400,12 +403,15 @@ async def repeat(self, count: int):
400403
if count <= 0:
401404
raise ValueError(f"Repeat count must be a positive integer, not {count!r}")
402405
tick = self.__aiter__()
403-
for _ in range(count):
404-
clk, rst, *values = await tick.__anext__()
405-
if rst:
406-
raise DomainReset
407-
assert clk
408-
return tuple(values)
406+
try:
407+
for _ in range(count):
408+
clk, rst, *values = await tick.__anext__()
409+
if rst:
410+
raise DomainReset
411+
assert clk
412+
return tuple(values)
413+
finally:
414+
await tick.aclose()
409415

410416
def _collect_trigger(self):
411417
clk_polarity = (1 if self._domain.clk_edge == "pos" else 0)

0 commit comments

Comments
 (0)