Skip to content

Limiting the RTMP connect to 5 seconds and releasing its lock on failure - #96

Open
erkin-io wants to merge 2 commits into
OpenIPC:masterfrom
erkin-io:fix/rtmp-connect-limit
Open

erkin-io wants to merge 2 commits into
OpenIPC:masterfrom
erkin-io:fix/rtmp-connect-limit

Conversation

@erkin-io

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

Copy link
Copy Markdown
Contributor

What was wrong

With stream.enable on and an RTMP destination that does not answer, the camera reboots again and again. Seen twice on a Ruision F8 board (FH8856V200) while testing RTMP: the hardware watchdog rebooted the board.

Why

Two faults in src/rtmp.c:

  • rtmp_start_sequence() connects with a blocking connect() and then waits for the handshake replies with no time limit. It runs on the main thread before the video starts, so a destination that drops the packets holds the program until the watchdog fires.
  • rtmp_init() holds rtmp_mutex and reports its errors with HAL_ERROR, which returns from the function (return EXIT_FAILURE is part of the macro). The mutex stays locked, and the next rtmp_close() (every stop or restart of the program) waits on it forever, again until the watchdog fires.

What this changes

  • SO_SNDTIMEO and SO_RCVTIMEO of 5 s on the socket during connect and handshake, set back to no limit once connected (the running stream keeps today's behaviour). A failed connect logs a warning.
  • HAL_DANGER instead of HAL_ERROR for the three messages in rtmp_init(); it logs the same red line without returning, so the existing pthread_mutex_unlock() and return ret; at the end are reached.

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: a destination that does not answer gives the warning after 5 s and the camera carries on without RTMP; stop and restart of the program work; RTMP to a listening FFmpeg still works.

Effect on other platforms

Shared code, used only with stream.enable and an rtmp:// destination. Socket timeouts are standard BSD socket options in glibc, musl and uClibc.

Update after the automatic review

A second commit makes the 5 s one deadline for the connect and the handshake together (each handshake read waits only for the time left), shuts the socket down before joining the receiver when the sender thread cannot be created, and closes the socket when the connect, createStream or publish step fails. Tested on the board against a server that answers one byte every 2 s: the camera now gives up after 5.0 s (before: as long as the server kept trickling).

🤖 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

Bound RTMP setup waits and release the mutex on initialization failure

🐞 Bug fix 🕐 10-20 Minutes

Grey Divider

AI Description

• Add five-second socket send and receive timeouts during RTMP setup to avoid indefinite waits
 before video starts.
• Restore unlimited socket waits after setup, preserving established-stream behavior.
• Keep failure logging from returning early so RTMP cleanup can release its mutex.
Diagram

graph TD
  A["Media startup"] --> B["RTMP init"] --> C["Timed socket setup"] --> D{"Setup succeeds?"}
  D -->|Yes| E["Restore socket waits"] --> F["Release mutex"] --> G["Continue startup"]
  D -->|No| F
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Use a single setup deadline
  • ➕ Enforces an overall time budget across connection and handshake operations.
  • ➖ Requires more involved nonblocking I/O and deadline handling than socket timeouts.

Recommendation: The socket-timeout approach is a small, targeted fix if the goal is to bound individual blocking socket calls. It does not guarantee RTMP initialization finishes within five seconds: DNS resolution is unaffected, and multiple calls can each consume their timeout. Use an overall deadline if a strict startup limit is required.

Files changed (1) +18 / -3

Bug fix (1) +18 / -3
rtmp.cBound RTMP setup socket waits and preserve mutex release on errors +18/-3

Bound RTMP setup socket waits and preserve mutex release on errors

• Applies five-second send and receive socket timeouts during RTMP connection and setup, warns on connection failure, and removes the timeouts after successful setup. Replaces three returning error macros with logging-only macros so thread-creation and connection failures reach the existing mutex unlock and return path.

src/rtmp.c

@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


Action required

1. A slow peer can still reboot the camera ✓ Resolved

Resolving commit: 5db8bca

Resolution reason
Introduces one absolute deadline (millis()+5000) in rtmp_start_sequence that covers connect and
handshake. The handshake now uses recv_until(), which recomputes the remaining time before every
recv() and resets SO_RCVTIMEO to that remainder, returning -1 once the deadline passes. This bounds
the total handshake time, matching the suggested fix's absolute-deadline approach. A warning is
logged on handshake timeout.
Description
rtmp_start_sequence() sets a five-second timeout on individual socket operations, while
rtmp_handshake() repeats recv() until both replies are complete. A peer that sends another byte
before each timeout can keep startup on the main thread indefinitely, preventing watchdog resets.

2. Sender startup failure can hang the camera ✓ Resolved

Resolving commit: 5db8bca

Resolution reason
Added shutdown(socket_fd, SHUT_RDWR) immediately before pthread_join(recvPid, NULL) in the
sender-thread-creation failure path, which unblocks the receiver's recv() so the join cannot hang.
This matches the suggested fix (shut down socket before joining, then close and complete existing
cleanup). The socket-close cleanup in the connect-failure branch was also added.
Description
After the sender-thread creation error, rtmp_init() now proceeds to join the receiver thread
before shutting down its socket. If that receiver is blocked in recv() against a silent peer, the
receive timeout has already been cleared and the join can block indefinitely.


Remediation recommended

3. Short watchdog periods still reset cameras ✓ Resolved

Resolving commit: 5db8bca

Resolution reason
Introduced a single 5000 ms deadline computed before connect, passed into rtmp_handshake(). New
recv_until() shrinks each recv timeout to the time remaining and fails once the deadline passes, so
the connect plus handshake cannot exceed the one 5s budget. A HAL_WARNING is logged on handshake
failure. This bounds the startup wait by a deadline, though it does not tie it to the watchdog
period or service the watchdog, so it is a different implementation of the same concern (bounding
startup waits) rather than the suggested watchdog-aware fix.
Description
rtmp_start_sequence() allows a blocking handshake read to consume the full five-second socket
timeout without servicing the watchdog. When the configured watchdog period is five seconds or less,
startup can reset the camera even if that read eventually times out and returns failure.

4. Failed streams can leak sockets on retry ✓ Resolved

Resolving commit: 5db8bca

Resolution reason
In rtmp_init(), after the failed connect/stream/publish sequence, the code now closes socket_fd if
open and resets it to -1 before returning EXIT_FAILURE, so a retry no longer overwrites a live
descriptor.
Description
rtmp_init() now unlocks and returns after a failed startup sequence, but the command-send failure
branches in rtmp_start_sequence() leave its opened socket in socket_fd. If initialization is
attempted again, the next socket assignment overwrites that descriptor, so later cleanup cannot
close 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

- One 5 s deadline for connect and handshake: each handshake read waits only for the time that is left, so a peer that trickles bytes cannot hold the start.
- Shut the socket down before joining the receiver when the sender thread cannot be created.
- Close the socket when the connect, createStream or publish step fails.

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).

  1. A peer that trickles bytes: correct. There is now one 5 s deadline for the connect and the handshake together; each handshake read waits only for the time that is left (recv_until()). Tested on a Ruision F8 board (FH8856V200) against a test server that sends the 1537-byte handshake reply one byte every 2 s: before, the camera waited 20.1 s (until the test server gave up); after, the camera closed the connection after 5.0 s, logged "Handshake not completed in time" and carried on, and RTSP kept working.
  2. Sender thread cannot be created: correct. The socket is now shut down before the receiver thread is joined, the order rtmp_close() already uses. Checked by reading; I could not make pthread_create() fail from outside.
  3. Watchdog of 5 s or less: the setup is now bounded at about 5 s in total, but a watchdog period at or below that is still at risk, and name resolution (gethostbyname()) is not bounded either. The watchdog is off by default. If you prefer, the limit can be derived from the configured watchdog period instead of fixed at 5 s.
  4. Socket left open when connect, createStream or publish fails: correct, and older than this change. rtmp_init() now closes the socket on that path. Checked by reading; those three sends are small and go into the socket buffer, so I could not force them to fail from outside.

🤖 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