From d2f96225cbd075253eee6c0b46b286e8fd36ad08 Mon Sep 17 00:00:00 2001 From: Andrei Smaliak Date: Sun, 8 Nov 2026 08:22:31 +0100 Subject: [PATCH] Retry transient camera publisher failures --- docs/releases/console-3.1.41.md | 6 ++++ docs/releases/ip-stream-1.0.10.md | 9 ++++++ stage-releases.json | 4 +-- stage_ip_stream/ip-stream.py | 28 +++++++++++++++++- stage_ip_stream/test_ip_stream.py | 35 ++++++++++++++++++++++- stage_ip_stream/version.json | 2 +- stage_ui/console_core/backend/app.py | 14 ++++++++- stage_ui/console_core/frontend/console.js | 2 +- stage_ui/console_core/frontend/index.html | 6 ++-- stage_ui/version.json | 2 +- 10 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 docs/releases/console-3.1.41.md create mode 100644 docs/releases/ip-stream-1.0.10.md diff --git a/docs/releases/console-3.1.41.md b/docs/releases/console-3.1.41.md new file mode 100644 index 0000000..26ab107 --- /dev/null +++ b/docs/releases/console-3.1.41.md @@ -0,0 +1,6 @@ +# Console 3.1.41 — camera stream cleanup + +- Add the authenticated camera-stream stop operation used by AITS when CDN readiness cannot be confirmed. +- Retain synchronized runtime, frontend, asset and stage version metadata. + +Validation: the complete Console suite and camera streaming contract tests pass. diff --git a/docs/releases/ip-stream-1.0.10.md b/docs/releases/ip-stream-1.0.10.md new file mode 100644 index 0000000..bc2b8d0 --- /dev/null +++ b/docs/releases/ip-stream-1.0.10.md @@ -0,0 +1,9 @@ +# IP Camera Streaming 1.0.10 + +Release date: 2026-11-08. + +- Retry a failed forwarding FFmpeg process up to two times while its requested TTL remains active. +- Preserve the last sanitized FFmpeg failure in local status and systemd logs for field diagnostics. +- Report retry count without exposing RTSP credentials or SRT publication tokens. + +Validation: reproduced the intermittent SRT `Input/output error` on `g-box-c453df5f`; unit coverage verifies bounded restart, expiry and secret redaction. diff --git a/stage-releases.json b/stage-releases.json index c8cf354..836c324 100644 --- a/stage-releases.json +++ b/stage-releases.json @@ -14,7 +14,7 @@ "version": "1.0.1" }, "stage_ui": { - "version": "3.1.40" + "version": "3.1.41" }, "stage_avahi": { "version": "1.0.0" @@ -31,7 +31,7 @@ }, "optional_stages": { "stage_ip_stream": { - "version": "1.0.9" + "version": "1.0.10" } } } diff --git a/stage_ip_stream/ip-stream.py b/stage_ip_stream/ip-stream.py index 677b175..1b6f1ef 100644 --- a/stage_ip_stream/ip-stream.py +++ b/stage_ip_stream/ip-stream.py @@ -24,6 +24,7 @@ SUPPORTED_OUTPUT_SCHEMES = {"rtsp", "rtmp", "srt", "udp"} MAX_STREAMS = 8 PREVIEW_TTL_SEC = 10 * 60 STARTUP_GUARD_SEC = 0.75 +MAX_FORWARD_RESTARTS = 2 FFMPEG_BINARY = "/usr/bin/ffmpeg" FFPROBE_BINARY = "/usr/bin/ffprobe" @@ -108,6 +109,7 @@ class StreamManager: self.operation_lock = threading.RLock() self.control_socket = None self.supervisor_stop = threading.Event() + self.last_failure = None def publish(self, payload): subprocess.run([ @@ -128,7 +130,17 @@ class StreamManager: code = process.poll() if code is None: continue - stopped.append({"stream_id": stream_id, "exit_code": code}) + error = self.read_error_log(record) + stopped.append({"stream_id": stream_id, "exit_code": code, "error": error}) + self.remember_failure(stream_id, code, error) + if (record.get("kind") == "forward" and record.get("expires_at", 0) > now + and record.get("restart_count", 0) < MAX_FORWARD_RESTARTS): + self.close_error_log(record) + record["restart_count"] = record.get("restart_count", 0) + 1 + record["error_log"] = tempfile.TemporaryFile(mode="w+b") + record["process"] = self.spawn(record["command"], stderr=record["error_log"]) + stopped[-1]["restarted"] = True + continue self.processes.pop(stream_id, None) self.close_error_log(record) self.write_status() @@ -151,11 +163,13 @@ class StreamManager: stream_id: { "kind": record.get("kind", "forward"), "expires_at": int(record.get("expires_at", 0)), + "restart_count": int(record.get("restart_count", 0)), } for stream_id, record in self.processes.items() }, "active_count": len(self.processes), "max_streams": MAX_STREAMS, + "last_failure": self.last_failure, }), encoding="utf-8") os.replace(temporary, STATUS_PATH) @@ -197,6 +211,8 @@ class StreamManager: "output_url": output_url, "low_latency": low_latency, "profile": profile, + "command": command, + "restart_count": 0, } self.processes[stream_id] = record deadline = time.monotonic() + STARTUP_GUARD_SEC @@ -205,6 +221,7 @@ class StreamManager: if process.poll() is not None: self.processes.pop(stream_id, None) error = self.read_error_log(record) + self.remember_failure(stream_id, process.returncode, error) self.close_error_log(record) self.write_status() message = f"Camera stream process exited during startup (code {process.returncode})" @@ -244,6 +261,15 @@ class StreamManager: except OSError: pass + def remember_failure(self, stream_id, exit_code, error): + self.last_failure = { + "stream_id": stream_id, + "exit_code": exit_code, + "error": error, + "at": int(time.time()), + } + print(json.dumps({"event": "camera_stream_process_failed", **self.last_failure}, separators=(",", ":")), flush=True) + def preview_start(self, payload): stream_id = validate_stream_id(payload.get("stream_id")) expires_at = time.time() + PREVIEW_TTL_SEC diff --git a/stage_ip_stream/test_ip_stream.py b/stage_ip_stream/test_ip_stream.py index 98179d2..351ec3d 100644 --- a/stage_ip_stream/test_ip_stream.py +++ b/stage_ip_stream/test_ip_stream.py @@ -148,6 +148,8 @@ class IpStreamTest(unittest.TestCase): source = console.read_text(encoding="utf-8") self.assertIn('error = str(result.get("error") or "").strip()', source) self.assertIn('raise RuntimeError(error or "Unable to start camera media operation")', source) + self.assertIn('if parsed.path == "/api/camera-stream/stop":', source) + self.assertIn('publish_camera_preview({"action": "stop", "stream_id": stream_id})', source) def test_supervisor_reaps_expired_and_dead_processes_without_status_request(self): manager = stream.StreamManager("g-box-test", "it-screen/dev") @@ -158,7 +160,7 @@ class IpStreamTest(unittest.TestCase): dead.poll.return_value = 7 manager.processes = { "expired": {"process": expired, "kind": "forward", "expires_at": 1}, - "dead": {"process": dead, "kind": "forward", "expires_at": 0}, + "dead": {"process": dead, "kind": "preview", "expires_at": 0}, } manager.write_status = MagicMock() @@ -167,8 +169,39 @@ class IpStreamTest(unittest.TestCase): self.assertEqual(manager.processes, {}) self.assertEqual({item["stream_id"] for item in stopped}, {"expired", "dead"}) + self.assertEqual(manager.last_failure["stream_id"], "dead") + self.assertEqual(manager.last_failure["exit_code"], 7) killpg.assert_called_once_with(1001, stream.signal.SIGTERM) + def test_supervisor_restarts_a_failed_forward_within_its_ttl(self): + manager = stream.StreamManager("g-box-test", "it-screen/dev") + dead = MagicMock(pid=1002) + dead.poll.return_value = 1 + replacement = MagicMock(pid=1003) + replacement.poll.return_value = None + error_log = tempfile.TemporaryFile(mode="w+b") + error_log.write(b"srt://cdn.example:8891?streamid=secret Input/output error") + manager.processes = { + "camera": { + "process": dead, + "error_log": error_log, + "kind": "forward", + "expires_at": stream.time.time() + 60, + "restart_count": 0, + "command": [stream.FFMPEG_BINARY, "-i", "rtsp://camera/stream2"], + }, + } + manager.spawn = MagicMock(return_value=replacement) + manager.write_status = MagicMock() + + stopped = manager.reap() + + self.assertTrue(stopped[0]["restarted"]) + self.assertEqual(manager.processes["camera"]["restart_count"], 1) + self.assertIs(manager.processes["camera"]["process"], replacement) + self.assertNotIn("streamid=secret", manager.last_failure["error"]) + manager.close_error_log(manager.processes["camera"]) + def test_run_starts_periodic_supervisor(self): source = MODULE_PATH.read_text(encoding="utf-8") self.assertIn('target=self.supervise, name="ip-stream-supervisor"', source) diff --git a/stage_ip_stream/version.json b/stage_ip_stream/version.json index 5d945d2..d608e61 100644 --- a/stage_ip_stream/version.json +++ b/stage_ip_stream/version.json @@ -1,3 +1,3 @@ { - "version": "1.0.9" + "version": "1.0.10" } diff --git a/stage_ui/console_core/backend/app.py b/stage_ui/console_core/backend/app.py index 857c072..d5659b2 100644 --- a/stage_ui/console_core/backend/app.py +++ b/stage_ui/console_core/backend/app.py @@ -62,7 +62,7 @@ APP_DIR = "/opt/iomatex-console" PUBLIC_DIR = os.path.join(APP_DIR, "public") PROVIDERS_DIR = os.path.join(APP_DIR, "providers.d") CONFIG_PATH = "/etc/iomatex/console.json" -UI_VERSION = "V 3.1.40" +UI_VERSION = "V 3.1.41" STORAGE_HEALTH_COMMAND = "/usr/local/sbin/iomatex-storage-health" ZONEINFO_ROOT = "/usr/share/zoneinfo" @@ -3994,6 +3994,18 @@ class Handler(BaseHTTPRequestHandler): except Exception as exc: self.send_json({"ok": False, "error": str(exc)}, code=503) return + if parsed.path == "/api/camera-stream/stop": + payload = self.read_json_body() + stream_id = str((payload or {}).get("stream_id", "")) + if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,63}", stream_id): + self.send_json({"ok": False, "error": "Invalid camera stream request"}, code=400) + return + try: + publish_camera_preview({"action": "stop", "stream_id": stream_id}) + self.send_json({"ok": True, "stream_id": stream_id}) + except Exception as exc: + self.send_json({"ok": False, "error": str(exc)}, code=503) + return if parsed.path == "/api/camera-preview/snapshot": payload = self.read_json_body() stream_id = str((payload or {}).get("stream_id", "")) diff --git a/stage_ui/console_core/frontend/console.js b/stage_ui/console_core/frontend/console.js index e2c1e95..aa9a758 100644 --- a/stage_ui/console_core/frontend/console.js +++ b/stage_ui/console_core/frontend/console.js @@ -49,7 +49,7 @@ { mode: "test_by", label: "test.it-screen.by", host: "rest.test.it-screen.by" }, { mode: "custom", label: "Custom", host: "" } ]; - const UI_VERSION = "V 3.1.40"; + const UI_VERSION = "V 3.1.41"; const ITSCREEN_LOG_INITIAL_LINES = 50; const ITSCREEN_LOG_BUFFER_LIMIT = 300; const ITSCREEN_LOG_BATCH_MS = 250; diff --git a/stage_ui/console_core/frontend/index.html b/stage_ui/console_core/frontend/index.html index 97176a3..fab164a 100644 --- a/stage_ui/console_core/frontend/index.html +++ b/stage_ui/console_core/frontend/index.html @@ -23,7 +23,7 @@ } catch (error) {} })(); - +
@@ -109,7 +109,7 @@
- - + + diff --git a/stage_ui/version.json b/stage_ui/version.json index 61194ac..aaa3c2f 100644 --- a/stage_ui/version.json +++ b/stage_ui/version.json @@ -1,3 +1,3 @@ { - "version": "3.1.40" + "version": "3.1.41" }