Skip to content

Fix session leaks, replace blocking threads with async tasks, add reauth - #542

Merged
rroller merged 3 commits into
rroller:mainfrom
brianegge:fix/async-tasks-session-leaks-reauth
Mar 8, 2026
Merged

Fix session leaks, replace blocking threads with async tasks, add reauth#542
rroller merged 3 commits into
rroller:mainfrom
brianegge:fix/async-tasks-session-leaks-reauth

Conversation

@brianegge

@brianegge brianegge commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix session leaks: Close aiohttp.ClientSession in config flow credential test (finally: await session.close()) and on integration unload (await coordinator.async_stop())
  • Replace blocking threads with async tasks: Remove DahuaEventThread and DahuaVtoEventThread (and thread.py entirely), replacing them with native asyncio.Task coroutines that run on HA's event loop. VTO client now uses loop.call_later() instead of threading.Timer and a disconnected future instead of loop.stop()
  • Fix swallowed exceptions: client.stream_events() no longer silently catches CancelledError or swallows all exceptions with bare except: pass
  • Add reauthentication flow: Detects HTTP 401 during initialization and triggers HA's reauth UI so users can update credentials without removing/re-adding the integration

Test plan

  • Deploy to live HA instance with multiple Dahua cameras
  • Restart HA and verify all cameras initialize without errors
  • Verify camera streams still work in dashboard
  • Verify motion/event detection still fires dahua_event_received events
  • Test reauth flow by temporarily changing camera password

🤖 Generated with Claude Code

brianegge and others added 3 commits February 28, 2026 06:09
Copy Dahua brand icons and logos from the home-assistant/brands
repository into custom_components/dahua/brand/ so HACS validation
no longer needs to fall back to the brands repository.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add brand assets for HACS validation
… reauth

- Fix session leak in config_flow._test_credentials() by adding finally clause
- Fix session leak on unload by calling coordinator.async_stop() which closes session
- Fix != None to is not None in _close_session()
- Replace DahuaEventThread/DahuaVtoEventThread with native async tasks
- Replace threading.Timer in VTO client with loop.call_later()
- Replace loop.stop() in VTO with disconnected future for clean reconnect
- Fix swallowed exceptions in client.stream_events() (re-raise CancelledError)
- Add reauthentication flow triggered on HTTP 401 during initialization
- Delete thread.py (no longer needed)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@brianegge
brianegge force-pushed the fix/async-tasks-session-leaks-reauth branch from 5d4a5cc to 1ca36d7 Compare February 28, 2026 11:14
@rroller

rroller commented Mar 8, 2026

Copy link
Copy Markdown
Owner

Thank you!

@rroller
rroller merged commit 00fa982 into rroller:main Mar 8, 2026
3 checks passed
@basp89

basp89 commented Mar 9, 2026

Copy link
Copy Markdown

Hi Brian, rroller, thanks for updating. Unfortunately after updating to anything from 0.9.76 up, scanning an rfid tag crashes home assistant. Anything you could do about that perhaps?

@brianegge

Copy link
Copy Markdown
Contributor Author

It's related to this change? Is there a stack trace or issue open?

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.

3 participants