From 1bd4f5edad49832ffb4dd3cd65764d972943f24d Mon Sep 17 00:00:00 2001 From: Thoxy67 <1775037+Thoxy67@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:42:07 +0100 Subject: [PATCH] Runtime: don't drop a read request that arrives during export loop() reset read_retries_left[i] to 0 after notifying the exporters. Exporters are synchronous and can be slow (webhook), and start_reading_tag() runs on the controller thread, so a request for the same slot made during the export was overwritten and never processed. Reset the counter before notifying, in all three branches, and add a regression test. --- src/runtime.py | 8 +++--- test/test_runtime.py | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 test/test_runtime.py diff --git a/src/runtime.py b/src/runtime.py index 48f1fc8..0df7d22 100644 --- a/src/runtime.py +++ b/src/runtime.py @@ -78,19 +78,21 @@ def loop(self): if filament: logging.info(f"Successfully read tag with UID {scan_result.uid.hex().upper()} on reader {reader.name}") - self._notify_exporters(scan_result, filament, reader, ExporterEvent.TAG_READ) + # Clear the request before notifying: exporters can be slow (webhook), and a + # start_reading_tag() for this slot arriving meanwhile must not be wiped out self.read_retries_left[i] = 0 + self._notify_exporters(scan_result, filament, reader, ExporterEvent.TAG_READ) elif scan_result: logging.info(f"Detected tag with UID {scan_result.uid.hex().upper()} on reader {reader.name} but failed to read data") - self._notify_exporters(scan_result, None, reader, ExporterEvent.TAG_PARSE_ERROR) self.read_retries_left[i] = 0 + self._notify_exporters(scan_result, None, reader, ExporterEvent.TAG_PARSE_ERROR) elif self.config.auto_read_mode: # Auto-mode # TODO: if tag was previously detected, it should at least once notify exporters about tag not present logging.info(f"No tag detected on reader {reader.name}, but auto read mode is enabled, will retry") else: # Fatal non-retrable error logging.info(f"No tag detected on reader {reader.name}") - self._notify_exporters(None, None, reader, ExporterEvent.TAG_NOT_PRESENT) self.read_retries_left[i] = 0 + self._notify_exporters(None, None, reader, ExporterEvent.TAG_NOT_PRESENT) time.sleep(self.config.read_interval_seconds) diff --git a/test/test_runtime.py b/test/test_runtime.py new file mode 100644 index 0000000..a26db71 --- /dev/null +++ b/test/test_runtime.py @@ -0,0 +1,62 @@ +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest + +SRC_ROOT = Path(__file__).resolve().parents[1] / "src" +if str(SRC_ROOT) not in sys.path: + sys.path.insert(0, str(SRC_ROOT)) + +import runtime as runtime_module +from exporters.exporter import ExporterEvent +from runtime import Runtime + + +class _StopLoop(Exception): + pass + + +class _RequestingExporter: + """Asks for a new read of slot 0 while it is being notified, like a + controller thread would during a slow webhook.""" + + def __init__(self, runtime): + self.runtime = runtime + self.events = [] + + def has_event(self, event): + return True + + def export_data(self, scan, filament, reader): + self.events.append(scan) + if len(self.events) == 1: + self.runtime.start_reading_tag(0) + + +@pytest.mark.parametrize("result", [ + ("scan", "filament"), # TAG_READ + ("scan", None), # TAG_PARSE_ERROR + (None, None), # TAG_NOT_PRESENT +]) +def test_request_during_export_is_kept(monkeypatch, result): + rt = Runtime.__new__(Runtime) + rt.config = SimpleNamespace(auto_read_mode=False, read_retries=3, read_interval_seconds=0) + rt.rfid_readers = [SimpleNamespace(name="slot0")] + exporter = _RequestingExporter(rt) + rt.exporters = [exporter] + rt.read_retries_left = [3] + + scan = SimpleNamespace(uid=b"\x01\x02\x03\x04") if result[0] else None + rt.process_reader_single = lambda reader: (scan, result[1], False) + + def stop(_seconds): + raise _StopLoop() + monkeypatch.setattr(runtime_module.time, "sleep", stop) + + with pytest.raises(_StopLoop): + rt.loop() + + assert len(exporter.events) == 1 + # The request made during the export is still pending for the next pass + assert rt.read_retries_left[0] == 3