Skip to content

fix(webrtc): don't raise RingError for ICE candidates arriving before stream creation - #565

Open
PCTEJA wants to merge 2 commits into
python-ring-doorbell:masterfrom
PCTEJA:fix-webrtc-candidate-race-560
Open

PCTEJA wants to merge 2 commits into
python-ring-doorbell:masterfrom
PCTEJA:fix-webrtc-candidate-race-560

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 14, 2026

Copy link
Copy Markdown

Fixes #560.

Problem

on_webrtc_candidate raises RingError when an ICE candidate arrives before the WebRTC stream is created (or after it closed). In practice this spams Home Assistant logs with tracebacks and drops candidates during the trickle-ICE race.

Fix

  • generate_webrtc_stream now registers the stream before awaiting generate() (matching generate_async_webrtc_stream, which already did this). Early candidates are then held by the stream itself — RingWebRtcStream.on_ice_candidate waits for _offered_event — so no buffering state was added.
  • on_webrtc_candidate debug-logs and drops candidates for an unknown or closed session instead of raising, as the issue allows as an alternative to buffering.
  • Both generate methods now unregister and best-effort close the stream if generation fails or is cancelled, so no stale stream or half-open websocket is left behind.

Tests

  • New tests/test_webrtc.py: 7 tests covering the race (candidate before stream creation, candidate after close, candidate during generation, cancellation cleanup). 3 fail on base, all 7 pass with the fix.
  • Full suite: 47 passed (40 baseline + 7 new). ruff check + ruff format --check clean; mypy clean on doorbot.py.

I used an AI coding assistant for parts of the analysis and test scaffolding; I ran and reviewed the reproduction and the tests myself and reviewed the code before submitting it.

… stream creation

An ICE candidate arriving before the WebRTC stream is registered (or
after it was closed) hard-raised RingError, and callers such as Home
Assistant do not catch it, so every early candidate logged a full
'Unknown error' traceback (fixes python-ring-doorbell#560).

- generate_webrtc_stream now registers the stream before awaiting
  generate(), matching generate_async_webrtc_stream; the registration
  is removed again if generate() raises so no stale stream is left.
  Early candidates are then held by the stream until the offer is
  sent (RingWebRtcStream.on_ice_candidate waits for _offered_event).
- on_webrtc_candidate now debug-logs and drops candidates for an
  unknown or closed session instead of raising; there is no stream
  to deliver them to.

Adds regression tests covering the unknown-session, during-generate,
and after-close cases, plus the live-stream forwarding control.
The first fix popped the registration only on except Exception,
but asyncio.CancelledError inherits BaseException, so cancelling
generate_webrtc_stream mid-handshake left the abandoned stream
registered: a later candidate would find it and wait on its unset
_offered_event until on_ice_candidate timed out after 10s.

- Catch BaseException in generate_webrtc_stream (and in the
  sibling generate_async_webrtc_stream, which had the same stale
  registration on failure), pop the registration, close the stream
  so no half-open websocket or reader task is left behind
  (close() is best-effort here; the original error still propagates),
  and re-raise so cancellation keeps propagating.
- Add cancellation regression tests for both generate paths.

(Sol review of fa007bc: CHANGES_REQUESTED, 1 medium finding — this
commit addresses it.)

This branch has not been deployed

No deployments
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.

on_webrtc_candidate raises RingError when an ICE candidate arrives before the stream is created (trickle-ICE race)

1 participant