From 23463fb61aacdd04a001858fce308ffcdc0925d8 Mon Sep 17 00:00:00 2001 From: Sasha Romijn Date: Mon, 6 Feb 2023 14:08:25 +0100 Subject: [PATCH] Ref #193 - Add additional logging in set resolve and traceback on SIGUSR1 --- irrexplorer/api/collectors.py | 2 ++ irrexplorer/app.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/irrexplorer/api/collectors.py b/irrexplorer/api/collectors.py index 8e39aaf..b654e79 100644 --- a/irrexplorer/api/collectors.py +++ b/irrexplorer/api/collectors.py @@ -219,6 +219,7 @@ def is_set(set_name: str) -> bool: results = [] def traverse_tree(stub_name: str, depth: int = 0, path: Optional[List[str]] = None) -> None: + print(f"traverse_tree called with: stub_name={stub_name} depth={depth} path={path}") if path is None: path = [] if stub_name in path: @@ -236,6 +237,7 @@ def traverse_tree(stub_name: str, depth: int = 0, path: Optional[List[str]] = No if sub_member in resolved: traverse_tree(sub_member, depth, path) + print("completed initial resolve loop, traversing tree") traverse_tree(name) results.sort(key=lambda item: (item.depth, item.name)) diff --git a/irrexplorer/app.py b/irrexplorer/app.py index db005da..f12ad23 100644 --- a/irrexplorer/app.py +++ b/irrexplorer/app.py @@ -1,3 +1,9 @@ +import os +import signal +import sys +import threading +import traceback + import databases from starlette.applications import Starlette from starlette.middleware import Middleware @@ -10,6 +16,7 @@ async def startup(): + signal.signal(signal.SIGUSR1, sigusr1_handler) app.state.database = databases.Database(DATABASE_URL, force_rollback=TESTING) await app.state.database.connect() @@ -48,3 +55,13 @@ async def shutdown(): on_startup=[startup], on_shutdown=[shutdown], ) + + +def sigusr1_handler(signal, frame): + thread_names = {th.ident: th.name for th in threading.enumerate()} + code = [f"Traceback follows for all threads of process {os.getpid()}:"] + for thread_id, stack in sys._current_frames().items(): + thread_name = thread_names.get(thread_id, "") + code.append(f"\n## Thread: {thread_name}({thread_id}) ##\n") + code += traceback.format_list(traceback.extract_stack(stack)) + print("".join(code))