From def534e0a6d1342c3a349d58da33f13fdfab0070 Mon Sep 17 00:00:00 2001 From: oliver Date: Tue, 12 May 2026 09:37:07 +0800 Subject: [PATCH] Fix UME token store sync: load token when expires_at is 0, clear stale memory Previously exp>mem_exp blocked loading when both were 0 (restart + DB row with missing expiry). Also clear in-memory token when DB row is cleared so token/status matches store after disconnect. Co-authored-by: Cursor --- netx_api/ume_client.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/netx_api/ume_client.py b/netx_api/ume_client.py index b5210e0..c5d20be 100644 --- a/netx_api/ume_client.py +++ b/netx_api/ume_client.py @@ -100,16 +100,42 @@ class UMEClient: except Exception: return if not loaded: + if self._token_value.strip(): + self._token_value = "" + self._token_expires_at = 0.0 + self._last_token_source = "memory" + self._last_store_sync_changed = True return token, exp = loaded token = str(token or "").strip() if not token: + if self._token_value.strip(): + self._token_value = "" + self._token_expires_at = 0.0 + self._last_token_source = "memory" + self._last_store_sync_changed = True return - if exp > float(self._token_expires_at): + exp_f = float(exp) + mem_exp = float(self._token_expires_at) + mem_tok = self._token_value.strip() + # Adopt DB when we have no local token, DB has newer expiry, or token string changed. + # If DB expiry is missing (0) but we already have the same token with a positive local expiry, + # keep local expiry (avoids token_status showing disconnected after restart when DB row is incomplete). + adopt = False + if not mem_tok: + adopt = True + elif token != mem_tok: + adopt = True + elif exp_f > mem_exp: + adopt = True + elif exp_f <= 0 and not (token == mem_tok and mem_exp > 0): + adopt = True + if adopt: + if token != mem_tok or exp_f != mem_exp: + self._last_store_sync_changed = True self._token_value = token - self._token_expires_at = float(exp) + self._token_expires_at = exp_f self._last_token_source = "db" - self._last_store_sync_changed = True def _persist_token_to_store(self) -> None: if self._token_saver is None: