From ab20c16982b1fa4cc2d84c3f88db054d9b21793b Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 9 Jan 2025 21:47:10 +0100 Subject: [PATCH] fix(api): don't crash requests after failing to unmarshal tokens (fixes #9909) (#9912) Unclear why this would happen, but apparently it does? --- lib/api/api.go | 3 +++ lib/api/tokenmanager.go | 11 ++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/api/api.go b/lib/api/api.go index 6205ae620..f09df33f3 100644 --- a/lib/api/api.go +++ b/lib/api/api.go @@ -402,6 +402,9 @@ func (s *service) Serve(ctx context.Context) error { // care about we log ourselves from the handlers. ErrorLog: log.New(io.Discard, "", 0), } + if shouldDebugHTTP() { + srv.ErrorLog = log.Default() + } l.Infoln("GUI and API listening on", listener.Addr()) l.Infoln("Access the GUI via the following URL:", guiCfg.URL()) diff --git a/lib/api/tokenmanager.go b/lib/api/tokenmanager.go index 96dfab4a7..38cd24415 100644 --- a/lib/api/tokenmanager.go +++ b/lib/api/tokenmanager.go @@ -36,11 +36,12 @@ type tokenManager struct { } func newTokenManager(key string, miscDB *db.NamespacedKV, lifetime time.Duration, maxItems int) *tokenManager { - tokens := &apiproto.TokenSet{ - Tokens: make(map[string]int64), - } + var tokens apiproto.TokenSet if bs, ok, _ := miscDB.Bytes(key); ok { - _ = proto.Unmarshal(bs, tokens) // best effort + _ = proto.Unmarshal(bs, &tokens) // best effort + } + if tokens.Tokens == nil { + tokens.Tokens = make(map[string]int64) } return &tokenManager{ key: key, @@ -49,7 +50,7 @@ func newTokenManager(key string, miscDB *db.NamespacedKV, lifetime time.Duration maxItems: maxItems, timeNow: time.Now, mut: sync.NewMutex(), - tokens: tokens, + tokens: &tokens, } }