From 448ea8ceb510dbf02ed737b5cca302b648bb0c42 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 15 Oct 2021 14:36:50 +0200 Subject: [PATCH] Added try statements for various callbacks --- RNS/Destination.py | 6 +++++- RNS/Link.py | 48 ++++++++++++++++++++++++++++++++++++---------- RNS/Packet.py | 14 +++++++++++--- RNS/Resource.py | 32 ++++++++++++++++++++++++------- RNS/Transport.py | 22 +++++++++++++-------- 5 files changed, 93 insertions(+), 29 deletions(-) diff --git a/RNS/Destination.py b/RNS/Destination.py index 2d24fbb..0e74936 100755 --- a/RNS/Destination.py +++ b/RNS/Destination.py @@ -263,7 +263,11 @@ class Destination: if plaintext != None: if packet.packet_type == RNS.Packet.DATA: if self.callbacks.packet != None: - self.callbacks.packet(plaintext, packet) + try: + self.callbacks.packet(plaintext, packet) + except Exception as e: + RNS.log("Error while executing receive callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) + def incoming_link_request(self, data, packet): link = RNS.Link.validate_request(self, data, packet) diff --git a/RNS/Link.py b/RNS/Link.py index 34d7b1b..5a93ef3 100644 --- a/RNS/Link.py +++ b/RNS/Link.py @@ -425,7 +425,11 @@ class Link: self.destination.links.remove(self) if self.callbacks.link_closed != None: - self.callbacks.link_closed(self) + try: + self.callbacks.link_closed(self) + except Exception as e: + RNS.log("Error while executing link closed callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) + def start_watchdog(self): thread = threading.Thread(target=self.__watchdog_job) @@ -598,7 +602,10 @@ class Link: elif self.destination.proof_strategy == RNS.Destination.PROVE_APP: if self.destination.callbacks.proof_requested: - self.destination.callbacks.proof_requested(packet) + try: + self.destination.callbacks.proof_requested(packet) + except Exception as e: + RNS.log("Error while executing proof request callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) elif packet.context == RNS.Packet.LINKIDENTIFY: plaintext = self.decrypt(packet.data) @@ -613,7 +620,10 @@ class Link: if identity.validate(signature, signed_data): self.__remote_identity = identity if self.callbacks.remote_identified != None: - self.callbacks.remote_identified(self.__remote_identity) + try: + self.callbacks.remote_identified(self.__remote_identity) + except Exception as e: + RNS.log("Error while executing remote identified callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) elif packet.context == RNS.Packet.REQUEST: try: @@ -659,8 +669,11 @@ class Link: pass elif self.resource_strategy == Link.ACCEPT_APP: if self.callbacks.resource != None: - if self.callbacks.resource(resource): - RNS.Resource.accept(packet, self.callbacks.resource_concluded) + try: + if self.callbacks.resource(resource): + RNS.Resource.accept(packet, self.callbacks.resource_concluded) + except Exception as e: + RNS.log("Error while executing resource accept callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) elif self.resource_strategy == Link.ACCEPT_ALL: RNS.Resource.accept(packet, self.callbacks.resource_concluded) @@ -933,7 +946,10 @@ class RequestReceipt(): self.link.pending_requests.remove(self) if self.callbacks.failed != None: - self.callbacks.failed(self) + try: + self.callbacks.failed(self) + except Exception as e: + RNS.log("Error while executing request failed callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) def __response_timeout_job(self): @@ -951,7 +967,10 @@ class RequestReceipt(): self.link.pending_requests.remove(self) if self.callbacks.failed != None: - self.callbacks.failed(self) + try: + self.callbacks.failed(self) + except Exception as e: + RNS.log("Error while executing request timed out callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) def response_resource_progress(self, resource): @@ -967,7 +986,10 @@ class RequestReceipt(): self.progress = resource.get_progress() if self.callbacks.progress != None: - self.callbacks.progress(self) + try: + self.callbacks.progress(self) + except Exception as e: + RNS.log("Error while executing response progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) else: resource.cancel() @@ -987,10 +1009,16 @@ class RequestReceipt(): self.packet_receipt.callbacks.delivery(self.packet_receipt) if self.callbacks.progress != None: - self.callbacks.progress(self) + try: + self.callbacks.progress(self) + except Exception as e: + RNS.log("Error while executing response progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) if self.callbacks.response != None: - self.callbacks.response(self) + try: + self.callbacks.response(self) + except Exception as e: + RNS.log("Error while executing response received callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) def get_request_id(self): """ diff --git a/RNS/Packet.py b/RNS/Packet.py index 4f28166..5aca955 100755 --- a/RNS/Packet.py +++ b/RNS/Packet.py @@ -407,7 +407,11 @@ class PacketReceipt: self.proof_packet = proof_packet if self.callbacks.delivery != None: - self.callbacks.delivery(self) + try: + self.callbacks.delivery(self) + except Exception as e: + RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR) + return True else: return False @@ -425,9 +429,13 @@ class PacketReceipt: self.proved = True self.concluded_at = time.time() self.proof_packet = proof_packet - + if self.callbacks.delivery != None: - self.callbacks.delivery(self) + try: + self.callbacks.delivery(self) + except Exception as e: + RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR) + return True else: return False diff --git a/RNS/Resource.py b/RNS/Resource.py index 41fcb07..832252e 100644 --- a/RNS/Resource.py +++ b/RNS/Resource.py @@ -123,7 +123,10 @@ class Resource: RNS.log("Accepting resource advertisement for "+RNS.prettyhexrep(resource.hash), RNS.LOG_DEBUG) if resource.link.callbacks.resource_started != None: - resource.link.callbacks.resource_started(resource) + try: + resource.link.callbacks.resource_started(resource) + except Exception as e: + RNS.log("Error while executing resource started callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) resource.hashmap_update(0, resource.hashmap_raw) @@ -506,7 +509,10 @@ class Resource: if self.segment_index == self.total_segments: if self.callback != None: self.data = open(self.storagepath, "rb") - self.callback(self) + try: + self.callback(self) + except Exception as e: + RNS.log("Error while executing resource assembled callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) try: self.data.close() @@ -540,7 +546,10 @@ class Resource: # If all segments were processed, we'll # signal that the resource sending concluded if self.callback != None: - self.callback(self) + try: + self.callback(self) + except Exception as e: + RNS.log("Error while executing resource concluded callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) else: # Otherwise we'll recursively create the # next segment of the resource @@ -596,7 +605,10 @@ class Resource: cp += 1 if self.__progress_callback != None: - self.__progress_callback(self) + try: + self.__progress_callback(self) + except Exception as e: + RNS.log("Error while executing progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) # TODO: Remove debug info # RNS.log("outstanding_parts "+str(self.outstanding_parts)) @@ -754,7 +766,10 @@ class Resource: self.status = Resource.AWAITING_PROOF if self.__progress_callback != None: - self.__progress_callback(self) + try: + self.__progress_callback(self) + except Exception as e: + RNS.log("Error while executing progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) def cancel(self): """ @@ -774,8 +789,11 @@ class Resource: self.link.cancel_incoming_resource(self) if self.callback != None: - self.link.resource_concluded(self) - self.callback(self) + try: + self.link.resource_concluded(self) + self.callback(self) + except Exception as e: + RNS.log("Error while executing callbacks on resource cancel from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) def set_callback(self, callback): self.callback = callback diff --git a/RNS/Transport.py b/RNS/Transport.py index 4486910..fd366d9 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -998,8 +998,11 @@ class Transport: elif destination.proof_strategy == RNS.Destination.PROVE_APP: if destination.callbacks.proof_requested: - if destination.callbacks.proof_requested(packet): - packet.prove() + try: + if destination.callbacks.proof_requested(packet): + packet.prove() + except Exception as e: + RNS.log("Error while executing proof request callback. The contained exception was: "+str(e), RNS.LOG_ERROR) # Handling for proofs and link-request proofs elif packet.packet_type == RNS.Packet.PROOF: @@ -1390,12 +1393,15 @@ class Transport: @staticmethod def path_request_handler(data, packet): - if len(data) >= RNS.Identity.TRUNCATED_HASHLENGTH//8: - Transport.path_request( - data[:RNS.Identity.TRUNCATED_HASHLENGTH//8], - Transport.from_local_client(packet), - packet.receiving_interface - ) + try: + if len(data) >= RNS.Identity.TRUNCATED_HASHLENGTH//8: + Transport.path_request( + data[:RNS.Identity.TRUNCATED_HASHLENGTH//8], + Transport.from_local_client(packet), + packet.receiving_interface + ) + except Exception as e: + RNS.log("Error while handling path request. The contained exception was: "+str(e), RNS.LOG_ERROR) @staticmethod def path_request(destination_hash, is_from_local_client, attached_interface):