Repository navigation
video: fix CPU spin on waiting WebSocket viewers and zombie leak - #50
Conversation
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.
Previous review (2026-11-12)Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting. Reviewed at head 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
Low
Minor (2)
|
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.
|
Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting. Reviewed at head 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). |
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.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'swaitpid.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.