Skip to content

fix(xiaomi): re-login cloud on 401 to recover without restart - #2371

Open
zcmk123 wants to merge 1 commit into
AlexxIT:masterfrom
zcmk123:fix/xiaomi-cloud-401-relogin
Open

fix(xiaomi): re-login cloud on 401 to recover without restart#2371
zcmk123 wants to merge 1 commit into
AlexxIT:masterfrom
zcmk123:fix/xiaomi-cloud-401-relogin

Conversation

@zcmk123

@zcmk123 zcmk123 commented Jul 20, 2026

Copy link
Copy Markdown

Problem

After running for a while (typically a few hours), Xiaomi cameras stop working with a 401 Unauthorized error from the Xiaomi cloud API, and the only way to recover is to restart the entire go2rtc process.

Reported in #2111, #2129 (closed as duplicate of #2111) and several other issues (#2187, #2234, #2237, #2273).

Root cause

internal/xiaomi/xiaomi.go:getCloud caches *xiaomi.Cloud in clouds[userID] and returns it forever. The serviceToken cookie inside that cached session is short-lived and expires after a few hours.

pkg/xiaomi/cloud.go:Cloud.Request returns the HTTP status as an error (errors.New(res.Status)) but nothing invalidates the cached Cloud on 401, so every subsequent cloud call (device list, miss_get_vendor, devicepass, wakeup) keeps failing with 401 until the process is restarted and the clouds map is rebuilt from scratch.

Note: the passToken stored in config (written by the WebUI login flow) is long-lived, so a fresh LoginWithToken always succeeds — we just need to actually call it again.

Fix

In cloudRequest, when the cloud response contains 401:

  1. Drop the cached Cloud for this userID from the clouds map.
  2. Call getCloud again — it falls through to LoginWithToken with the long-lived passToken and gets a fresh serviceToken.
  3. Retry the original request once.

This makes the camera recover automatically without a process restart. The change is minimal and limited to a single helper that all cloud requests funnel through, so the behavior change is consistent across device list / miss_get_vendor / devicepass / wakeup.

Test

  • Xiaomi camera configured with passToken from WebUI.
  • Before the patch: after some uptime, opening the stream returned 401 Unauthorized and required restarting go2rtc.
  • After the patch: same scenario, the first stream open after token expiry triggers one extra re-login and then succeeds transparently.

Related issues

Refs #2111, #2129.

The cached Cloud in clouds[userID] is never invalidated, so once the
Xiaomi serviceToken expires (typically after a few hours) all subsequent
cloud requests keep returning 401 Unauthorized until go2rtc is restarted.

Detect 401 in cloudRequest, drop the cached Cloud and re-login via the
long-lived passToken from config, then retry the request once. This makes
the camera recover automatically without a process restart.

Refs: AlexxIT#2111, AlexxIT#2129
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