Skip to content

Handle "Not enough data to satisfy content length header" errors from broken cameras & NVRs #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Jul 29, 2025

Summary

  • Added error handling for response.read() to catch "Not enough data to satisfy content length header" errors
  • Fixes issues with broken Dahua and Lorex cameras that return invalid Content-Length headers
  • Refactored error handling into a reusable handle_snapshot_errors decorator

Problem

Certain Dahua and Lorex camera models return invalid Content-Length headers that are larger or smaller than the actual response body. This causes response.read() to either:

  • Hang indefinitely waiting for data that will never arrive
  • Fail with aiohttp.ClientPayloadError: Not enough data to satisfy content length header

Previously, these errors during the content read phase were not being caught, causing snapshot retrieval to fail with unhandled exceptions which could cause Home Assistant to not be able to setup these devices.

Solution

This PR fixes the issue by:

  1. Creating a new _try_read_snapshot_content method that wraps response.read() with proper error handling
  2. Implementing a handle_snapshot_errors decorator that catches:
    • TimeoutError → converts to ONVIFTimeoutError
    • aiohttp.ClientError (including ClientPayloadError) → converts to ONVIFError
  3. Applying the decorator to both:
    • _try_snapshot_uri (for connection errors)
    • _try_read_snapshot_content (for read/payload errors)

Now when these broken cameras cause payload errors or timeouts during content reading, they're properly caught and converted to appropriate ONVIF exceptions.

Related Issues

Fixes home-assistant/core#148093

Copy link

codecov bot commented Jul 29, 2025

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
onvif/zeep_aiohttp.py 50.00% 2 Missing ⚠️
Files with missing lines Coverage Δ
onvif/client.py 66.48% <100.00%> (+0.82%) ⬆️
onvif/zeep_aiohttp.py 96.84% <50.00%> (-1.01%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco merged commit e75a325 into async Jul 29, 2025
6 checks passed
@bdraco bdraco deleted the handle_error_snapshot_read branch July 29, 2025 04:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant