Skip to content

video: a WebSocket viewer's backlog waits in the ring, not the send queue - #53

Merged
tridge merged 4 commits into
ArduPilot:mainfrom
tridge:pr-video-ws-backpressure
Nov 27, 2026
Merged

tridge merged 4 commits into
ArduPilot:mainfrom
tridge:pr-video-ws-backpressure

Conversation

@tridge

@tridge tridge commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A WebSocket video viewer on a long path was disconnected every time it joined a high-bitrate stream, and reconnected into the same thing.

Cause

WebSocket::send() queues what the socket will not take and reports the whole frame as sent. The viewer loop in videoview.cpp therefore never saw a full socket, and kept queueing until the 1 MiB WS_MAX_TX_QUEUE limit closed the connection:

WebSocket: output queue full, dropping connection
[11094] video slot 1 viewer disconnected after 1536 KiB (write error)

A new viewer is sent everything from the last keyframe. On a 12.5 Mbit/s stream that is about 1.5 MB, more than the queue holds while TCP is still in slow start on a long path. Viewers close to the proxy drain it in time; viewers in Japan and the US were dropped on every join (264 times in one log).

Fix

  • The viewer stops while the WebSocket has output queued, for the stream prefix and the stream itself. The backlog waits in the ring, where the existing lapped and chronically-behind limits apply.
  • A viewer that used its write budget while still behind asks to be called again, rather than waiting for the next ingest. Without this a viewer with a backlog caught up only as fast as the publisher sent packets.

Testing

  • New test test_ws_viewer_slow_to_start_is_not_dropped: a WebSocket viewer with a small receive buffer does not read while 3 MB is published, then must receive more than 2 MB as a verbatim span of the stream with no disconnect. It fails before the fix with "output queue full" and passes after.
  • pytest tests --ignore=tests/webadmin: 419 passed, 1 skipped.
  • Not tested over a real long-distance link.

@tridge tridge added the AIReview Request an automated AI review; picked up by the reviewprs sweep label Nov 26, 2026
@AP-Review

AP-Review commented Nov 27, 2026 •

Copy link
Copy Markdown

Deprecated — see below for the updated review.

Previous review (2026-11-27)

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

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

The fix holds up: a WebSocket viewer's backlog now stays in the ring instead of piling into the send queue, and the new slow-start test fails on the base and passes on this head. Only two small follow-ups remain, neither blocking.

Minor (2)
  • videoview.cpp:652: while a queued frame drains, ws_->flush() progress does not refresh last_progress_, so a WS viewer draining one frame slower than about 1.6 KB/s is dropped as stalled while bytes are still moving (same at :634). Refresh last_progress_ when the flush drains bytes, for parity with raw viewers.
  • videoview.cpp:636: the prefix-stage timeout returns false without setting drop_reason_, so the log says "closed" instead of "stalled". Set drop_reason_ = "stalled" first.

@AP-Review

AP-Review commented Nov 27, 2026 •

Copy link
Copy Markdown

Deprecated — see below for the updated review.

Previous review (2026-11-27)

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

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

The ring-not-queue fix still holds at ffd91ec, and this update adds no new problems.

Since the last review at e4857ea, both earlier minors are fixed: a slowly draining WebSocket viewer now counts as making progress, and prefix-stage timeouts are logged as "stalled".

Minor (1)
  • tests/test_video_view.py:761: test_ws_viewer_that_never_reads_is_dropped_as_stalled also passes on e4857ea, so it pins neither the slow-drain refresh (videoview.cpp:598) nor the prefix "stalled" labels (:643, :653). A test that stops ingest and drains one queued frame slower than 16 KiB per VIDEO_VIEWER_STUCK_S would cover them.

Withdrawn: the unlabelled hard-error drop at videoview.cpp:656 is unchanged from the base, so it is out of scope here.

…ueue

WebSocket::send() queues what the socket will not take and reports the
whole frame as sent, so the viewer loop never saw a full socket and kept
queueing until the 1 MiB limit closed the connection ("output queue
full", logged as a write error). A viewer on a long path joining a
12 Mbit/s stream was dropped on every join, as the data from the last
keyframe is more than the queue holds while TCP is still in slow start.

The viewer now stops while the WebSocket has output queued, leaving the
backlog in the ring where the lapped and chronically-behind limits
apply. A viewer that used its write budget while still behind asks to be
called again rather than waiting for the next ingest.
… is logged as one

Bytes leaving the WebSocket queue now count as progress, as bytes
accepted by a raw viewer's socket do, so a viewer draining one frame
slowly is not dropped while data is still moving. A viewer that stops
taking the stream prefix is logged as stalled rather than closed.
…Socket viewers

A viewer that takes one queued frame over several stall times is not
dropped, and a viewer stalled before the stream prefix is logged as
stalled. Both fail without the previous commit.
@tridge
tridge force-pushed the pr-video-ws-backpressure branch from 95ba01f to 04c201f Compare November 27, 2026 09:02
@AP-Review

AP-Review commented Nov 27, 2026 •

Copy link
Copy Markdown

Deprecated — see below for the updated review.

Previous review (2026-11-27)

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

Reviewed at head 04c201f50f.
Full report: https://firmware.ardupilot.org/Tools/APReview/DevCallReviews/PRReviews/ardupilot/supportproxy/53/4.html#prSupportProxy-53

The ring-not-queue fix and the slow-drain and stall-label changes all hold at 04c201f, with one minor gap left.

Since the last review at ffd91ec, the missing test coverage is fixed: both new tests fail without af760c5 and pass on this head.

Minor (1)
  • videoview.cpp:598: progress made by the pong flush inside ws_->recv() (the WS branch of on_readable, videoview.cpp:315) is not counted. Only the flush in on_writable refreshes last_progress_, so a slow viewer that sends pings can be dropped as "stalled" while video bytes are still going out. Refresh last_progress_ when pending_output() drops across ws_->recv() too (or track sent bytes inside WebSocket).

The earlier review's "no new problems" and "a slowly draining viewer is not stalled" are superseded by the item above; the withdrawn hard-error drop stays out of scope.

A WebSocket viewer's queued video also drains when a pong is flushed on
the read path. Only the flush on the write path refreshed the stall timer,
so a slow viewer that sends pings could be dropped as stalled while its
video was still going out. The WebSocket now counts the bytes it has
handed to the socket and the viewer compares that count.
@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 e836205559.
Full report: https://firmware.ardupilot.org/Tools/APReview/DevCallReviews/PRReviews/ardupilot/supportproxy/53/5.html#prSupportProxy-53

The new commit e836205 fixes the one open item, and no new problems turned up at this head.

Since the last review at 04c201f, the ping-path progress gap is fixed. WebSocket::flush() now counts every byte it sends (websocket.cpp:550), and on_writable() refreshes the stall timer whenever that count moves (videoview.cpp:598). So output flushed while answering a ping counts as progress. The new test (test_video_view.py:803) fails on 04c201f and passes on this head.

Not fully reviewed: the WSS/TLS version of a slow, pinging viewer and the full test suite were not run; only the video and WebSocket tests were.

@tridge
tridge merged commit d5ca8e4 into ArduPilot:main Nov 27, 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