Added locking and timeouts to table saving routines

This commit is contained in:
Mark Qvist 2022-09-06 18:05:02 +02:00
parent af538e0489
commit 4042dd6ef7

View File

@ -2023,6 +2023,7 @@ class Transport:
Transport.announce_handlers = [] Transport.announce_handlers = []
Transport.tunnels = {} Transport.tunnels = {}
@staticmethod @staticmethod
def shared_connection_reappeared(): def shared_connection_reappeared():
if Transport.owner.is_connected_to_shared_instance: if Transport.owner.is_connected_to_shared_instance:
@ -2030,6 +2031,7 @@ class Transport:
if registered_destination.type == RNS.Destination.SINGLE: if registered_destination.type == RNS.Destination.SINGLE:
registered_destination.announce(path_response=True) registered_destination.announce(path_response=True)
@staticmethod @staticmethod
def drop_announce_queues(): def drop_announce_queues():
for interface in Transport.interfaces: for interface in Transport.interfaces:
@ -2044,6 +2046,7 @@ class Transport:
interface.announce_queue = [] interface.announce_queue = []
RNS.log("Dropped "+na_str+" on "+str(interface), RNS.LOG_VERBOSE) RNS.log("Dropped "+na_str+" on "+str(interface), RNS.LOG_VERBOSE)
@staticmethod @staticmethod
def announce_emitted(packet): def announce_emitted(packet):
random_blob = packet.data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8] random_blob = packet.data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8]
@ -2051,9 +2054,21 @@ class Transport:
return announce_emitted return announce_emitted
@staticmethod @staticmethod
def save_packet_hashlist(): def save_packet_hashlist():
if hasattr(Transport, "saving_packet_hashlist"):
wait_interval = 0.2
wait_timeout = 5
wait_start = time.time()
while Transport.saving_packet_hashlist:
time.sleep(wait_interval)
if time.time() > wait_start+wait_timeout:
RNS.log("Could not save packet hashlist to storage, waiting for previous save operation timed out.", RNS.LOG_ERROR)
return False
try: try:
Transport.saving_packet_hashlist = True
save_start = time.time() save_start = time.time()
if not RNS.Reticulum.transport_enabled(): if not RNS.Reticulum.transport_enabled():
@ -2076,13 +2091,27 @@ class Transport:
except Exception as e: except Exception as e:
RNS.log("Could not save packet hashlist to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Could not save packet hashlist to storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
Transport.saving_packet_hashlist = False
@staticmethod @staticmethod
def save_path_table(): def save_path_table():
if not Transport.owner.is_connected_to_shared_instance: if not Transport.owner.is_connected_to_shared_instance:
if hasattr(Transport, "saving_path_table"):
wait_interval = 0.2
wait_timeout = 5
wait_start = time.time()
while Transport.saving_path_table:
time.sleep(wait_interval)
if time.time() > wait_start+wait_timeout:
RNS.log("Could not save path table to storage, waiting for previous save operation timed out.", RNS.LOG_ERROR)
return False
try:
Transport.saving_path_table = True
save_start = time.time() save_start = time.time()
RNS.log("Saving path table to storage...", RNS.LOG_VERBOSE) RNS.log("Saving path table to storage...", RNS.LOG_VERBOSE)
try:
serialised_destinations = [] serialised_destinations = []
for destination_hash in Transport.destination_table: for destination_hash in Transport.destination_table:
# Get the destination entry from the destination table # Get the destination entry from the destination table
@ -2132,12 +2161,27 @@ class Transport:
except Exception as e: except Exception as e:
RNS.log("Could not save path table to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Could not save path table to storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
Transport.saving_path_table = False
@staticmethod @staticmethod
def save_tunnel_table(): def save_tunnel_table():
if not Transport.owner.is_connected_to_shared_instance: if not Transport.owner.is_connected_to_shared_instance:
if hasattr(Transport, "saving_tunnel_table"):
wait_interval = 0.2
wait_timeout = 5
wait_start = time.time()
while Transport.saving_tunnel_table:
time.sleep(wait_interval)
if time.time() > wait_start+wait_timeout:
RNS.log("Could not save tunnel table to storage, waiting for previous save operation timed out.", RNS.LOG_ERROR)
return False
try:
Transport.saving_tunnel_table = True
save_start = time.time() save_start = time.time()
RNS.log("Saving tunnel table to storage...", RNS.LOG_VERBOSE) RNS.log("Saving tunnel table to storage...", RNS.LOG_VERBOSE)
try:
serialised_tunnels = [] serialised_tunnels = []
for tunnel_id in Transport.tunnels: for tunnel_id in Transport.tunnels:
te = Transport.tunnels[tunnel_id] te = Transport.tunnels[tunnel_id]
@ -2194,6 +2238,9 @@ class Transport:
except Exception as e: except Exception as e:
RNS.log("Could not save tunnel table to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Could not save tunnel table to storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
Transport.saving_tunnel_table = False
@staticmethod @staticmethod
def exit_handler(): def exit_handler():
Transport.save_packet_hashlist() Transport.save_packet_hashlist()