From edf7b32c62cb4b34a117163590d6adaaf4649186 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 25 Oct 2026 14:17:19 +0100 Subject: [PATCH 1/2] fix(app): one replan moves the plan rev once across sessions appPlans.Rev read the planner's plan before it took revMu. A session that read the old plan could take the lock after another session had counted the new one, count the old plan again, and leave the next read to count the new plan a third time. The plan is now read under revMu. Co-Authored-By: Claude Opus 5.5 --- .changeset/plan-rev-once.md | 5 +++++ go/cmd/ftw/app_link.go | 9 ++++++--- go/cmd/ftw/app_link_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 .changeset/plan-rev-once.md diff --git a/.changeset/plan-rev-once.md b/.changeset/plan-rev-once.md new file mode 100644 index 000000000..fcec11f31 --- /dev/null +++ b/.changeset/plan-rev-once.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +A replan now moves the plan number the app sees by one, even with two phones or a phone and a watch connected. Before, two sessions reading the plan at the same moment could move it more than once for one replan. diff --git a/go/cmd/ftw/app_link.go b/go/cmd/ftw/app_link.go index 4ac24905e..89aeba800 100644 --- a/go/cmd/ftw/app_link.go +++ b/go/cmd/ftw/app_link.go @@ -552,14 +552,17 @@ func (a *appPlans) Latest() *mpc.Plan { // redelivery of the same one. Derived from the plan's own timestamp because // mpc has no revision of its own and inventing one there would be a field two // packages had to keep in step. +// +// The plan is read under revMu. Each session calls Rev, and a session that +// read the old plan outside the lock could count it again after another had +// counted the new one, moving the rev twice for one replan. func (a *appPlans) Rev() uint64 { + a.revMu.Lock() + defer a.revMu.Unlock() plan := a.Latest() if plan == nil { return 0 } - - a.revMu.Lock() - defer a.revMu.Unlock() if plan.GeneratedAtMs != a.lastGen { a.lastGen = plan.GeneratedAtMs a.rev++ diff --git a/go/cmd/ftw/app_link_test.go b/go/cmd/ftw/app_link_test.go index cbeae1bc2..ad9ef4391 100644 --- a/go/cmd/ftw/app_link_test.go +++ b/go/cmd/ftw/app_link_test.go @@ -11,6 +11,7 @@ import ( "github.com/srcfl/ftw/go/internal/config" "github.com/srcfl/ftw/go/internal/control" "github.com/srcfl/ftw/go/internal/loadpoint" + "github.com/srcfl/ftw/go/internal/mpc" "github.com/srcfl/ftw/go/internal/state" "github.com/srcfl/ftw/go/internal/telemetry" ) @@ -469,3 +470,33 @@ func TestAnOfflineSiteMeterSendsNoPhases(t *testing.T) { t.Fatalf("an offline meter sent phases %v", got) } } + +// One replan moves the plan's rev once, however many sessions read it. A +// session that read the old plan just before another session counted the new +// one must not count the old plan again when its turn comes. +func TestPlanRevMovesOncePerReplanAcrossSessions(t *testing.T) { + svc := mpc.New(nil, nil, "SE3", mpc.Params{}) + svc.InstallPlan(mpc.Plan{GeneratedAtMs: 1_760_000_000_000}, svc.Defaults, "") + plans := &appPlans{planner: svc} + if rev := plans.Rev(); rev != 1 { + t.Fatalf("first plan: rev %d, want 1", rev) + } + + // The test holds the counter. A session that reads the plan before it + // takes the counter still has the old plan when it gets it. + plans.revMu.Lock() + slow := make(chan uint64) + go func() { slow <- plans.Rev() }() + time.Sleep(50 * time.Millisecond) // let the slow session reach the counter + svc.InstallPlan(mpc.Plan{GeneratedAtMs: 1_760_000_060_000}, svc.Defaults, "") + // Another session counts the replan, as its own Rev does under the lock. + plans.lastGen, plans.rev = 1_760_000_060_000, 2 + plans.revMu.Unlock() + + if rev := <-slow; rev != 2 { + t.Errorf("the slow session got rev %d for the replan, want 2", rev) + } + if rev := plans.Rev(); rev != 2 { + t.Errorf("the next read got rev %d with no replan since, want 2", rev) + } +} From e54e5b30625036fb51b6b50d092c9887d280b90f Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 25 Oct 2026 14:23:30 +0100 Subject: [PATCH 2/2] chore(changeset): a replan moves the plan number at most once Review note: two replans between two reads move it once in total, so "by one" per replan was not true. Co-Authored-By: Claude Opus 5.5 --- .changeset/plan-rev-once.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/plan-rev-once.md b/.changeset/plan-rev-once.md index fcec11f31..da70216a1 100644 --- a/.changeset/plan-rev-once.md +++ b/.changeset/plan-rev-once.md @@ -2,4 +2,4 @@ "ftw": patch --- -A replan now moves the plan number the app sees by one, even with two phones or a phone and a watch connected. Before, two sessions reading the plan at the same moment could move it more than once for one replan. +A replan now moves the plan number the app sees at most once, even with two phones or a phone and a watch connected. Before, two sessions reading the plan at the same moment could move it more than once for one replan.