fix: limit video device open attempts to prevent infinite loops in streaming

This commit is contained in:
Adam Shiervani 2025-11-20 23:11:58 +01:00
parent 9c4a9e144f
commit 91e58d7205
1 changed files with 15 additions and 2 deletions

View File

@ -387,6 +387,10 @@ void *run_video_stream(void *arg)
streaming_stopped = false; streaming_stopped = false;
// Prevent infinite loop: count how many times we've tried to open device in this thread
int open_attempts = 0;
const int MAX_OPEN_ATTEMPTS = 20;
while (streaming_flag) while (streaming_flag)
{ {
if (detected_signal == false) if (detected_signal == false)
@ -395,6 +399,13 @@ void *run_video_stream(void *arg)
continue; continue;
} }
open_attempts++;
if (open_attempts > MAX_OPEN_ATTEMPTS) {
log_error("Tried to open video device %d times without success. "
"Exiting to trigger supervisor restart.", MAX_OPEN_ATTEMPTS);
exit(1);
}
int video_dev_fd = open(VIDEO_DEV, O_RDWR); int video_dev_fd = open(VIDEO_DEV, O_RDWR);
if (video_dev_fd < 0) if (video_dev_fd < 0)
{ {
@ -538,7 +549,8 @@ void *run_video_stream(void *arg)
r = select(video_dev_fd + 1, &fds, NULL, NULL, &tv); r = select(video_dev_fd + 1, &fds, NULL, NULL, &tv);
if (r == 0) if (r == 0)
{ {
log_info("select timeout"); log_info("select timeout (num_frames=%u, open_attempts=%d/%d)",
num, open_attempts, MAX_OPEN_ATTEMPTS);
break; break;
} }
if (r == -1) if (r == -1)
@ -547,7 +559,8 @@ void *run_video_stream(void *arg)
{ {
continue; continue;
} }
log_error("select in video streaming"); log_error("select error in video streaming: %s (open_attempts=%d/%d)",
strerror(errno), open_attempts, MAX_OPEN_ATTEMPTS);
break; break;
} }
memset(&buf, 0, sizeof(buf)); memset(&buf, 0, sizeof(buf));