Skip to content

Commit 448ea8c

Browse files
committed
Added try statements for various callbacks
1 parent f7e8fc4 commit 448ea8c

File tree

5 files changed

+93
-29
lines changed

5 files changed

+93
-29
lines changed

RNS/Destination.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ def receive(self, packet):
263263
if plaintext != None:
264264
if packet.packet_type == RNS.Packet.DATA:
265265
if self.callbacks.packet != None:
266-
self.callbacks.packet(plaintext, packet)
266+
try:
267+
self.callbacks.packet(plaintext, packet)
268+
except Exception as e:
269+
RNS.log("Error while executing receive callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
270+
267271

268272
def incoming_link_request(self, data, packet):
269273
link = RNS.Link.validate_request(self, data, packet)

RNS/Link.py

+38-10
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,11 @@ def link_closed(self):
425425
self.destination.links.remove(self)
426426

427427
if self.callbacks.link_closed != None:
428-
self.callbacks.link_closed(self)
428+
try:
429+
self.callbacks.link_closed(self)
430+
except Exception as e:
431+
RNS.log("Error while executing link closed callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
432+
429433

430434
def start_watchdog(self):
431435
thread = threading.Thread(target=self.__watchdog_job)
@@ -598,7 +602,10 @@ def receive(self, packet):
598602

599603
elif self.destination.proof_strategy == RNS.Destination.PROVE_APP:
600604
if self.destination.callbacks.proof_requested:
601-
self.destination.callbacks.proof_requested(packet)
605+
try:
606+
self.destination.callbacks.proof_requested(packet)
607+
except Exception as e:
608+
RNS.log("Error while executing proof request callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
602609

603610
elif packet.context == RNS.Packet.LINKIDENTIFY:
604611
plaintext = self.decrypt(packet.data)
@@ -613,7 +620,10 @@ def receive(self, packet):
613620
if identity.validate(signature, signed_data):
614621
self.__remote_identity = identity
615622
if self.callbacks.remote_identified != None:
616-
self.callbacks.remote_identified(self.__remote_identity)
623+
try:
624+
self.callbacks.remote_identified(self.__remote_identity)
625+
except Exception as e:
626+
RNS.log("Error while executing remote identified callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
617627

618628
elif packet.context == RNS.Packet.REQUEST:
619629
try:
@@ -659,8 +669,11 @@ def receive(self, packet):
659669
pass
660670
elif self.resource_strategy == Link.ACCEPT_APP:
661671
if self.callbacks.resource != None:
662-
if self.callbacks.resource(resource):
663-
RNS.Resource.accept(packet, self.callbacks.resource_concluded)
672+
try:
673+
if self.callbacks.resource(resource):
674+
RNS.Resource.accept(packet, self.callbacks.resource_concluded)
675+
except Exception as e:
676+
RNS.log("Error while executing resource accept callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
664677
elif self.resource_strategy == Link.ACCEPT_ALL:
665678
RNS.Resource.accept(packet, self.callbacks.resource_concluded)
666679

@@ -933,7 +946,10 @@ def request_resource_concluded(self, resource):
933946
self.link.pending_requests.remove(self)
934947

935948
if self.callbacks.failed != None:
936-
self.callbacks.failed(self)
949+
try:
950+
self.callbacks.failed(self)
951+
except Exception as e:
952+
RNS.log("Error while executing request failed callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
937953

938954

939955
def __response_timeout_job(self):
@@ -951,7 +967,10 @@ def request_timed_out(self, packet_receipt):
951967
self.link.pending_requests.remove(self)
952968

953969
if self.callbacks.failed != None:
954-
self.callbacks.failed(self)
970+
try:
971+
self.callbacks.failed(self)
972+
except Exception as e:
973+
RNS.log("Error while executing request timed out callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
955974

956975

957976
def response_resource_progress(self, resource):
@@ -967,7 +986,10 @@ def response_resource_progress(self, resource):
967986
self.progress = resource.get_progress()
968987

969988
if self.callbacks.progress != None:
970-
self.callbacks.progress(self)
989+
try:
990+
self.callbacks.progress(self)
991+
except Exception as e:
992+
RNS.log("Error while executing response progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
971993
else:
972994
resource.cancel()
973995

@@ -987,10 +1009,16 @@ def response_received(self, response):
9871009
self.packet_receipt.callbacks.delivery(self.packet_receipt)
9881010

9891011
if self.callbacks.progress != None:
990-
self.callbacks.progress(self)
1012+
try:
1013+
self.callbacks.progress(self)
1014+
except Exception as e:
1015+
RNS.log("Error while executing response progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
9911016

9921017
if self.callbacks.response != None:
993-
self.callbacks.response(self)
1018+
try:
1019+
self.callbacks.response(self)
1020+
except Exception as e:
1021+
RNS.log("Error while executing response received callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
9941022

9951023
def get_request_id(self):
9961024
"""

RNS/Packet.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ def validate_proof(self, proof, proof_packet=None):
407407
self.proof_packet = proof_packet
408408

409409
if self.callbacks.delivery != None:
410-
self.callbacks.delivery(self)
410+
try:
411+
self.callbacks.delivery(self)
412+
except Exception as e:
413+
RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
414+
411415
return True
412416
else:
413417
return False
@@ -425,9 +429,13 @@ def validate_proof(self, proof, proof_packet=None):
425429
self.proved = True
426430
self.concluded_at = time.time()
427431
self.proof_packet = proof_packet
428-
432+
429433
if self.callbacks.delivery != None:
430-
self.callbacks.delivery(self)
434+
try:
435+
self.callbacks.delivery(self)
436+
except Exception as e:
437+
RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
438+
431439
return True
432440
else:
433441
return False

RNS/Resource.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ def accept(advertisement_packet, callback=None, progress_callback = None, reques
123123

124124
RNS.log("Accepting resource advertisement for "+RNS.prettyhexrep(resource.hash), RNS.LOG_DEBUG)
125125
if resource.link.callbacks.resource_started != None:
126-
resource.link.callbacks.resource_started(resource)
126+
try:
127+
resource.link.callbacks.resource_started(resource)
128+
except Exception as e:
129+
RNS.log("Error while executing resource started callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
127130

128131
resource.hashmap_update(0, resource.hashmap_raw)
129132

@@ -506,7 +509,10 @@ def assemble(self):
506509
if self.segment_index == self.total_segments:
507510
if self.callback != None:
508511
self.data = open(self.storagepath, "rb")
509-
self.callback(self)
512+
try:
513+
self.callback(self)
514+
except Exception as e:
515+
RNS.log("Error while executing resource assembled callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
510516

511517
try:
512518
self.data.close()
@@ -540,7 +546,10 @@ def validate_proof(self, proof_data):
540546
# If all segments were processed, we'll
541547
# signal that the resource sending concluded
542548
if self.callback != None:
543-
self.callback(self)
549+
try:
550+
self.callback(self)
551+
except Exception as e:
552+
RNS.log("Error while executing resource concluded callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
544553
else:
545554
# Otherwise we'll recursively create the
546555
# next segment of the resource
@@ -596,7 +605,10 @@ def receive_part(self, packet):
596605
cp += 1
597606

598607
if self.__progress_callback != None:
599-
self.__progress_callback(self)
608+
try:
609+
self.__progress_callback(self)
610+
except Exception as e:
611+
RNS.log("Error while executing progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
600612

601613
# TODO: Remove debug info
602614
# RNS.log("outstanding_parts "+str(self.outstanding_parts))
@@ -754,7 +766,10 @@ def request(self, request_data):
754766
self.status = Resource.AWAITING_PROOF
755767

756768
if self.__progress_callback != None:
757-
self.__progress_callback(self)
769+
try:
770+
self.__progress_callback(self)
771+
except Exception as e:
772+
RNS.log("Error while executing progress callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
758773

759774
def cancel(self):
760775
"""
@@ -774,8 +789,11 @@ def cancel(self):
774789
self.link.cancel_incoming_resource(self)
775790

776791
if self.callback != None:
777-
self.link.resource_concluded(self)
778-
self.callback(self)
792+
try:
793+
self.link.resource_concluded(self)
794+
self.callback(self)
795+
except Exception as e:
796+
RNS.log("Error while executing callbacks on resource cancel from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
779797

780798
def set_callback(self, callback):
781799
self.callback = callback

RNS/Transport.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,11 @@ def inbound(raw, interface=None):
998998

999999
elif destination.proof_strategy == RNS.Destination.PROVE_APP:
10001000
if destination.callbacks.proof_requested:
1001-
if destination.callbacks.proof_requested(packet):
1002-
packet.prove()
1001+
try:
1002+
if destination.callbacks.proof_requested(packet):
1003+
packet.prove()
1004+
except Exception as e:
1005+
RNS.log("Error while executing proof request callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
10031006

10041007
# Handling for proofs and link-request proofs
10051008
elif packet.packet_type == RNS.Packet.PROOF:
@@ -1390,12 +1393,15 @@ def request_path_on_interface(destination_hash, interface):
13901393

13911394
@staticmethod
13921395
def path_request_handler(data, packet):
1393-
if len(data) >= RNS.Identity.TRUNCATED_HASHLENGTH//8:
1394-
Transport.path_request(
1395-
data[:RNS.Identity.TRUNCATED_HASHLENGTH//8],
1396-
Transport.from_local_client(packet),
1397-
packet.receiving_interface
1398-
)
1396+
try:
1397+
if len(data) >= RNS.Identity.TRUNCATED_HASHLENGTH//8:
1398+
Transport.path_request(
1399+
data[:RNS.Identity.TRUNCATED_HASHLENGTH//8],
1400+
Transport.from_local_client(packet),
1401+
packet.receiving_interface
1402+
)
1403+
except Exception as e:
1404+
RNS.log("Error while handling path request. The contained exception was: "+str(e), RNS.LOG_ERROR)
13991405

14001406
@staticmethod
14011407
def path_request(destination_hash, is_from_local_client, attached_interface):

0 commit comments

Comments
 (0)