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 <noreply@anthropic.com>
This commit is contained in:
merlinmiao 2026-04-05 20:06:03 +08:00
parent 1ad94ffb34
commit 2701037efb

View file

@ -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()