Skip to content

Keep the event listener alive after errors and session expiry - #559

Open
mogarber wants to merge 1 commit into
python-ring-doorbell:masterfrom
mogarber:fix/listener-resilience
Open

mogarber wants to merge 1 commit into
python-ring-doorbell:masterfrom
mogarber:fix/listener-resilience

Conversation

@mogarber

Copy link
Copy Markdown

Summary

  • Keep refreshing the Ring session every 12 hours for as long as the listener runs. Previously the refresh task stopped after its first refresh.
  • Catch exceptions while parsing a push payload, so a malformed notification is logged and skipped instead of raising into FcmPushClient.
  • Set abort_on_sequential_error_count = None in RingEventListenerConfig.default_config() so the push client keeps resetting the connection instead of permanently shutting down after 3 sequential errors.
  • Fix legacy (gcmData) ding events being reported with the push action string as their kind: action.lower == PUSH_ACTION_DING.lower() compared the bound method rather than calling it.

Why

Home Assistant users have reported for months that doorbell and motion events silently stop arriving until the integration is reloaded (home-assistant/core#157764). Tracing it shows that several unrelated failures all end in the same place: the push client stops for good.

  1. Sequential error limit. FcmPushClient calls _terminate() after 3 sequential errors of one type. Two of those types are relevant here:
  2. Session refresh runs once. _periodic_session_refresh refreshes the session and push subscription after 12 hours, then breaks and the task ends. After the first refresh, nothing re-subscribes the FCM token for the lifetime of the listener. Its sleep was also computed from the time elapsed since the last refresh (1 + now - refresh_time), not the time left until the next one.

With the error limit disabled, FcmPushClient keeps calling _reset() after connection errors, and callback errors are still logged but no longer stop the client. Malformed payloads are now caught in _on_notification, so they no longer reach the client at all.

This PR does not make the listener unkillable. FcmPushClient._reset() still terminates if it cannot reconnect after connection_retry_count attempts, and its catch-all in _listen() still terminates on decode errors such as Incorrect padding (sdb9696/firebase-messaging#40, #44). Callers still need to be able to tell that the receiver has died, which is what #555 provides. These changes are independent of #555 and #556 and touch different code paths, so they can be merged in any order.

Related to #526 and home-assistant/core#157764. Complements #555 and #556.

Tests

Added regression tests for:

  • legacy ding events being reported with kind == "ding";
  • an unparseable v2 payload (missing ding.id) being logged and skipped, with later notifications still delivered;
  • the default config not aborting on sequential errors;
  • the session refresh repeating, and retrying after a RingError.

All four new tests fail on master and pass with this change. Full suite: 44 passed. ruff check, ruff format --check and mypy pass on the changed files.

🤖 Generated with Claude Code

- Keep refreshing the ring session every 12 hours instead of only once,
  and retry if a refresh fails rather than ending the refresh task.
- Catch exceptions parsing push payloads so a malformed notification is
  logged and skipped instead of counting towards the FcmPushClient error
  limit.
- Do not let FcmPushClient permanently shut down after 3 sequential
  errors; keep resetting the connection.
- Fix legacy ding events being reported with the push action as their
  kind because str.lower was compared without being called.

Related to python-ring-doorbell#526 and home-assistant/core#157764.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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