From 144751ef0266132a79f53c9bf4f7a7e6c019fb06 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Fri, 1 Aug 2025 09:28:49 -0400 Subject: [PATCH] fix exit crash when nostr client missing --- src/main.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main.py b/src/main.py index 3a18c6c..cf8cabd 100644 --- a/src/main.py +++ b/src/main.py @@ -459,10 +459,21 @@ def handle_view_relays(cfg_mgr: "ConfigManager") -> None: print(colored(f"Error: {e}", "red")) +def _safe_close_client_pool(pm: PasswordManager) -> None: + """Close the Nostr client pool if the client exists.""" + client = getattr(pm, "nostr_client", None) + if client is None: + return + try: + client.close_client_pool() + except Exception as exc: + logging.error(f"Error during NostrClient shutdown: {exc}") + + def _reload_relays(password_manager: PasswordManager, relays: list) -> None: """Reload NostrClient with the updated relay list.""" try: - password_manager.nostr_client.close_client_pool() + _safe_close_client_pool(password_manager) except Exception as exc: logging.warning(f"Failed to close client pool: {exc}") try: @@ -1051,7 +1062,7 @@ def display_menu( logging.info("Exiting the program.") print(colored("Exiting the program.", "green")) getattr(password_manager, "cleanup", lambda: None)() - password_manager.nostr_client.close_client_pool() + _safe_close_client_pool(password_manager) sys.exit(0) if choice == "1": while True: @@ -1255,7 +1266,7 @@ def main(argv: list[str] | None = None, *, fingerprint: str | None = None) -> in logging.info(f"Received shutdown signal: {sig}. Initiating graceful shutdown.") try: getattr(password_manager, "cleanup", lambda: None)() - password_manager.nostr_client.close_client_pool() + _safe_close_client_pool(password_manager) logging.info("NostrClient closed successfully.") except Exception as exc: logging.error(f"Error during shutdown: {exc}") @@ -1274,7 +1285,7 @@ def main(argv: list[str] | None = None, *, fingerprint: str | None = None) -> in print(colored("\nProgram terminated by user.", "yellow")) try: getattr(password_manager, "cleanup", lambda: None)() - password_manager.nostr_client.close_client_pool() + _safe_close_client_pool(password_manager) logging.info("NostrClient closed successfully.") except Exception as exc: logging.error(f"Error during shutdown: {exc}") @@ -1285,7 +1296,7 @@ def main(argv: list[str] | None = None, *, fingerprint: str | None = None) -> in print(colored(f"Error: {e}", "red")) try: getattr(password_manager, "cleanup", lambda: None)() - password_manager.nostr_client.close_client_pool() + _safe_close_client_pool(password_manager) logging.info("NostrClient closed successfully.") except Exception as exc: logging.error(f"Error during shutdown: {exc}") @@ -1296,7 +1307,7 @@ def main(argv: list[str] | None = None, *, fingerprint: str | None = None) -> in print(colored(f"Error: An unexpected error occurred: {e}", "red")) try: getattr(password_manager, "cleanup", lambda: None)() - password_manager.nostr_client.close_client_pool() + _safe_close_client_pool(password_manager) logging.info("NostrClient closed successfully.") except Exception as exc: logging.error(f"Error during shutdown: {exc}")