|
1 | 1 | import asyncio |
| 2 | +import collections.abc |
2 | 3 | import unittest |
3 | 4 | import os |
4 | 5 | import textwrap |
|
14 | 15 | from unittest.mock import ANY |
15 | 16 | from test.support import ( |
16 | 17 | os_helper, |
| 18 | + threading_helper, |
17 | 19 | SHORT_TIMEOUT, |
18 | 20 | busy_retry, |
19 | 21 | requires_gil_enabled, |
@@ -763,6 +765,84 @@ async def main(): |
763 | 765 | stacks = self._extract_coroutine_funcnames(asyncio.run(main())) |
764 | 766 | self.assertEqual(stacks["consumer"], ["consumer"]) |
765 | 767 |
|
| 768 | + @skip_if_not_supported |
| 769 | + def test_running_task_stack_between_steps(self): |
| 770 | + # gh-159645 |
| 771 | + traces = [] |
| 772 | + |
| 773 | + class Loop(asyncio.SelectorEventLoop): |
| 774 | + def call_soon(self, *args, **kwargs): |
| 775 | + if asyncio.current_task(self) is not None: |
| 776 | + unwinder = RemoteUnwinder(os.getpid()) |
| 777 | + traces.append(unwinder.get_async_stack_trace()) |
| 778 | + return super().call_soon(*args, **kwargs) |
| 779 | + |
| 780 | + async def child(): |
| 781 | + await asyncio.sleep(0) |
| 782 | + |
| 783 | + async def main(): |
| 784 | + await asyncio.create_task(child(), name="child") |
| 785 | + |
| 786 | + asyncio.run(main(), loop_factory=Loop) |
| 787 | + stacks = [list(self._extract_coroutine_funcnames(trace).values()) |
| 788 | + for trace in traces] |
| 789 | + self.assertIn([["__sleep0", "sleep", "child"], ["main"]], stacks) |
| 790 | + self.assertIn([[], ["main"]], stacks) |
| 791 | + |
| 792 | + @skip_if_not_supported |
| 793 | + def test_running_task_frame_not_in_thread_stack(self): |
| 794 | + # gh-159645 |
| 795 | + ready = threading.Event() |
| 796 | + release = threading.Event() |
| 797 | + |
| 798 | + async def child(): |
| 799 | + ready.set() |
| 800 | + release.wait() |
| 801 | + await asyncio.sleep(0) |
| 802 | + |
| 803 | + async def main(): |
| 804 | + loop = asyncio.get_running_loop() |
| 805 | + coro = child() |
| 806 | + task = asyncio.create_task(coro, name="child") |
| 807 | + thread = threading.Thread(target=coro.send, args=(None,)) |
| 808 | + with threading_helper.start_threads([thread], unlock=release.set): |
| 809 | + self.assertTrue(ready.wait(SHORT_TIMEOUT)) |
| 810 | + self.assertTrue(coro.cr_running) |
| 811 | + previous = tasks._swap_current_task(loop, task) |
| 812 | + try: |
| 813 | + trace = RemoteUnwinder(os.getpid()).get_async_stack_trace() |
| 814 | + finally: |
| 815 | + tasks._swap_current_task(loop, previous) |
| 816 | + await task |
| 817 | + return trace |
| 818 | + |
| 819 | + stacks = self._extract_coroutine_funcnames(asyncio.run(main())) |
| 820 | + self.assertEqual(stacks["child"], ["child"]) |
| 821 | + |
| 822 | + @skip_if_not_supported |
| 823 | + def test_running_task_with_non_native_coroutine(self): |
| 824 | + # gh-159645 |
| 825 | + class Wrapper(collections.abc.Coroutine): |
| 826 | + def __init__(self, coro): |
| 827 | + self._coro = coro |
| 828 | + def send(self, value): |
| 829 | + return self._coro.send(value) |
| 830 | + def throw(self, *args): |
| 831 | + return self._coro.throw(*args) |
| 832 | + def close(self): |
| 833 | + return self._coro.close() |
| 834 | + def __await__(self): |
| 835 | + return self._coro.__await__() |
| 836 | + |
| 837 | + async def inner(): |
| 838 | + return RemoteUnwinder(os.getpid()).get_async_stack_trace() |
| 839 | + |
| 840 | + async def main(): |
| 841 | + return await asyncio.create_task(Wrapper(inner()), name="wrapper") |
| 842 | + |
| 843 | + stacks = self._extract_coroutine_funcnames(asyncio.run(main())) |
| 844 | + self.assertEqual(stacks["wrapper"], []) |
| 845 | + |
766 | 846 | @skip_if_not_supported |
767 | 847 | def test_self_trace_with_empty_linetable(self): |
768 | 848 | def sample(): |
|
0 commit comments