Skip to content

Running Simulator inside asyncio eventloop and breaking out of async for trigger: ... causes runtime warning #1647

@Sazzach

Description

@Sazzach

I was writing a testcase in for a Glasgow applet, and my testbench emitted a bunch (~200) of warnings saying Task was destroyed but it is pending!

I did some investigation, and came up with the following minimal example:

import asyncio

from amaranth import *
from amaranth.sim import Simulator


def setup():
    m = Module()
    foo = Signal()
    m.d.sync += foo.eq(1)

    async def tb(ctx):
        async for _ in ctx.tick():
            break

    sim = Simulator(m)
    sim.add_clock(1e-6)
    sim.add_testbench(tb, background=False)
    sim.run()


async def async_setup():
    setup()


# no warning when running setup directly
#setup()
asyncio.new_event_loop().run_until_complete(async_setup())

Output:

samuelz@sz-t14 ~/D/g/async_for_test (main)> uv run --python 3.13 python -X tracemalloc asyncio_tb.py
<sys>:0: RuntimeWarning: coroutine method 'aclose' of 'TickTrigger.__aiter__' was never awaited
Object allocated at (most recent call last):
  File "/home/samuelz/Documents/glasgow_devel/async_for_test/asyncio_tb.py", lineno 13
    async for _ in ctx.tick():
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<<async_generator_athrow without __name__>()>>

If this isn't a issue with the simulator and running it within an asyncio event loop is unsupported I'd be happy to make a PR to Glasgow to fix it there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions