From 2701037efb723b9b6538c0fad68413cd176a8b1c Mon Sep 17 00:00:00 2001 From: merlinmiao <820962493@qq.com> Date: Sun, 5 Apr 2026 20:06:03 +0800 Subject: [PATCH] fix(web): re-read pidFile before validating pico token When the gateway process crashes and restarts, the backend caches the old pidData (PID, token). If the WebSocket proxy handler uses stale pidData, picoComposedToken returns an empty string causing "Invalid Pico token" errors. ReadPidFileWithCheck validates that the process is still alive and returns fresh data if the gateway restarted, so always re-read the pidFile before token validation. Co-Authored-By: Claude Opus 4.6 --- web/backend/api/pico.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/web/backend/api/pico.go b/web/backend/api/pico.go index c8ef47308..ef2955124 100644 --- a/web/backend/api/pico.go +++ b/web/backend/api/pico.go @@ -11,6 +11,7 @@ import ( "github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/logger" + ppid "github.com/sipeed/picoclaw/pkg/pid" ) // registerPicoRoutes binds Pico Channel management endpoints to the ServeMux. @@ -56,6 +57,12 @@ func (h *Handler) createWsProxy(origProtocol string, token string) *httputil.Rev func (h *Handler) handleWebSocketProxy() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { gateway.mu.Lock() + // Re-read pidFile to detect if gateway restarted with new credentials. + // ReadPidFileWithCheck also validates the process is still alive. + if newPidData := ppid.ReadPidFileWithCheck(globalConfigDir()); newPidData != nil { + gateway.pidData = newPidData + refreshPicoTokensLocked(h.configPath) + } ensurePicoTokenCachedLocked(h.configPath) gatewayAvailable := gateway.pidData != nil gateway.mu.Unlock()