Skip to content

Fix "Task cannot await on itself" when close() runs inside the reader task#534

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

Fix "Task cannot await on itself" when close() runs inside the reader task#534
Nielsh82 wants to merge 1 commit into
python-ring-doorbell:masterfrom
Nielsh82:fix-webrtc-self-await

Conversation

@Nielsh82

@Nielsh82 Nielsh82 commented Jul 4, 2026

Copy link
Copy Markdown

Description

When the Ring server ends a WebRTC session, handle_message() (running inside the websocket reader task) ends up calling close()_close(), which does:

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

Since this code is already executing inside read_task, it raises:

RuntimeError: Task cannot await on itself: <Task pending name='Task-...' coro=<RingWebRtcStream.reader() running at .../ring_doorbell/webrtcstream.py:247>>

Observed in the wild through the Home Assistant ring integration (ring-doorbell 0.9.14, HA 2026.7.x): every live-view session teardown logs this error, and live views break.

Fix

Guard the await with read_task is not asyncio.current_task(). When _close() is invoked from the reader task itself, awaiting is unnecessary: the websocket has just been closed a few lines above, so the async for loop terminates and the task completes naturally once handle_message() returns.

Verified on a live Home Assistant install (HA 2026.7.0, three Ring cameras): with this one-line guard applied, repeated live-view start/stop cycles produce zero RuntimeErrors where previously every teardown logged one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01T7LmwdKV7sFZkcYt6TTMZt

When the Ring server ends a WebRTC session, handle_message() calls
close() from within the reader task; _close() then awaits read_task,
i.e. the task it is running in, raising
"RuntimeError: Task cannot await on itself".

Skip awaiting read_task in that case - the websocket has just been
closed, so the reader loop exits naturally once handle_message returns.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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