Improved link teardown on SIGINT/SIGTERM

This commit is contained in:
Mark Qvist 2026-04-26 17:07:43 +02:00
parent 015692d51e
commit 695d4d8684
2 changed files with 13 additions and 12 deletions

View file

@ -186,13 +186,11 @@ class Reticulum:
# out cleanup operations. # out cleanup operations.
if not Reticulum.__exit_handler_ran: if not Reticulum.__exit_handler_ran:
Reticulum.__exit_handler_ran = True Reticulum.__exit_handler_ran = True
if not Reticulum.__interface_detach_ran: if not Reticulum.__interface_detach_ran: RNS.Transport.detach_interfaces()
RNS.Transport.detach_interfaces()
RNS.Transport.exit_handler() RNS.Transport.exit_handler()
RNS.Identity.exit_handler() RNS.Identity.exit_handler()
if RNS.Profiler.ran(): if RNS.Profiler.ran(): RNS.Profiler.results()
RNS.Profiler.results()
RNS.loglevel = -1 RNS.loglevel = -1

View file

@ -2998,15 +2998,18 @@ class Transport:
@staticmethod @staticmethod
def detach_interfaces(): def detach_interfaces():
with Transport.active_links_lock: closed_links = 0
for link in Transport.active_links: for link in Transport.active_links.copy():
try: link.teardown() try: link.teardown(); closed_links += 1
except Exception as e: RNS.log(f"Could not tear down active link before interface detach: {e}", RNS.LOG_WARNING) except Exception as e: RNS.log(f"Could not tear down active link before interface detach: {e}", RNS.LOG_WARNING)
with Transport.pending_links_lock: for link in Transport.pending_links.copy():
for link in Transport.pending_links: try: link.teardown(); closed_links += 1
try: link.teardown() except Exception as e: RNS.log(f"Could not tear down pending link before interface detach: {e}", RNS.LOG_WARNING)
except Exception as e: RNS.log(f"Could not tear down pending link before interface detach: {e}", RNS.LOG_WARNING)
# Provide a 150ms window to allow link teardown
# packets to leave local transport
if closed_links: time.sleep(0.15)
detachable_interfaces = [] detachable_interfaces = []