Skip to content

video: fix CPU spin on waiting WebSocket viewers and zombie leak - #50

Merged
tridge merged 4 commits into
ArduPilot:mainfrom
tridge:pr-fix-zombies
Nov 13, 2026
Merged

tridge merged 4 commits into
ArduPilot:mainfrom
tridge:pr-fix-zombies

Conversation

@tridge

@tridge tridge commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Two bugs seen on the production server: several video children each spinning a full core, and hundreds of defunct processes.

CPU spin. A WebSocket viewer that completed its handshake before the slot had a joinable stream stayed in detect and was never read again. Its close frame or EOF re-fired level-triggered EPOLLIN on every epoll_wait, so the video child spun and the viewer slot was never freed, eventually refusing all new viewers with "slot full". Internet scanners were enough to trigger it.

  • read waiting viewers on every pump and readable event
  • only treat the handshake as done once the 101 has been fully sent
  • time out a stalled WebSocket/TLS handshake (10s)
  • bound pongs queued for a peer that pings without reading, and flush queued WebSocket output on EPOLLOUT

Zombies. VideoWriter::close_segment() forked an fsync helper that was never reaped, leaving one zombie per segment rotation. Now double forks, waiting only on the intermediate PID so it can't interfere with the RTSP backend's waitpid.

Tests. New regressions for a closed waiting viewer (close frame and FIN), a stalled handshake, a ping flood, and no zombies after segment rotation. Each fails without the fix and passes with it; full suite passes.

A WebSocket viewer that completed its handshake before the slot had a
joinable stream stayed in detect and was never read again. Its close
frame or EOF re-fired level-triggered EPOLLIN on every wait, so the
video child spun a full core and the viewer slot was never freed,
eventually refusing every new viewer with "slot full".

Read waiting viewers on every pump and readable event, only treat the
handshake as done once the 101 has been sent, and time out a handshake
that stalls. Bound pongs queued for a peer that pings without reading,
and flush queued WebSocket output when the socket becomes writable.
close_segment() forked an fsync helper that the video child never
reaped, leaving one defunct process per segment rotation. Double fork
so the helper is reaped by init, waiting only on the intermediate PID.
@tridge tridge added the AIReview Request an automated AI review; picked up by the reviewprs sweep label Nov 12, 2026
@AP-Review

AP-Review commented Nov 12, 2026 •

Copy link
Copy Markdown

Deprecated — see below for the updated review.

Previous review (2026-11-12)

Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting.
Verdict: COMMENT

Reviewed at head f9fd9e3b39.
Full report: https://firmware.ardupilot.org/Tools/APReview/DevCallReviews/PRReviews/ardupilot/supportproxy/50/1.html#prSupportProxy-50

The CPU-spin, handshake-timeout and zombie-leak fixes look correct, but one of the new tests fails reliably on a recent kernel, and there are a few small follow-ups.

Should fix

  • tests/test_video_view.py:617: test_ping_flood_without_reading_is_dropped fails every time here (WSL2, kernel 6.18). Setting SO_RCVBUF after the handshake stalls the loopback connection before 1 MiB of pongs can queue. Setting it before connect() makes it pass, e.g. via an option on ws_connect().

Low

  • videoview.cpp:526: pongs queued by the periodic begin_ws_pump() read can sit unsent, because pump_viewers() continues without epoll_sync_viewer(), so EPOLLOUT is never armed until the next inbound frame. Sync the viewer's events after the WS pump.
Minor (2)
  • websocket.cpp:347: the new pong bound only checks the unsent backlog. A peer that keeps reading slightly behind still grows tx, because the sent prefix is only freed on a full drain (pre-existing, also affects send()). Compacting a large sent prefix would complete the bound.
  • tests/test_video_view.py:650: the inserted class took the @pytest.mark.integration that belonged to TestWebSocketViewer, which is now unmarked. Re-add the decorator above it.

Sent bytes were only freed when the queue fully drained, so a peer that
reads but stays slightly behind grew tx without bound even though the
unsent backlog stayed under WS_MAX_TX_QUEUE. Erase the sent prefix once
it reaches 64 KiB.
The periodic pump of a viewer waiting for a stream can queue pongs, but
skipped the epoll mask sync, so they sat unsent until the next inbound
frame.

Tests: set SO_RCVBUF before connect() in the ping-flood test, since
setting it afterwards stalls loopback on some kernels before the queue
limit is reached, and restore the integration marker on
TestWebSocketViewer.
@AP-Review

Copy link
Copy Markdown

Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting.
Verdict: ACCEPT

Reviewed at head 526a28f606.
Full report: https://firmware.ardupilot.org/Tools/APReview/DevCallReviews/PRReviews/ardupilot/supportproxy/50/2.html#prSupportProxy-50

No open issues remain at 526a28f, and I found nothing new.

Everything from the last review is fixed. The ping-flood test now sets SO_RCVBUF before connect() and passes on the kernel where it failed. The waiting-viewer pump now arms EPOLLOUT for queued pongs (video.cpp:1662). queue_frame() compacts the sent prefix (websocket.cpp:504). The integration mark is back on TestWebSocketViewer (tests/test_video_view.py:652).

@tridge
tridge merged commit 80d06ed into ArduPilot:main Nov 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AIReview Request an automated AI review; picked up by the reviewprs sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants