Skip to content

fix: suppress ClientConnectionError in frames() generator teardown#4

Open
livepeer-tessa wants to merge 1 commit intomainfrom
fix/media-output-generator-exit-connection-error
Open

fix: suppress ClientConnectionError in frames() generator teardown#4
livepeer-tessa wants to merge 1 commit intomainfrom
fix/media-output-generator-exit-connection-error

Conversation

@livepeer-tessa
Copy link
Copy Markdown

Summary

Fixes daydreamlive/scope#897 — asyncio logs Task exception was never retrieved on every session close where media output is active.

Root Cause

In MediaOutput.frames(), the async generator's finally block cancels and awaits producer_task. During teardown, that task may raise aiohttp.ClientConnectionError('Connection closed') because the underlying connection was already dropped when the client disconnected. This error was not suppressed, causing asyncio to log it as an unhandled task exception.

Fix

Add aiohttp.ClientConnectionError to the suppress() context in the finally block, alongside the existing asyncio.CancelledError:

# before
with suppress(asyncio.CancelledError):
    await producer_task

# after
with suppress(asyncio.CancelledError, aiohttp.ClientConnectionError):
    await producer_task

Both errors are expected during normal session teardown — the connection being closed is the reason we're tearing down in the first place.

Impact

  • Eliminates log noise (asyncio - ERROR - Task exception was never retrieved) on every clean session close
  • No functional change — the session is already terminating when this fires
  • aiohttp is already a declared dependency (>=3.9.0), so no new deps added

Testing

The error occurs on every session close with active media output. After this fix, the log line should no longer appear in Grafana/Loki for normal session teardowns (only genuine errors should remain).

During session close, the async generator's finally block awaits the
producer task which may raise aiohttp.ClientConnectionError when the
connection was already dropped. This is expected during teardown but
was not suppressed, causing asyncio to log 'Task exception was never
retrieved' noise on every clean session close.

Add aiohttp.ClientConnectionError to the suppress() context alongside
the existing asyncio.CancelledError so teardown-time connection drops
are silently discarded.

Fixes daydreamlive/scope#897

Signed-off-by: livepeer-tessa <livepeer-tessa@users.noreply.github.com>
@livepeer-tessa livepeer-tessa requested a review from mjh1 April 10, 2026 06:24
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