Skip to content

Giving up on an RTSP/TCP client that takes no data for a second - #101

Open
erkin-io wants to merge 2 commits into
OpenIPC:masterfrom
erkin-io:fix/rtsp-stuck-tcp-client
Open

erkin-io wants to merge 2 commits into
OpenIPC:masterfrom
erkin-io:fix/rtsp-stuck-tcp-client

Conversation

@erkin-io

@erkin-io erkin-io commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What was wrong

One RTSP/TCP client that stops reading freezes the stream for every other client, for as long as it stays stuck. Reproduced on a Ruision F8 board (FH8856V200): a test client that plays over TCP and then reads nothing for 10 s gave a second, normal client an 8.4 s gap in the video and an 8.7 s gap in the audio. A viewer on a wireless link that stalls for a few seconds does the same in real use; we saw three such 7 s gaps in an hour.

Why

One thread sends each frame to all clients in turn (__rtp_send() walks the transfer list). For TCP clients the data is written to a non-blocking socket, and when the socket buffer is full the loops in __tcp_flush(), __rtp_send_eachconnection() and __rtcp_send_sr() wait (poll() for 100 ms, or usleep(1000)) and try again, with no limit. While they wait for one client, no other client gets anything, and the encoder's frames pile up or are lost.

A client that fails is also returned as FAILURE, which makes list_map_inline() stop, so the clients after it in the list miss the rest of that frame.

What this changes

  • A helper __tcp_send_all() in src/rtsp/rtsp.h sends a buffer completely. When the client takes no data for RTSP_TX_STALL_MS (1000 ms), it logs one line, marks the connection (tx_dead), and shuts the socket down. The RTSP thread then sees the end of input and frees the connection as it does for any client that leaves; the player can reconnect. A send error marks the connection the same way, without the log line.
  • __tcp_flush(), the unstaged path of __rtp_send_eachconnection() and the interleaved branch of __rtcp_send_sr() use the helper.
  • A marked or failed TCP client is skipped and SUCCESS is returned, so the other clients still get the frame. UDP clients are unchanged.
  • tx_dead is cleared when a connection slot is reused.

How it was checked

  • Built with ./build.sh fhan-v2, no new warnings; ./test.sh -a passes.
  • On a Ruision F8 board, in the firmware that normally runs on the board, with this change in the same form: the test client (plays over TCP with a 4 kB receive buffer, then reads nothing for 20 s) was disconnected after about one second of no progress, and the normal client's 40 s capture had 1001 video and 624 audio packets with no step off the 40 ms frame interval, no audio gap and no decode error, in two runs. Then one hour over RTSP/TCP: 90002 video and 56250 audio packets, no gap.
  • The same test on the firmware without the change reproduced the 8.4 s freeze described above.

Effect on other platforms

Shared RTSP code, every target. Only TCP clients that stop taking data are affected: they are now disconnected after about one second without progress (with the socket buffer the server sets, a client that falls behind has some seconds of slack before the buffer is full). clock_gettime(CLOCK_MONOTONIC), poll() and shutdown() are POSIX; tools.c already uses the clock. The one-second limit matches the read side (#85, open), which closes a client that stalls in the middle of an interleaved packet after one second.

Update after the automatic review

A second commit also shuts the socket down after a hard send error, so the RTSP thread frees such a client even without a read event. Tested on the board with viewers that reset their connection while being sent to: no connection left behind, the other viewer unaffected.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Disconnect stalled RTSP/TCP clients without interrupting other viewers

🐞 Bug fix 🕐 20-40 Minutes

Grey Divider

AI Description

• Disconnect TCP clients after one second without send progress to prevent stream-wide stalls.
• Continue delivering RTP and RTCP to other clients when one TCP send fails.
• Reset the failed-send state when a connection slot is reused.
Diagram

graph TD
  A["Frame sender"] --> B["RTP and flush"] --> D["TCP send helper"] --> E{"No progress 1s?"}
  A --> C["RTCP reports"] --> D
  E -- "Yes" --> F["Mark and shutdown"] --> G["RTSP cleanup"]
  E -- "No" --> H["Next client"]
  F -- "Continue traversal" --> H
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Per-client asynchronous send queues
  • ➕ A slow client would not block the shared sender for up to one second.
  • ➖ Requires substantially more queueing, backpressure, ordering, and lifecycle management.

Recommendation: The bounded-send approach is a proportionate fix for the existing shared sender and constrained devices. Per-client queues would offer stronger isolation but warrant a separate architectural change.

Files changed (4) +66 / -71

Bug fix (4) +66 / -71
rtcp.hBound interleaved RTCP sender-report writes +4/-23

Bound interleaved RTCP sender-report writes

• Replaces unbounded TCP header and report write loops with the shared send helper. A failed TCP report no longer stops processing subsequent clients; UDP reporting remains unchanged.

src/rtsp/rtcp.h

rtp.cKeep RTP delivery moving past failed TCP clients +17/-48

Keep RTP delivery moving past failed TCP clients

• Uses the shared helper for staged flushes and direct interleaved RTP writes. Failed or marked TCP clients are skipped without terminating transfer-list traversal or blocking delivery to later clients.

src/rtsp/rtp.c

rtsp.cClear failed-send state on connection reuse +1/-0

Clear failed-send state on connection reuse

• Resets tx_dead when a connection slot is assigned to a newly accepted client.

src/rtsp/rtsp.c

rtsp.hAdd progress-limited TCP sending and connection state +44/-0

Add progress-limited TCP sending and connection state

• Adds a shared send helper that retries non-blocking writes, tracks time without progress, and marks failed connections. After one second without progress it logs the stall and shuts down the socket so normal RTSP cleanup can release the client.

src/rtsp/rtsp.h

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Failed TCP clients can occupy all slots ✓ Resolved

Resolving commit: c684afe

Resolution reason
On the hard send-error path, the commit now calls shutdown(con->client_fd, SHUT_RDWR) right after
setting tx_dead = 1, so the RTSP read side sees EOF and runs its existing cleanup, freeing the slot
and transport resources. This matches the suggested fix (shut down the client socket on the
hard-error path so the RTSP thread observes EOF).
Description
__tcp_send_all() sets tx_dead on a non-retryable send error but does not shut down the socket or
mark the connection disconnected. If that error occurs without a read-side EOF or other read event,
the RTSP thread keeps the connection and its transport resources allocated while all subsequent TCP
media sends skip it.

Grey Divider

Tip of the day
💡 Did you know, you can enable the Remediation agent and Qodo fixes findings in a dedicated fix PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

A send error other than a full buffer marked the client but left its socket open; without a read event the RTSP thread would keep its slot. The socket is now shut down as in the stall case, so the RTSP thread frees the connection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@erkin-io

Copy link
Copy Markdown
Contributor Author

Updated after the automatic review (second commit).

Hard send error without a read event: correct. The socket is now shut down on that path as well, as in the one-second stall case, so the RTSP thread sees the end of input and frees the connection.

Tested on a Ruision F8 board (FH8856V200) with the second commit:

  • three viewers that reset their connection (RST) while the camera was sending to them, during a normal 30 s RTSP/TCP capture: 3 s after the last reset only the normal viewer was still connected; its capture had 751 video packets, no step off the 40 ms frame interval and no decode error;
  • a viewer that stops reading for 10 s: disconnected after one second without progress, the other viewer got 1001 video and 624 audio packets with no gap.

In the reset test the RTSP thread also saw the reset on its read side, so that test cannot show the case the review describes (an error with no read event); the change makes the cleanup independent of it.

🤖 Generated with Claude Code

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