From 3fd606946db8df368da0cd0e9f6e9a0c18d36525 Mon Sep 17 00:00:00 2001 From: Hemna Date: Thu, 31 Oct 2024 17:39:04 -0400 Subject: [PATCH] Fix a small issue with packet sending failures When a packet _send_direct() failed to send due to a network timeout or client issue, we don't want to count that as a send attempt for the packet. This patch catches that and allows for another retry. --- aprsd/cmds/admin.py | 5 ++++- aprsd/threads/tx.py | 30 ++++++++++++++++++++++++++---- aprsd/wsgi.py | 1 - 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/aprsd/cmds/admin.py b/aprsd/cmds/admin.py index cc77bed..6f3a8d9 100644 --- a/aprsd/cmds/admin.py +++ b/aprsd/cmds/admin.py @@ -14,7 +14,10 @@ from aprsd.main import cli os.environ["APRSD_ADMIN_COMMAND"] = "1" -from aprsd import wsgi as aprsd_wsgi +# this import has to happen AFTER we set the +# above environment variable, so that the code +# inside the wsgi.py has the value +from aprsd import wsgi as aprsd_wsgi # noqa CONF = cfg.CONF diff --git a/aprsd/threads/tx.py b/aprsd/threads/tx.py index 25e61b1..b7ed45b 100644 --- a/aprsd/threads/tx.py +++ b/aprsd/threads/tx.py @@ -89,6 +89,9 @@ def _send_direct(packet, aprs_client=None): except Exception as e: LOG.error(f"Failed to send packet: {packet}") LOG.error(e) + return False + else: + return True class SendPacketThread(aprsd_threads.APRSDThread): @@ -150,8 +153,17 @@ class SendPacketThread(aprsd_threads.APRSDThread): # no attempt time, so lets send it, and start # tracking the time. packet.last_send_time = int(round(time.time())) - _send_direct(packet) - packet.send_count += 1 + sent = False + try: + sent = _send_direct(packet) + except Exception: + LOG.error(f"Failed to send packet: {packet}") + else: + # If an exception happens while sending + # we don't want this attempt to count + # against the packet + if sent: + packet.send_count += 1 time.sleep(1) # Make sure we get called again. @@ -199,8 +211,18 @@ class SendAckThread(aprsd_threads.APRSDThread): send_now = True if send_now: - _send_direct(self.packet) - self.packet.send_count += 1 + sent = False + try: + sent = _send_direct(self.packet) + except Exception: + LOG.error(f"Failed to send packet: {self.packet}") + else: + # If an exception happens while sending + # we don't want this attempt to count + # against the packet + if sent: + self.packet.send_count += 1 + self.packet.last_send_time = int(round(time.time())) time.sleep(1) diff --git a/aprsd/wsgi.py b/aprsd/wsgi.py index 0d7d2d6..47da201 100644 --- a/aprsd/wsgi.py +++ b/aprsd/wsgi.py @@ -269,7 +269,6 @@ def init_app(config_file=None, log_level=None): return log_level -print(f"__name__ = {__name__}") if __name__ == "__main__": async_mode = "threading"