Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

Keep a manual catalog sync alive past its caller and recover sync panics (#218)

Carter9d678665293d
diff --git a/backend/stores/exercise.go b/backend/stores/exercise.go
index 339c0d8..dcb845d 100644
--- a/backend/stores/exercise.go
+++ b/backend/stores/exercise.go
@@ -616,16 +616,18 @@ func (s *ExerciseStore) CacheStatus() (models.ExerciseCacheStatus, error) {
// RefreshCached is the scheduled catalog pass run on demand: it imports every upstream
// exercise in English and re-reads every held one in each other language. The int is
-// the number of exercises the English pass recorded.
+// the number of exercises the English pass recorded. The pass outlives ctx: a caller that
+// gives up (a proxy timing out at 60s) must not abort a half-written import and record a
+// false "unreachable".
func (s *ExerciseStore) RefreshCached(ctx context.Context) (int, error) {
- return s.syncCatalog(ctx)
+ return s.syncCatalog(context.WithoutCancel(ctx))
}
// RefreshCachedConfirmed is RefreshCached for an operator who has checked that the upstream
// catalog really did lose most of its exercises: this one pass accepts a shrunken export
// instead of failing with ErrCatalogShrank. An empty export is still refused.
func (s *ExerciseStore) RefreshCachedConfirmed(ctx context.Context) (int, error) {
- return s.syncCatalogWith(ctx, true)
+ return s.syncCatalogWith(context.WithoutCancel(ctx), true)
}
// refresh brings one language up to date. English imports every exercise in the bulk
diff --git a/backend/stores/exercise_locale_sync.go b/backend/stores/exercise_locale_sync.go
index 51cdcc5..bd96c44 100644
--- a/backend/stores/exercise_locale_sync.go
+++ b/backend/stores/exercise_locale_sync.go
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log"
+ "runtime/debug"
"sort"
"time"
)
@@ -112,6 +113,14 @@ func (s *ExerciseStore) syncCatalogWith(ctx context.Context, acceptShrink bool)
// Deferred after the flag's release, so it runs first: a reader never sees the pass
// finished with the old outcome.
defer func() { s.recordPass(started, err) }()
+ // Runs before recordPass, so a panic is recorded as the failed pass it was, and the
+ // scheduler goroutine survives to retry instead of taking the whole server down.
+ defer func() {
+ if r := recover(); r != nil {
+ err = fmt.Errorf("catalog sync panicked: %v", r)
+ log.Printf("exercises: %v\n%s", err, debug.Stack())
+ }
+ }()
dctx, cancel := context.WithTimeout(ctx, localeSyncTimeout)
codes, err := s.catalog.Revalidating().Locales(dctx)
cancel()
diff --git a/backend/stores/exercise_locale_sync_test.go b/backend/stores/exercise_locale_sync_test.go
index 0405f6f..d405f07 100644
--- a/backend/stores/exercise_locale_sync_test.go
+++ b/backend/stores/exercise_locale_sync_test.go
@@ -873,3 +873,39 @@ func TestRunCatalogSync_SchedulesNextAttempt(t *testing.T) {
}
})
}
+
+func TestRefreshCached_SurvivesACancelledCaller(t *testing.T) {
+ s, cat := syncStore(t, 3, "en", "es")
+ ctx, cancel := context.WithCancel(t.Context())
+ cancel()
+
+ n, err := s.RefreshCached(ctx)
+ if err != nil || n != 3 {
+ t.Fatalf("RefreshCached(cancelled ctx) = %d, %v; want 3, nil", n, err)
+ }
+ if cat.listCount("es") == 0 {
+ t.Error("translations were not refreshed after the caller gave up")
+ }
+ if st, _ := s.CacheStatus(); st.LastFailure != nil {
+ t.Errorf("a cancelled caller was recorded as a failure: %v", *st.LastFailure)
+ }
+}
+
+func TestSyncCatalog_PanicIsAFailedPassNotACrash(t *testing.T) {
+ s, _ := syncStore(t, 2, "en", "es")
+ real := s.db
+ s.db = nil // purgeDroppedLocales dereferences it
+
+ _, err := s.syncCatalog(t.Context())
+ s.db = real
+ if err == nil || !strings.Contains(err.Error(), "panicked") {
+ t.Fatalf("err = %v, want a recovered panic", err)
+ }
+ st, _ := s.CacheStatus()
+ if st.LastFailure == nil || st.LastSyncedAt != nil {
+ t.Errorf("status = %+v, want a recorded failure and no clean sync", st)
+ }
+ if st.Syncing {
+ t.Error("the single-flight flag stayed set after a panic")
+ }
+}