Skip to content

nest: close PeerConnection/RTSP client on failed dial (fd + ICE mDNS socket leak) - #2378

Merged
AlexxIT merged 2 commits into
AlexxIT:masterfrom
ajplotkin:fix/nest-close-pc-on-failed-dial
Nov 28, 2026
Merged

AlexxIT merged 2 commits into
AlexxIT:masterfrom
ajplotkin:fix/nest-close-pc-on-failed-dial

Conversation

@ajplotkin

Copy link
Copy Markdown
Contributor

Summary

pkg/nest/client.go leaks connections on error paths:

  • rtcConn() creates a webrtc.PeerConnection per dial attempt but never calls pc.Close() on any failure. Each abandoned pc keeps its ICE agent's UDP sockets open — including two mDNS sockets on :5353 (one udp4, one udp6, opened by pion's default MulticastDNSModeQueryOnly), plus its gathered host-candidate sockets and agent goroutines. These are never reclaimed on their own: the pc never reaches ICE connectivity (no answer is set on the failure path), so pion's connection-state auto-close never fires. Accumulation is unbounded.
  • rtspConn() has the analogous bug: if Describe() fails after Dial() succeeds, the RTSP client's TCP connection is never closed.

Impact (observed)

  • go2rtc 1.9.14 on a Raspberry Pi 4, --network host, Nest WEB_RTC cameras via Device Access / SDM.
  • A couple of cameras were powered off. go2rtc redials each roughly every ~2 min, and each dial to a powered-off camera returns wrong status: 400 Bad Request from SDM.
  • After ~40 min: 124 open UDP sockets on :5353 owned by the single go2rtc process, climbing steadily. That matches the leak arithmetic — a persistently-failing dial leaks 3 PeerConnections per cycle × 2 mDNS sockets = 6/cycle, and ~20 dial cycles across the off cameras in ~40 min ≈ 120 sockets.
  • On --network host alongside other mDNS responders, local mDNS discovery then degraded. (The socket accumulation and its cause are confirmed from source; the downstream "degrades host mDNS" step is an observed correlation on that host.)

Root cause

In rtcConn's retry loop, pc is created and then abandoned on every error path — none closes it. The ExchangeSDP failure path is the worst: it continues the loop, which creates a new pc next iteration while the previous one leaks.

Fix

Close the connection on each failure path. Success paths are unchanged — there the returned WebRTCClient / RTSPClient owns the connection and closes it on teardown (Stop() → Connection.Stop() closes the pc). pc.Close() is idempotent and non-blocking here, and reclaims the whole per-pc fd/goroutine footprint, not just the mDNS sockets.

Notes

  • The retry loop retries a hard SDM 400 up to 3× with backoff; for a persistently-off camera that's wasted work and triples the per-cycle leak. Reusing one pc/offer across the ExchangeSDP attempts (the 400 doesn't invalidate the local offer), or not retrying a hard 400 at all, would be a reasonable follow-up — kept out of scope here to keep the fix minimal.
  • The Nest dial never needs pion's ICE mDNS (answers come from remote cloud peers that can't offer usable .local candidates), so a nest-scoped SetICEMulticastDNSMode(Disabled) would avoid opening the :5353 sockets entirely — but that should be scoped to the nest path only (the shared WebRTC server API also serves browser viewers that legitimately send .local host candidates). Not needed to fix this leak; closing the pc is the correct minimal fix.

rtcConn creates a PeerConnection per dial attempt but never closes it on
any error path. A failed dial leaves the pc unclosed, and because it never
reaches ICE connectivity (no answer is set), pion's connection-state
auto-close never fires either. Each abandoned pc keeps its ICE agent's UDP
sockets open, including two mDNS sockets on :5353 (udp4+udp6, from pion's
default MulticastDNSModeQueryOnly), plus its gathered host-candidate
sockets and agent goroutines.

On a host that repeatedly fails to dial a camera (e.g. a powered-off Nest
camera returning HTTP 400 on every reconnect), these accumulate without
bound. The retry loop makes it worse: it continues after a failed
ExchangeSDP without closing the previous pc, leaking up to 3 per cycle.
Observed 124 open :5353 sockets in one go2rtc process after ~40 min on a
--network host deployment, which then degraded host mDNS discovery.

rtspConn has the analogous bug: if Describe() fails after Dial() succeeds,
the RTSP client's TCP connection is never closed.

Close the connection on every failure path. Success paths are unchanged;
there the returned client owns the connection and closes it on teardown.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ajplotkin added a commit to ajplotkin/go2rtc that referenced this pull request Jul 26, 2026
rtcConn/rtspConn never closed the connection on error paths. A failed
dial (e.g. a powered-off Nest camera returning HTTP 400) leaked the
PeerConnection, which never reaches ICE connectivity so pion's auto-close
never fires — each abandoned pc kept its ICE agent's mDNS socket (:5353)
open. Observed 142 leaked :5353 sockets in one go2rtc process on the Pi;
they starve host mDNS and break HomeKit discovery.

Deployed to the Pi as go2rtc-nestfix:v5-20260726 (this fix on 43fc3b3):
post-fix, 4 failed off-camera dials left the :5353 count flat at 2.

Upstream PR: AlexxIT#2378.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@AlexxIT AlexxIT self-assigned this Oct 2, 2026
@AlexxIT AlexxIT added kind/feature Proposes or implements a new capability or enhancement brand/nest Google Nest cameras and doorbells size/S PR with 10-29 lines changed, excluding generated files. ai Submission contains text or code generated by AI labels Oct 2, 2026
@AlexxIT AlexxIT added kind/bug Reports a defect or fixes one and removed kind/feature Proposes or implements a new capability or enhancement labels Nov 25, 2026 — with ChatGPT Codex Connector
@AlexxIT AlexxIT added size/XS PR with 0-9 lines changed, excluding generated files. and removed size/S PR with 10-29 lines changed, excluding generated files. labels Nov 26, 2026
@AlexxIT
AlexxIT merged commit 5c9ee07 into AlexxIT:master Nov 28, 2026
@AlexxIT AlexxIT added this to the master milestone Nov 28, 2026
@AlexxIT

AlexxIT commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Submission contains text or code generated by AI brand/nest Google Nest cameras and doorbells kind/bug Reports a defect or fixes one size/XS PR with 0-9 lines changed, excluding generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants