From b90f9c9e8d795fc38e9ff046e0a62535ee3ec1e8 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Fri, 13 Aug 2021 15:15:31 +0200
Subject: [PATCH] L2SS-271: Initialise default parameters before starting UDP
 receiver thread to avoid a race condition in which the UDP packets are
 processed before the default parameter list is initialised.

---
 devices/devices/sdp/statistics_collector.py | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/devices/devices/sdp/statistics_collector.py b/devices/devices/sdp/statistics_collector.py
index f7d01d2cb..f3aac3c19 100644
--- a/devices/devices/sdp/statistics_collector.py
+++ b/devices/devices/sdp/statistics_collector.py
@@ -23,7 +23,13 @@ class StatisticsCollector(Thread):
         self.queue = queue
         self.last_packet = None
 
-        self.parameters = {
+        self.parameters = self._default_parameters()
+
+        super().__init__()
+        self.start()
+
+    def _default_parameters(self):
+        return {
             "nof_packets":           numpy.uint64(0),
 
             # Packet count for packets that could not be parsed
@@ -33,9 +39,6 @@ class StatisticsCollector(Thread):
             "last_invalid_packet":   numpy.zeros((9000,), dtype=numpy.uint8),
         }
 
-        super().__init__()
-        self.start()
-
     def run(self):
         logger.info("Starting statistics thread")
 
@@ -54,7 +57,7 @@ class StatisticsCollector(Thread):
             except Exception as e:
                 logger.exception("Could not parse statistics UDP packet")
 
-                self.parameters["last_invalid_packet"] = numpy.frombuffer(packet, dtype=numpy.uint8)
+                self.parameters["last_invalid_packet"] = numpy.frombuffer(self.last_packet, dtype=numpy.uint8)
                 self.parameters["nof_invalid_packets"] += numpy.uint64(1)
 
         logger.info("Stopped statistics thread")
@@ -92,10 +95,10 @@ class SSTCollector(StatisticsCollector):
     # Maximum number of subbands we support (used to determine array sizes)
     MAX_SUBBANDS = 512
 
-    def __init__(self, queue):
-        super().__init__(queue)
+    def _default_parameters(self):
+        defaults = super()._default_parameters()
 
-        self.parameters.extend({
+        defaults.update({
             # Number of packets received so far that we could parse correctly and do not have a payload error
             "nof_valid_payloads":    numpy.zeros((self.MAX_INPUTS,), dtype=numpy.uint64),
 
@@ -108,6 +111,8 @@ class SSTCollector(StatisticsCollector):
             "integration_intervals": numpy.zeros((self.MAX_INPUTS,), dtype=numpy.float32),
         })
 
+        return defaults
+
     def process_packet(self, packet):
         fields = SSTPacket(packet)
 
-- 
GitLab