Skip to content

Guard RingWebRtcStream._close() against awaiting its own reader task - #548

Open
Nielsh82 wants to merge 1 commit into
python-ring-doorbell:masterfrom
Nielsh82:fix/webrtc-close-self-await
Open

Nielsh82 wants to merge 1 commit into
python-ring-doorbell:masterfrom
Nielsh82:fix/webrtc-close-self-await

Conversation

@Nielsh82

Copy link
Copy Markdown

Problem

When the Ring backend ends a WebRTC session server-side, handle_message() processes the session-close and ends up calling _close() from within the reader task itself. _close() then hits:

if read_task := self.read_task:
    self.read_task = None
    if not read_task.done():
        await read_task

Since the currently-running task is read_task, this raises:

RuntimeError: Task cannot await on itself: <Task pending name='Task-…' coro=<RingWebRtcStream.reader() …>>

The exception propagates out of the teardown path, and the stream bookkeeping is left in a state where every subsequent live-view attempt for that camera fails until the client is fully reinitialized.

Observed continuously via Home Assistant's ring integration (which pins this library; reproduced on 0.9.14 with HA 2026.7.x and 2026.8.x): the first server-side session end kills all Ring live views until the integration reloads. We've been running the one-line guard below as a local patch since early July with no recurrence.

Fix

Skip awaiting the reader task when _close() is executing on that task — completion is inherent (the task is ending), and cancellation semantics for the external-caller path are unchanged.

Repro sketch

  1. Start a WebRTC stream (e.g. HA Ring camera live view).
  2. Let the session run until Ring's backend ends it (session timeout), or end it from the Ring side.
  3. Without the guard: RuntimeError: Task cannot await on itself in the close path; subsequent generate_async_webrtc_stream() calls for the device fail until client re-init.

When the Ring backend ends a WebRTC session, handle_message() receives the
session-close and calls _close() from within the reader task itself. The
unconditional 'await read_task' then raises
RuntimeError('Task cannot await on itself'), which propagates out of the
teardown path and leaves the stream registry in a state where every
subsequent live-view attempt fails until the client is fully reinitialized
(observed via Home Assistant's ring integration on 0.9.14).

Skip awaiting the reader task when _close() is executing on that very task;
cancellation/completion is already handled by the task ending itself.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
AMoo-Miki added a commit to AMoo-Miki/python-ring-cameras that referenced this pull request Oct 9, 2026
Upstream python-ring-doorbell has had no merges for months; this takes
what its open pull requests offer, adapted to this fork:

- WebRTC: _close() no longer awaits its own reader task when the stream
  closes from inside it (upstream python-ring-doorbell#548, python-ring-doorbell#534).
- New RingDoorBell.async_take_snapshot(): one request that returns a
  stored snapshot newer than max_age or captures a fresh one, via the
  endpoint ring-mqtt uses (upstream python-ring-doorbell#528). Existing snapshot methods
  are unchanged.
- Outdoor Cam Plus reports a battery from its health data when it says
  either way, so plug-in variants no longer get one; a camera whose
  battery died reports nothing and keeps it (upstream python-ring-doorbell#538, reworked).
- Device kinds: Battery Doorbell Pro 4K, Wired Doorbell Plus (2nd Gen),
  Wired Doorbell (doorbell_tahoe), Doorbell (2nd Gen) cocoa_doorbell_v3,
  Indoor Cam Plus, Spotlight Cam Pro 4K, Elite Cam 140/360 4K and
  Chime (2nd Gen) (upstream python-ring-doorbell#553, python-ring-doorbell#539, python-ring-doorbell#536, python-ring-doorbell#531, python-ring-doorbell#550).

Co-authored-by: Niels Haagsman <haagsman@gmail.com>
Co-authored-by: Adam Reeve <adam@adamreeve.com>
Co-authored-by: danielcolquitt <daniel@colquitt.xyz>
Co-authored-by: Tom Roberts <tom@tomroberts.co.uk>
Co-authored-by: ben79 <benjaminfearnley@gmail.com>
Co-authored-by: Aaron Wolen <aaron@wolen.com>
Co-authored-by: Yoan <yoan.cousin@proton.me>
AMoo-Miki added a commit to AMoo-Miki/python-ring-cameras that referenced this pull request Oct 9, 2026
Upstream python-ring-doorbell has had no merges for months; this takes what its open pull requests offer, adapted to this fork:

- WebRTC: _close() no longer awaits its own reader task when the stream closes from inside it (upstream python-ring-doorbell#548, python-ring-doorbell#534).
- New RingDoorBell.async_take_snapshot(): one request that returns a stored snapshot newer than max_age or captures a fresh one, via the endpoint ring-mqtt uses (upstream python-ring-doorbell#528). Existing snapshot methods are unchanged.
- Outdoor Cam Plus reports a battery from its health data when it says either way, so plug-in variants no longer get one; a camera whose battery died reports nothing and keeps it (upstream python-ring-doorbell#538, reworked).
- Device kinds: Battery Doorbell Pro 4K, Wired Doorbell Plus (2nd Gen), Wired Doorbell (doorbell_tahoe), Doorbell (2nd Gen) cocoa_doorbell_v3, Indoor Cam Plus, Spotlight Cam Pro 4K, Elite Cam 140/360 4K and Chime (2nd Gen) (upstream python-ring-doorbell#553, python-ring-doorbell#539, python-ring-doorbell#536, python-ring-doorbell#531, python-ring-doorbell#550).

Co-authored-by: Niels Haagsman <haagsman@gmail.com>
Co-authored-by: Adam Reeve <adam@adamreeve.com>
Co-authored-by: danielcolquitt <daniel@colquitt.xyz>
Co-authored-by: Tom Roberts <tom@tomroberts.co.uk>
Co-authored-by: ben79 <benjaminfearnley@gmail.com>
Co-authored-by: Aaron Wolen <aaron@wolen.com>
Co-authored-by: Yoan <yoan.cousin@proton.me>
AMoo-Miki added a commit to AMoo-Miki/python-ring-cameras that referenced this pull request Oct 16, 2026
Upstream python-ring-doorbell has had no merges for months; this takes what its open pull requests offer, adapted to this fork:

- WebRTC: _close() no longer awaits its own reader task when the stream closes from inside it (upstream python-ring-doorbell#548, python-ring-doorbell#534).
- New RingDoorBell.async_take_snapshot(): one request that returns a stored snapshot newer than max_age or captures a fresh one, via the endpoint ring-mqtt uses (upstream python-ring-doorbell#528). Existing snapshot methods are unchanged.
- Outdoor Cam Plus reports a battery from its health data when it says either way, so plug-in variants no longer get one; a camera whose battery died reports nothing and keeps it (upstream python-ring-doorbell#538, reworked).
- Device kinds: Battery Doorbell Pro 4K, Wired Doorbell Plus (2nd Gen), Wired Doorbell (doorbell_tahoe), Doorbell (2nd Gen) cocoa_doorbell_v3, Indoor Cam Plus, Spotlight Cam Pro 4K, Elite Cam 140/360 4K and Chime (2nd Gen) (upstream python-ring-doorbell#553, python-ring-doorbell#539, python-ring-doorbell#536, python-ring-doorbell#531, python-ring-doorbell#550).

Co-authored-by: Niels Haagsman <haagsman@gmail.com>
Co-authored-by: Adam Reeve <adam@adamreeve.com>
Co-authored-by: danielcolquitt <daniel@colquitt.xyz>
Co-authored-by: Tom Roberts <tom@tomroberts.co.uk>
Co-authored-by: ben79 <benjaminfearnley@gmail.com>
Co-authored-by: Aaron Wolen <aaron@wolen.com>
Co-authored-by: Yoan <yoan.cousin@proton.me>
AMoo-Miki added a commit to AMoo-Miki/python-ring-cameras that referenced this pull request Oct 16, 2026
Upstream python-ring-doorbell has had no merges for months; this takes what its open pull requests offer, adapted to this fork:

- WebRTC: _close() no longer awaits its own reader task when the stream closes from inside it (upstream python-ring-doorbell#548, python-ring-doorbell#534).
- New RingDoorBell.async_take_snapshot(): one request that returns a stored snapshot newer than max_age or captures a fresh one, via the endpoint ring-mqtt uses (upstream python-ring-doorbell#528). Existing snapshot methods are unchanged.
- Outdoor Cam Plus reports a battery from its health data when it says either way, so plug-in variants no longer get one; a camera whose battery died reports nothing and keeps it (upstream python-ring-doorbell#538, reworked).
- Device kinds: Battery Doorbell Pro 4K, Wired Doorbell Plus (2nd Gen), Wired Doorbell (doorbell_tahoe), Doorbell (2nd Gen) cocoa_doorbell_v3, Indoor Cam Plus, Spotlight Cam Pro 4K, Elite Cam 140/360 4K and Chime (2nd Gen) (upstream python-ring-doorbell#553, python-ring-doorbell#539, python-ring-doorbell#536, python-ring-doorbell#531, python-ring-doorbell#550).

Co-authored-by: Niels Haagsman <haagsman@gmail.com>
Co-authored-by: Adam Reeve <adam@adamreeve.com>
Co-authored-by: danielcolquitt <daniel@colquitt.xyz>
Co-authored-by: Tom Roberts <tom@tomroberts.co.uk>
Co-authored-by: ben79 <benjaminfearnley@gmail.com>
Co-authored-by: Aaron Wolen <aaron@wolen.com>
Co-authored-by: Yoan <yoan.cousin@proton.me>
AMoo-Miki added a commit to AMoo-Miki/python-ring-cameras that referenced this pull request Oct 16, 2026
Upstream python-ring-doorbell has had no merges for months; this takes what its open pull requests offer, adapted to this fork:

- WebRTC: _close() no longer awaits its own reader task when the stream closes from inside it (upstream python-ring-doorbell#548, python-ring-doorbell#534).
- New RingDoorBell.async_take_snapshot(): one request that returns a stored snapshot newer than max_age or captures a fresh one, via the endpoint ring-mqtt uses (upstream python-ring-doorbell#528). Existing snapshot methods are unchanged.
- Outdoor Cam Plus reports a battery from its health data when it says either way, so plug-in variants no longer get one; a camera whose battery died reports nothing and keeps it (upstream python-ring-doorbell#538, reworked).
- Device kinds: Battery Doorbell Pro 4K, Wired Doorbell Plus (2nd Gen), Wired Doorbell (doorbell_tahoe), Doorbell (2nd Gen) cocoa_doorbell_v3, Indoor Cam Plus, Spotlight Cam Pro 4K, Elite Cam 140/360 4K and Chime (2nd Gen) (upstream python-ring-doorbell#553, python-ring-doorbell#539, python-ring-doorbell#536, python-ring-doorbell#531, python-ring-doorbell#550).

Co-authored-by: Niels Haagsman <haagsman@gmail.com>
Co-authored-by: Adam Reeve <adam@adamreeve.com>
Co-authored-by: danielcolquitt <daniel@colquitt.xyz>
Co-authored-by: Tom Roberts <tom@tomroberts.co.uk>
Co-authored-by: ben79 <benjaminfearnley@gmail.com>
Co-authored-by: Aaron Wolen <aaron@wolen.com>
Co-authored-by: Yoan <yoan.cousin@proton.me>

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.

1 participant