Repository navigation
fix: stop serving and scoring frozen frames when a patrol hangs - #458
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #458 +/- ##
===========================================
+ Coverage 81.05% 84.32% +3.27%
===========================================
Files 7 7
Lines 871 938 +67
===========================================
+ Hits 706 791 +85
+ Misses 165 147 -18
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Tested on pi-test (Pi 5, Debian 13, Docker 29), branch develop + #419 + #459 + #461 (includes this PR), images built on the Pi, real cameras unplugged. Reolink call that hangs: OK
Camera API frozen, healthcheck restarts it: OK
Not tested on the Pi: the Remark: the kill is inside the healthcheck command, so it fires on the first failed check and |
… camera - The pass only collected the frames it received, so one night frame next to a camera answering 204 (no fresh image, PR #458) read as "every camera reports night" and put the site to sleep for an hour. The site now sleeps only when every expected frame came back and all of them are night frames; one day frame still wakes it. - After the night sleep, only the first camera was probed: in infrared or down, it kept the whole site asleep in daylight. Every camera is probed until one shows daylight, and the site stays asleep only when all of them answer night. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… camera - The pass only collected the frames it received, so one night frame next to a camera answering 204 (no fresh image, PR #458) read as "every camera reports night" and put the site to sleep for an hour. The site now sleeps only when every expected frame came back and all of them are night frames; one day frame still wakes it. - After the night sleep, only the first camera was probed: in infrared or down, it kept the whole site asleep in daylight. Every camera is probed until one shows daylight, and the site stays asleep only when all of them answer night.
719ea53 to
3bc0f60
Compare
… camera - The pass only collected the frames it received, so one night frame next to a camera answering 204 (no fresh image, PR #458) read as "every camera reports night" and put the site to sleep for an hour. The site now sleeps only when every expected frame came back and all of them are night frames; one day frame still wakes it. - After the night sleep, only the first camera was probed: in infrared or down, it kept the whole site asleep in daylight. Every camera is probed until one shows daylight, and the site stays asleep only when all of them answer night.
fe51
left a comment
There was a problem hiding this comment.
@MateoLostanlen Reviewed the diff and the code around it. No blocker in the mechanics, CI is green. A few points on behaviour, most important first.
1. A camera stuck in infrared goes blind silently, and one IR frame wipes its history (core.py, _analyze)
- A camera stuck in IR during the day is no longer analyzed. Since
predictisn't called, it also stops sending heartbeats, and the only trace is one INFO line per frame. #461 won't catch it either, as IR frames are fresh and changing. - A single night frame runs
self.engine._states.pop(cam_id, None), which also dropsongoingand the frozen crop boxes. An isolated IR frame during an ongoing fire restarts that event from zero.
Suggestion: only drop the state after N consecutive night frames, and log a WARNING when a camera is in IR while others on the site see daylight.
2. The healthcheck can restart the API after about 25 s of load (docker-compose.yml)
The kill is inside the test command, so retries: 3 has no effect (as you noted). The only margin is 3 curl attempts, about 25 s. A saturated Pi could therefore restart the whole camera API, with every camera and any live stream. A restart may also skip the zoom restore done when a stream stops (not verified).
Suggestion: wait about 2 min by extending the curl retries, with timeout above their total duration (not tested):
test: ["CMD-SHELL", "curl -sf --max-time 5 --retry 8 --retry-delay 10 http://localhost:8081/health/patrols; rc=$$?; [ $$rc -eq 0 ] && exit 0; [ $$rc -eq 7 ] && exit 1; kill -9 -1"]
timeout: 140s
That gives 9 × 5 s + 8 × 10 s = 125 s before the kill, which is still small next to the 10 min STALL_LIMIT.
what do you think ?
3. One dead camera keeps the site awake all night (can be a follow-up)
With cameras A and B on night frames and C dead, len(day_frames) == expected is never true, and _probe_day also wakes the site when a camera doesn't answer. Detection isn't affected, since night frames are skipped, but PTZs patrol all night and the hourly autofocus runs on IR images. With #461, C's patrol also stays active overnight, so the detector keeps counting no_fresh_image and spends its reboot attempts on it.
Suggestion: only count the cameras that answered, with N passes of confirmation so one frame can't put the site to sleep.
4. Identical frame warning (minor, optional)
The comment says a live camera never returns the same pixels twice, but the engine reads the API cache. When a pass is shorter than the patrol cycle, reading the same image twice is normal, so skipping it is fine but the WARNING can be a false signal. #461 handles the real failure by comparing actual captures through last_image_at, then rebooting the camera. I would keep the digest here to avoid re-uploads, and move the log to DEBUG with a corrected comment.
Points 1 and 2 are the ones I'd settle before merging.
Co author with Claude
|
Hi @fe51, thanks for the detailed review! All four points are addressed in 10cc832:
Also merged develop (#417) and fixed two more things in 137c461: an unchanged frame no longer counts as a new night frame, and it still sends the camera heartbeat. |
…atrols The Shelly watchdog reads /health to decide whether to power-cycle the Pi.
The healthcheck kills on every curl failure but rc 7, which looked like a hole: a uvicorn that never binds would stay unhealthy forever. Killing on rc 7 would loop at startup instead, as the kill runs during start_period too, and a hang before the bind is bounded by the 10 s timeout on every camera call.
… camera - The pass only collected the frames it received, so one night frame next to a camera answering 204 (no fresh image, PR #458) read as "every camera reports night" and put the site to sleep for an hour. The site now sleeps only when every expected frame came back and all of them are night frames; one day frame still wakes it. - After the night sleep, only the first camera was probed: in infrared or down, it kept the whole site asleep in daylight. Every camera is probed until one shows daylight, and the site stays asleep only when all of them answer night.
…at answered A single infrared frame dropped the camera's detection history, and with it an ongoing event: cameras flicker between colour and infrared at dusk. The history is now dropped after 3 night frames in a row, and a camera in infrared while the others see daylight is warned once, as it sends no heartbeat. A camera without a fresh image never counted as night, so one dead camera kept the site awake all night: PTZ patrols ran, autofocus ran on infrared images, and the stuck detector spent its reboot budget overnight. Only cameras with a fresh frame count now, and the site sleeps after 3 all-night passes. The frozen frame case is already handled by the API, which drops it after max_age_s. Same rule for the wake-up probe. The identical frame log goes to DEBUG: the engine reads the API cache, which the patrol refreshes slower than the loop runs, so the same frame twice is the normal case. The API's stuck detector diagnoses frozen cameras. The healthcheck killed the API after 25 s of failed checks, as the kill runs inside the check and `retries` gives no margin. The probe now retries for about 2 min, still well under the 10 min stall limit. The image ships no curl anymore, so the probe uses Python's HTTP client with the same retries. Also: - an unchanged frame no longer counts as a new night frame, so one cached infrared capture read on several passes cannot drop an ongoing event - an unchanged frame still sends the camera heartbeat, so a slow URL camera does not look offline
137c461 to
d26367e
Compare
fe51
left a comment
There was a problem hiding this comment.
Thanks @MateoLostanlen, all four points are addressed and the night logic is much safer now. LGTM.
One note for the PR description: since an identical frame is no longer re-scored, the same image no longer fills the sliding window several times. Alerts are more trustworthy, but they can take one or two more rounds than before, and much longer on a network refreshing slowly (e.g. 10 min). Worth a line so nobody is surprised if alert latency moves.
|
Thanks @fe51! Added the note on alert latency to the description: it only affects webcams that refresh their snapshot slowly, not our cameras. |
#461) * fix(reolink): default timeout on every camera request * fix(camera-api): return 204 from latest_image when the frame is stale * fix(camera-api): restart the container when a patrol thread hangs * refactor(camera-api): restart on a hung patrol from the compose healthcheck * fix(camera-api): keep /health as liveness, check patrols on /health/patrols The Shelly watchdog reads /health to decide whether to power-cycle the Pi. * docs(compose): say why connection refused does not kill the camera API The healthcheck kills on every curl failure but rc 7, which looked like a hole: a uvicorn that never binds would stay unhealthy forever. Killing on rc 7 would loop at startup instead, as the kill runs during start_period too, and a hang before the bind is bounded by the 10 s timeout on every camera call. * fix(engine): stop running patrols at night * fix(engine): decide day or night per frame, sleep only when every camera is at night * fix(engine): skip a frame identical to the previous one for the same pose * fix(engine): keep day evidence on prediction errors and drop night history * fix(engine): a missing frame never proves night, wake-up probes every camera - The pass only collected the frames it received, so one night frame next to a camera answering 204 (no fresh image, PR #458) read as "every camera reports night" and put the site to sleep for an hour. The site now sleeps only when every expected frame came back and all of them are night frames; one day frame still wakes it. - After the night sleep, only the first camera was probed: in infrared or down, it kept the whole site asleep in daylight. Every camera is probed until one shows daylight, and the site stays asleep only when all of them answer night. * fix(engine): confirm night over several frames, count only cameras that answered A single infrared frame dropped the camera's detection history, and with it an ongoing event: cameras flicker between colour and infrared at dusk. The history is now dropped after 3 night frames in a row, and a camera in infrared while the others see daylight is warned once, as it sends no heartbeat. A camera without a fresh image never counted as night, so one dead camera kept the site awake all night: PTZ patrols ran, autofocus ran on infrared images, and the stuck detector spent its reboot budget overnight. Only cameras with a fresh frame count now, and the site sleeps after 3 all-night passes. The frozen frame case is already handled by the API, which drops it after max_age_s. Same rule for the wake-up probe. The identical frame log goes to DEBUG: the engine reads the API cache, which the patrol refreshes slower than the loop runs, so the same frame twice is the normal case. The API's stuck detector diagnoses frozen cameras. The healthcheck killed the API after 25 s of failed checks, as the kill runs inside the check and `retries` gives no margin. The probe now retries for about 2 min, still well under the 10 min stall limit. The image ships no curl anymore, so the probe uses Python's HTTP client with the same retries. Also: - an unchanged frame no longer counts as a new night frame, so one cached infrared capture read on several passes cannot drop an ongoing event - an unchanged frame still sends the camera heartbeat, so a slow URL camera does not look offline * feat(camera-api): detect frozen cameras and missing images, cap reboots * feat(camera-api): run the stuck detector on every camera, add ALLOW_STILL_CAMERA * fix(camera-api): read stored images from one copy, report reboot attempts * fix(camera-api): persist the reboot budget, hide the camera password, grace after reboot - Reboot attempts were in memory with monotonic times, so every container restart (healthcheck, update) handed out three new attempts. They are now wall-clock times saved in data/camera_recovery.json (RECOVERY_STATE_FILE) and loaded at startup, pruned to the last 24 h. - `Reboot raised: <exc>` logged the Reolink URL with the camera password. Only the exception type is logged. - A static camera rebooted by the detector failed its next two captures while booting and entered the 30 min skip window. The detector now resets the failure count and gives it 90 s before captures count again. - /patrol_status iterated the attempts deque while the detector appended to it: both sides now hold a lock. - "reboot budget spent" is logged once per episode instead of every 5 min. * fix(camera-api): report the saved reboot attempt after a restart /patrol_status showed one attempt in the last 24 h but no last_reboot_attempt once the detector restarted: derive it from the attempts loaded from disk. * fix(camera-api): ignore frozen and log once for cameras that cannot reboot A URL or REST source may serve the same cached picture for minutes, which was seen as frozen and logged an error every check. --------- Co-authored-by: Mateo Lostanlen <mateo@pyronear.org>
Part of pyronear/roadmap#16, fixes #422. Replaces #459, closed in favour of this one.
A patrol thread could hang on a Reolink call without timeout. The camera API then served the same frame for hours, and the engine either scored dead frames or slept all day as "night". Both sides are fixed here.
Camera API
Engine
Since an unchanged frame is no longer re-scored, the same image can no longer fill the sliding window several times. This does not affect our cameras, but on webcams that refresh their snapshot slowly (e.g. every 10 min) an alert now needs several distinct images and can take much longer.