Skip to content

Restart recorder and motion ffmpeg when they stop making progress - #4

Open
hyngsk wants to merge 1 commit into
codextde:mainfrom
hyngsk:fix/restart-stalled-ffmpeg
Open

hyngsk wants to merge 1 commit into
codextde:mainfrom
hyngsk:fix/restart-stalled-ffmpeg

Conversation

@hyngsk

@hyngsk hyngsk commented Nov 24, 2026

Copy link
Copy Markdown

Problem

When the camera behind go2rtc drops and go2rtc reconnects within a few seconds (in my case the camera's RTSP server restarts every few minutes), the RTSP consumers stay connected but the RTP timestamps jump. After that:

  • the recorder ffmpeg stops writing the current segment. It buffers in memory, so the file stays at 48 bytes until the process exits.
  • the motion ffmpeg stops printing lavfi.scene_score lines, so no motion events are raised.

Neither process exits, so the exit-based restart in Supervisor never runs. Recording and motion detection stay dead until the container restarts.

Change

  • Supervisor gets an optional stallMs and a progress() method. If a running process has not reported progress for stallMs, it gets SIGINT (SIGKILL after 5 s), and the normal exit path restarts it with the usual backoff.
  • Motion: every scene score line counts as progress. stallMs is 60 s, which leaves room for -skip_frame nokey bursts.
  • Recorder: growth of the segment being written counts as progress. The existing 20 s doScan checks it. stallMs is 90 s.
  • Three tests in test/proc.test.ts: a silent process is restarted, progress() keeps it running, and nothing changes without stallMs.

Testing

  • bun test passes 76 of 76, and tsc --noEmit is clean.
  • Running on a real camera (ONVIF/RTSP over a VPN link). Sending SIGINT to a stalled recorder flushed the buffered segment (8.2 MB written at exit). Freezing the motion ffmpeg with SIGSTOP produced no progress for 65s, restarting, and scene scores came back right after the restart.

When the camera behind go2rtc drops and go2rtc reconnects within a few
seconds, the RTSP consumers keep their connection but the RTP timestamps
jump. The recorder ffmpeg then stops writing the segment (it buffers in
memory until it exits) and the motion ffmpeg stops printing scene scores.
Neither process exits, so the exit-based restart never kicks in and
recording/motion detection stay dead until the container restarts.

Supervisor gets an optional stallMs: when a running process has not
reported progress() for that long, it is sent SIGINT (SIGKILL after 5 s)
and the normal exit path restarts it.

- motion: every scene score line counts as progress (stallMs 60 s)
- recorder: growth of the segment being written, checked by the existing
  20 s scan, counts as progress (stallMs 90 s)

Seen with a camera whose RTSP server restarts every few minutes: the
current segment stayed at 48 bytes and no motion events were raised after
a reconnect; SIGINT flushed the buffered segment and both recovered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant