From 78ff72b5d80ebc277517af0cb2a5a850c638a742 Mon Sep 17 00:00:00 2001 From: SimplifiedPrivacy Date: Sun, 12 Jul 2026 17:27:04 -0400 Subject: [PATCH] Improved exception error handling --- essentials/modules/TorModule.py | 32 +++++++++++----------- essentials/observers/ConnectionObserver.py | 1 + pyproject.toml | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/essentials/modules/TorModule.py b/essentials/modules/TorModule.py index 9f18848..858d513 100644 --- a/essentials/modules/TorModule.py +++ b/essentials/modules/TorModule.py @@ -31,7 +31,7 @@ class TorModule: f"{self.state_home}/{Constants.TOR_INSTANCE_LOCK_FILENAME}" ) - def start_service(self, connection_observer: Optional[ConnectionObserver] = None): + def start_service(self, connection_observer: Optional[ConnectionObserver] = None) -> bool: Path(self.state_home).mkdir(mode=0o700, parents=True, exist_ok=True) @@ -40,9 +40,6 @@ class TorModule: print("[TOR MODULE] Starting Tor service without IPv6") - # if connection_observer is not None: - # connection_observer.notify("tor_bootstrapping") - with ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit( @@ -69,8 +66,6 @@ class TorModule: "The dedicated Tor service could not be initialized." ) - # if connection_observer is not None: - # connection_observer.notify("tor_bootstrapped") print("tor bootstrapped") try: @@ -79,12 +74,15 @@ class TorModule: ) controller.authenticate() - except (FileNotFoundError, stem.SocketError, TypeError, IndexError): + return True + except (FileNotFoundError, stem.SocketError, TypeError, IndexError) as e: + print(f"Tor startup failed: {e}") + if connection_observer: + connection_observer.notify("error", f"Tor startup failed {e}") self.stop_service() - raise TorServiceInitializationError( - "The dedicated Tor service could not be initialized." - ) + return False + def stop_service(self): control_socket_file = Path(self.control_socket_path) @@ -134,7 +132,7 @@ class TorModule: except (FileNotFoundError, stem.SocketError, TypeError, IndexError): - self.start_service(connection_observer=connection_observer) + service_begun = self.start_service(connection_observer=connection_observer) controller = stem.control.Controller.from_socket_file( self.control_socket_path @@ -148,11 +146,11 @@ class TorModule: controller.set_conf("SocksPort", socks_port_numbers) + def destroy_session(self, port_number: int): print("destroying Tor session") try: - controller = stem.control.Controller.from_socket_file( self.control_socket_path ) @@ -163,19 +161,21 @@ class TorModule: ] if len(socks_port_numbers) > 1: - socks_port_numbers = [ socks_port_number for socks_port_number in socks_port_numbers if socks_port_number != port_number ] controller.set_conf("SocksPort", socks_port_numbers) - else: controller.set_conf("SocksPort", "0") - except (FileNotFoundError, stem.SocketError, TypeError, IndexError): - pass + except (FileNotFoundError, stem.SocketError, TypeError, IndexError) as e: + error_type = e.__class__.__name__ + print(f"destroy_session failed: {error_type}: {str(e)}") + # if connection_observer: + # connection_observer.notify("error", f"Tor destroy_session: {error_type}") + @staticmethod def __on_initialization_message( diff --git a/essentials/observers/ConnectionObserver.py b/essentials/observers/ConnectionObserver.py index c13e82d..273e1fc 100644 --- a/essentials/observers/ConnectionObserver.py +++ b/essentials/observers/ConnectionObserver.py @@ -8,3 +8,4 @@ class ConnectionObserver(BaseObserver): self.on_tor_bootstrapping = [] self.on_tor_bootstrap_progressing = [] self.on_tor_bootstrapped = [] + self.on_custom_message = [] diff --git a/pyproject.toml b/pyproject.toml index 8079db6..4fc9461 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "sp-essentials" -version = "1.2.0" +version = "1.2.1" authors = [ { name = "Simplified Privacy" }, ]