Added try statements for various callbacks

This commit is contained in:
Mark Qvist 2021-10-15 14:36:50 +02:00
parent f7e8fc4719
commit 448ea8ceb5
5 changed files with 93 additions and 29 deletions

View File

@ -263,7 +263,11 @@ class Destination:
if plaintext != None: if plaintext != None:
if packet.packet_type == RNS.Packet.DATA: if packet.packet_type == RNS.Packet.DATA:
if self.callbacks.packet != None: 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): def incoming_link_request(self, data, packet):
link = RNS.Link.validate_request(self, data, packet) link = RNS.Link.validate_request(self, data, packet)

View File

@ -425,7 +425,11 @@ class Link:
self.destination.links.remove(self) self.destination.links.remove(self)
if self.callbacks.link_closed != None: 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): def start_watchdog(self):
thread = threading.Thread(target=self.__watchdog_job) thread = threading.Thread(target=self.__watchdog_job)
@ -598,7 +602,10 @@ class Link:
elif self.destination.proof_strategy == RNS.Destination.PROVE_APP: elif self.destination.proof_strategy == RNS.Destination.PROVE_APP:
if self.destination.callbacks.proof_requested: 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: elif packet.context == RNS.Packet.LINKIDENTIFY:
plaintext = self.decrypt(packet.data) plaintext = self.decrypt(packet.data)
@ -613,7 +620,10 @@ class Link:
if identity.validate(signature, signed_data): if identity.validate(signature, signed_data):
self.__remote_identity = identity self.__remote_identity = identity
if self.callbacks.remote_identified != None: 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: elif packet.context == RNS.Packet.REQUEST:
try: try:
@ -659,8 +669,11 @@ class Link:
pass pass
elif self.resource_strategy == Link.ACCEPT_APP: elif self.resource_strategy == Link.ACCEPT_APP:
if self.callbacks.resource != None: if self.callbacks.resource != None:
if self.callbacks.resource(resource): try:
RNS.Resource.accept(packet, self.callbacks.resource_concluded) 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: elif self.resource_strategy == Link.ACCEPT_ALL:
RNS.Resource.accept(packet, self.callbacks.resource_concluded) RNS.Resource.accept(packet, self.callbacks.resource_concluded)
@ -933,7 +946,10 @@ class RequestReceipt():
self.link.pending_requests.remove(self) self.link.pending_requests.remove(self)
if self.callbacks.failed != None: 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): def __response_timeout_job(self):
@ -951,7 +967,10 @@ class RequestReceipt():
self.link.pending_requests.remove(self) self.link.pending_requests.remove(self)
if self.callbacks.failed != None: 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): def response_resource_progress(self, resource):
@ -967,7 +986,10 @@ class RequestReceipt():
self.progress = resource.get_progress() self.progress = resource.get_progress()
if self.callbacks.progress != None: 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: else:
resource.cancel() resource.cancel()
@ -987,10 +1009,16 @@ class RequestReceipt():
self.packet_receipt.callbacks.delivery(self.packet_receipt) self.packet_receipt.callbacks.delivery(self.packet_receipt)
if self.callbacks.progress != None: 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: 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): def get_request_id(self):
""" """

View File

@ -407,7 +407,11 @@ class PacketReceipt:
self.proof_packet = proof_packet self.proof_packet = proof_packet
if self.callbacks.delivery != None: 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 return True
else: else:
return False return False
@ -425,9 +429,13 @@ class PacketReceipt:
self.proved = True self.proved = True
self.concluded_at = time.time() self.concluded_at = time.time()
self.proof_packet = proof_packet self.proof_packet = proof_packet
if self.callbacks.delivery != None: 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 return True
else: else:
return False return False

View File

@ -123,7 +123,10 @@ class Resource:
RNS.log("Accepting resource advertisement for "+RNS.prettyhexrep(resource.hash), RNS.LOG_DEBUG) RNS.log("Accepting resource advertisement for "+RNS.prettyhexrep(resource.hash), RNS.LOG_DEBUG)
if resource.link.callbacks.resource_started != None: 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) resource.hashmap_update(0, resource.hashmap_raw)
@ -506,7 +509,10 @@ class Resource:
if self.segment_index == self.total_segments: if self.segment_index == self.total_segments:
if self.callback != None: if self.callback != None:
self.data = open(self.storagepath, "rb") 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: try:
self.data.close() self.data.close()
@ -540,7 +546,10 @@ class Resource:
# If all segments were processed, we'll # If all segments were processed, we'll
# signal that the resource sending concluded # signal that the resource sending concluded
if self.callback != None: 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: else:
# Otherwise we'll recursively create the # Otherwise we'll recursively create the
# next segment of the resource # next segment of the resource
@ -596,7 +605,10 @@ class Resource:
cp += 1 cp += 1
if self.__progress_callback != None: 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 # TODO: Remove debug info
# RNS.log("outstanding_parts "+str(self.outstanding_parts)) # RNS.log("outstanding_parts "+str(self.outstanding_parts))
@ -754,7 +766,10 @@ class Resource:
self.status = Resource.AWAITING_PROOF self.status = Resource.AWAITING_PROOF
if self.__progress_callback != None: 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): def cancel(self):
""" """
@ -774,8 +789,11 @@ class Resource:
self.link.cancel_incoming_resource(self) self.link.cancel_incoming_resource(self)
if self.callback != None: if self.callback != None:
self.link.resource_concluded(self) try:
self.callback(self) 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): def set_callback(self, callback):
self.callback = callback self.callback = callback

View File

@ -998,8 +998,11 @@ class Transport:
elif destination.proof_strategy == RNS.Destination.PROVE_APP: elif destination.proof_strategy == RNS.Destination.PROVE_APP:
if destination.callbacks.proof_requested: if destination.callbacks.proof_requested:
if destination.callbacks.proof_requested(packet): try:
packet.prove() 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 # Handling for proofs and link-request proofs
elif packet.packet_type == RNS.Packet.PROOF: elif packet.packet_type == RNS.Packet.PROOF:
@ -1390,12 +1393,15 @@ class Transport:
@staticmethod @staticmethod
def path_request_handler(data, packet): def path_request_handler(data, packet):
if len(data) >= RNS.Identity.TRUNCATED_HASHLENGTH//8: try:
Transport.path_request( if len(data) >= RNS.Identity.TRUNCATED_HASHLENGTH//8:
data[:RNS.Identity.TRUNCATED_HASHLENGTH//8], Transport.path_request(
Transport.from_local_client(packet), data[:RNS.Identity.TRUNCATED_HASHLENGTH//8],
packet.receiving_interface 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 @staticmethod
def path_request(destination_hash, is_from_local_client, attached_interface): def path_request(destination_hash, is_from_local_client, attached_interface):