From 0e344eead877c30bca4adad37db9bdb729694392 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 12 Oct 2020 08:56:49 -0400 Subject: [PATCH] Add a "cancel arrives during a sync sleep in child" test This appears to demonstrate the same bug found in #156. It looks like cancelling a subactor with a child, while that child is running sync code, can result in the child never getting cancelled due to some strange condition where the internal nurseries aren't being torn down as expected when a `trio.Cancelled` is raised. --- tests/test_cancellation.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test_cancellation.py b/tests/test_cancellation.py index dbb05024d..80232bfd0 100644 --- a/tests/test_cancellation.py +++ b/tests/test_cancellation.py @@ -413,3 +413,34 @@ async def main(): with pytest.raises(KeyboardInterrupt): tractor.run(main) + + + +async def spin_for(period=3): + "Sync sleep." + time.sleep(period) + + +async def spawn(): + async with tractor.open_nursery() as tn: + portal = await tn.run_in_actor('sleeper', spin_for) + + +def test_cancel_while_childs_child_in_sync_sleep( + loglevel, + start_method, + spawn_backend, +): + """Verify that a child cancelled while executing sync code is torn + down even when that cancellation is triggered by the parent + 2 nurseries "up". + """ + async def main(): + with trio.fail_after(2): + async with tractor.open_nursery() as tn: + portal = await tn.run_in_actor('spawn', spawn) + await trio.sleep(1) + assert 0 + + with pytest.raises(AssertionError): + tractor.run(main)