question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kill a coroutine along with all the child coroutines that it has forked (feature request)

See original GitHub issue

I find that it is often useful to kill a coroutine and also at the same time kill all the coroutines that it has forked.

A typical example is if I have a testing coroutine that takes care of forking all the stimulus coroutines and checking coroutines. In the main test function I fork the clock, send a reset signal and then fork the testing coroutine. The main test then waits for some arbitrary number of clock cycles before resetting again, killing the testing coroutine and all it’s child coroutines, and then forking another testing coroutine. In this way I can test that the reset is working well interrupting the module at an arbitrary state.

async def do_test(dut):
    fork(send_stimulus(dut))
    await check_output(dut)

@cocotb.test(): 
def top_level_test(dut)   
    seed = 0
    n_resets = 10
    p_reset = 0.001
    do_test_task = None
    rnd = random.Random(seed)
    fork(start_clock(dut))
    for reset_index in range(n_resets):
        await triggers.RisingEdge(dut.clk)
        if do_test_task is not None:
             do_test_task.kill()
        dut.reset <= 1
        await triggers.RisingEdge(dut.clk)
        dut.reset <= 0
        do_test_task = fork(do_test(dut))
        while True:
             if rnd.random() < p_reset:
                 break
             await RisingEdge(dut.clk)

At the moment can’t just kill the coroutine, rather I also need to manually kill all the children that the coroutine has created with fork or await. Currently I’m taking care of this with a hackish solution (https://github.com/benreynwar/slvcodec/blob/master/slvcodec/cocotb_helper.py), but it would be really nice if this was something that the scheduler could take care of itself. I’m also curious about how other people are solving these kinds of issues.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
benreynwarcommented, Jul 10, 2020

I had a crack at implementing something like that in https://github.com/benreynwar/slvcodec/blob/master/slvcodec/event.py#L493

1reaction
eric-wiesercommented, Jul 10, 2020

In asyncio this would be achieved with try/finally blocks to clean up after the cancellation error. Throwing a similar error I’m cocotb after a kill would allow the same pattern.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Coroutine exceptions handling | Kotlin
We already know that a cancelled coroutine throws CancellationException in suspension points and that it is ignored by the coroutines' machinery ...
Read more >
Library Reference — cocotb 1.7.2 documentation
Schedule a coroutine to be run concurrently. See Coroutines and Tasks for details on its use. Deprecated since version 1.7.0: This function has...
Read more >
Elements of Kotlin Coroutines, Version 0.3 - CommonsWare
Trying Out Coroutines In the Klassbook . ... The Web app forks a thread to respond to a request, ... All coroutine work...
Read more >
Cooperative multitasking using coroutines (in PHP!)
All this can also be done without generators, by manually ... The main thing that coroutines add to the above functionality is the...
Read more >
curio Documentation - Read the Docs
functions defined as coroutines using the async syntax. To make a task execute, ... curio.spawn() coroutine to launch a new child task.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found