mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-18 19:43:00 +03:00
Update the default timeout for detection subscriber to avoid blocking indefinitely
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from frigate.const import FAST_QUEUE_TIMEOUT
|
||||||
|
|
||||||
from .zmq_proxy import Publisher, Subscriber
|
from .zmq_proxy import Publisher, Subscriber
|
||||||
|
|
||||||
|
|
||||||
@@ -33,7 +35,7 @@ class DetectionSubscriber(Subscriber):
|
|||||||
super().__init__(topic)
|
super().__init__(topic)
|
||||||
|
|
||||||
def check_for_update(
|
def check_for_update(
|
||||||
self, timeout: float | None = None
|
self, timeout: float | None = FAST_QUEUE_TIMEOUT
|
||||||
) -> tuple[str, Any] | tuple[None, None] | None:
|
) -> tuple[str, Any] | tuple[None, None] | None:
|
||||||
return super().check_for_update(timeout)
|
return super().check_for_update(timeout)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""Tests for the detection subscriber's default receive timeout."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import zmq
|
||||||
|
|
||||||
|
from frigate.comms import zmq_proxy
|
||||||
|
from frigate.comms.detections_updater import DetectionSubscriber, DetectionTypeEnum
|
||||||
|
|
||||||
|
|
||||||
|
class TestDetectionSubscriberTimeout(unittest.TestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(tmp.cleanup)
|
||||||
|
self.address = f"ipc://{os.path.join(tmp.name, 'proxy_sub')}"
|
||||||
|
|
||||||
|
def _subscriber(self) -> DetectionSubscriber:
|
||||||
|
with patch.object(zmq_proxy, "SOCKET_SUB", self.address):
|
||||||
|
return DetectionSubscriber(DetectionTypeEnum.video.value)
|
||||||
|
|
||||||
|
def test_default_check_returns_when_nothing_is_published(self) -> None:
|
||||||
|
subscriber = self._subscriber()
|
||||||
|
results: list = []
|
||||||
|
thread = threading.Thread(
|
||||||
|
target=lambda: results.append(subscriber.check_for_update()),
|
||||||
|
daemon=True,
|
||||||
|
)
|
||||||
|
thread.start()
|
||||||
|
thread.join(timeout=5)
|
||||||
|
|
||||||
|
# a blocked check still owns the socket, so only stop it after returning
|
||||||
|
self.assertFalse(
|
||||||
|
thread.is_alive(), "check_for_update blocked with nothing published"
|
||||||
|
)
|
||||||
|
subscriber.stop()
|
||||||
|
self.assertEqual(results, [(None, None)])
|
||||||
|
|
||||||
|
def test_default_check_returns_queued_detection(self) -> None:
|
||||||
|
context = zmq.Context()
|
||||||
|
publisher = context.socket(zmq.PUB)
|
||||||
|
publisher.bind(self.address)
|
||||||
|
subscriber = self._subscriber()
|
||||||
|
self.addCleanup(context.destroy, linger=0)
|
||||||
|
self.addCleanup(subscriber.stop)
|
||||||
|
|
||||||
|
# wait for the subscription to reach the publisher before relying on it
|
||||||
|
deadline = time.monotonic() + 5
|
||||||
|
while True:
|
||||||
|
publisher.send_string(f"detection/video {json.dumps('ready')}")
|
||||||
|
|
||||||
|
if subscriber.check_for_update(timeout=0.05) == ("video", "ready"):
|
||||||
|
break
|
||||||
|
|
||||||
|
self.assertLess(time.monotonic(), deadline, "subscription never joined")
|
||||||
|
|
||||||
|
# drop any late readiness messages so only the real one is queued
|
||||||
|
while subscriber.check_for_update(timeout=0.05) != (None, None):
|
||||||
|
pass
|
||||||
|
|
||||||
|
publisher.send_string(f"detection/video {json.dumps(['front', 1.0])}")
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
self.assertEqual(subscriber.check_for_update(), ("video", ["front", 1.0]))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user