fix(xiaomi): re-login cloud on 401 to recover without restart - #2371
Open
zcmk123 wants to merge 1 commit into
Open
fix(xiaomi): re-login cloud on 401 to recover without restart#2371zcmk123 wants to merge 1 commit into
zcmk123 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After running for a while (typically a few hours), Xiaomi cameras stop working with a
401 Unauthorizederror 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:getCloudcaches*xiaomi.Cloudinclouds[userID]and returns it forever. TheserviceTokencookie inside that cached session is short-lived and expires after a few hours.pkg/xiaomi/cloud.go:Cloud.Requestreturns the HTTP status as an error (errors.New(res.Status)) but nothing invalidates the cachedCloudon401, so every subsequent cloud call (device list,miss_get_vendor,devicepass,wakeup) keeps failing with401until the process is restarted and thecloudsmap is rebuilt from scratch.Note: the
passTokenstored in config (written by the WebUI login flow) is long-lived, so a freshLoginWithTokenalways succeeds — we just need to actually call it again.Fix
In
cloudRequest, when the cloud response contains401:Cloudfor thisuserIDfrom thecloudsmap.getCloudagain — it falls through toLoginWithTokenwith the long-livedpassTokenand gets a freshserviceToken.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
passTokenfrom WebUI.401 Unauthorizedand required restarting go2rtc.Related issues
Refs #2111, #2129.