Skip to content

Commit a96cd85

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 83d5b60 commit a96cd85

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
@@ -352,12 +352,15 @@ async def until(self, condition: ValueLike):
352352
raise TypeError(f"The shape of a condition may only be `signed` or `unsigned`, "
353353
f"not {shape!r}")
354354
tick = self.sample(condition).__aiter__()
355-
done = False
356-
while not done:
357-
clk, rst, *values, done = await tick.__anext__()
358-
if rst:
359-
raise DomainReset
360-
return tuple(values)
355+
try:
356+
done = False
357+
while not done:
358+
clk, rst, *values, done = await tick.__anext__()
359+
if rst:
360+
raise DomainReset
361+
return tuple(values)
362+
finally:
363+
await tick.aclose()
361364

362365
async def repeat(self, count: int):
363366
"""Repeat this trigger a specific number of times.
@@ -390,12 +393,15 @@ async def repeat(self, count: int):
390393
if count <= 0:
391394
raise ValueError(f"Repeat count must be a positive integer, not {count!r}")
392395
tick = self.__aiter__()
393-
for _ in range(count):
394-
clk, rst, *values = await tick.__anext__()
395-
if rst:
396-
raise DomainReset
397-
assert clk
398-
return tuple(values)
396+
try:
397+
for _ in range(count):
398+
clk, rst, *values = await tick.__anext__()
399+
if rst:
400+
raise DomainReset
401+
assert clk
402+
return tuple(values)
403+
finally:
404+
await tick.aclose()
399405

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

0 commit comments

Comments
 (0)