Skip to content

Fix InvalidSessionException treated as permanent auth failure - #520

Merged
iMicknl merged 1 commit into
iMicknl:mainfrom
arpena:fix/invalid-session-exception-hierarchy
Aug 2, 2026
Merged

Fix InvalidSessionException treated as permanent auth failure#520
iMicknl merged 1 commit into
iMicknl:mainfrom
arpena:fix/invalid-session-exception-hierarchy

Conversation

@arpena

@arpena arpena commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Summary

InvalidSessionException inherits from UnauthorizedException, which causes consumers (like the ha-sagemcom-fast integration) to treat transient session errors as permanent authorization failures.

Problem

When the router invalidates a session (e.g., due to a concurrent login from the web interface), the API raises InvalidSessionException. Because it inherits from UnauthorizedException, consumers that catch UnauthorizedException to detect permanent auth failures (like Home Assistant raising ConfigEntryAuthFailed) incorrectly treat this transient condition as "bad credentials", permanently disabling the integration.

Additionally, max_tries=1 on the backoff decorators means the on_backoff=retry_login handler fires but no actual retry occurs — the function is only called once.

Fix

  1. Change exception hierarchy: InvalidSessionException now inherits from BaseSagemcomException instead of UnauthorizedException. This is a transient error, not a permanent auth failure.

  2. Increase max_tries to 2: With max_tries=2, when an InvalidSessionException occurs, the backoff handler calls retry_login() and then retries the operation once before giving up.

Breaking Change Note

This is a minor breaking change for consumers that catch UnauthorizedException and expect InvalidSessionException to be included. Consumers should update to catch InvalidSessionException separately if they need to handle it. The ha-sagemcom-fast integration already has a PR to handle this.

Reproduction

  1. Log in to the router web interface during an API poll cycle
  2. The concurrent login invalidates the library's session
  3. get_hosts() raises InvalidSessionException
  4. Consumer catches it as UnauthorizedException → permanent failure

After this fix, the library retries the login and operation automatically.

Two changes:

1. Change InvalidSessionException to inherit from BaseSagemcomException
   instead of UnauthorizedException. An invalid session is a transient
   error (e.g., router invalidated the session due to a concurrent login),
   not a permanent authorization failure. Consumers that catch
   UnauthorizedException to signal permanent auth failures (like
   Home Assistant's ConfigEntryAuthFailed) should not catch transient
   session errors.

2. Increase max_tries from 1 to 2 on all backoff-decorated API methods.
   With max_tries=1, the on_backoff=retry_login handler fires but no
   retry actually occurs. With max_tries=2, after an InvalidSessionException
   the client will re-login and retry the operation once before giving up.
@iMicknl
iMicknl merged commit 8c83f08 into iMicknl:main Aug 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants