From 418ac11d22a069b2435de44f6a06ebecdf91bccb Mon Sep 17 00:00:00 2001 From: Alexey Shamrin Date: Tue, 26 Jan 2021 10:06:25 +0200 Subject: [PATCH 1/3] debugging deadlocks example --- README.rst | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.rst b/README.rst index 86b60a5..2af8523 100644 --- a/README.rst +++ b/README.rst @@ -57,3 +57,40 @@ will likely undergo some incompatible API changes before an initial release. Documentation is also currently light. Watch this space! License: Your choice of MIT or Apache License 2.0 + +Debugging deadlocks in Trio programs +------------------------------------ + +``asynctb`` can help debugging deadlocks in Trio programs: + + import signal + import os + + import trio + import asynctb + + async def traceback_on_signal(): + print('pid:', os.getpid()) + with trio.open_signal_receiver(signal.SIGHUP) as signal_aiter: + async for signum in signal_aiter: + assert signum == signal.SIGHUP + for nursery, task in get_tasks(trio.lowlevel.current_root_task()): + print(f'# {task!r} in {nursery!r}') + print(asynctb.Traceback.of(task.coro)) + + def get_tasks(task, nursery = None): + yield nursery, task + for nursery in task.child_nurseries: + for task in nursery.child_tasks: + yield from get_tasks(task, nursery) + + async def main(): + # ... + async with trio.open_nursery() as nursery: + nursery.start_soon(traceback_on_signal) + + trio.run(main) + +Use SIGHUP signal to make the program print tracebacks for all running Trio tasks: + + kill -s HUP From d7faac59783238a69530ba23f5ed351b5512779d Mon Sep 17 00:00:00 2001 From: Alexey Shamrin Date: Tue, 26 Jan 2021 10:12:20 +0200 Subject: [PATCH 2/3] make rest happy --- README.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 2af8523..619b7ba 100644 --- a/README.rst +++ b/README.rst @@ -61,7 +61,7 @@ License: Your choice of MIT or Apache License 2.0 Debugging deadlocks in Trio programs ------------------------------------ -``asynctb`` can help debugging deadlocks in Trio programs: +``asynctb`` can help debugging deadlocks in Trio programs:: import signal import os @@ -69,7 +69,7 @@ Debugging deadlocks in Trio programs import trio import asynctb - async def traceback_on_signal(): + async def print_tracebacks_on_sighup(): print('pid:', os.getpid()) with trio.open_signal_receiver(signal.SIGHUP) as signal_aiter: async for signum in signal_aiter: @@ -87,10 +87,10 @@ Debugging deadlocks in Trio programs async def main(): # ... async with trio.open_nursery() as nursery: - nursery.start_soon(traceback_on_signal) + nursery.start_soon(print_tracebacks_on_sighup) trio.run(main) -Use SIGHUP signal to make the program print tracebacks for all running Trio tasks: +You can now send SIGHUP signal to make your program print tracebacks for all running Trio tasks:: kill -s HUP From 251e8dbbc83c411c03bdfbab2e025d518ff5e11d Mon Sep 17 00:00:00 2001 From: Alexey Shamrin Date: Tue, 26 Jan 2021 10:13:12 +0200 Subject: [PATCH 3/3] Update README.rst --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index 619b7ba..e0a66c0 100644 --- a/README.rst +++ b/README.rst @@ -88,6 +88,7 @@ Debugging deadlocks in Trio programs # ... async with trio.open_nursery() as nursery: nursery.start_soon(print_tracebacks_on_sighup) + # ... trio.run(main)