From 35928013e2af71e87961492ac633b094cb0c8ee4 Mon Sep 17 00:00:00 2001 From: Hagar Meir Date: Tue, 1 Dec 2026 16:36:12 +0200 Subject: [PATCH] Don't move to a left view on a sync A sync that collects the state of a view the node already left no longer stops the current view, writes a new view record for it, or moves to it, as changeView already refuses to start a left view. Written by Claude for @HagarMeir. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Hagar Meir --- internal/bft/controller.go | 15 ++++++++++++--- internal/bft/controller_sync_test.go | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/internal/bft/controller.go b/internal/bft/controller.go index a791d3f7..2552867b 100644 --- a/internal/bft/controller.go +++ b/internal/bft/controller.go @@ -983,14 +983,23 @@ func (c *Controller) sync() (viewNum uint64, seq uint64, decisions uint64, carri // A sync that brings a reconfiguration closed the controller, which restarts in the node's view (see run), so the // sync doesn't save a new view. saveNewView := !syncResponse.Reconfig.InReplicatedDecisions && response != nil && response.View > newViewNum && response.Seq == latestDecisionSeq+1 + if saveNewView { + c.viewTransitionLock.Lock() + // Like changeView, sync doesn't move to a view the node left (see minStartView), whose record would also hide + // the view change record from a restart. + if response.View < c.minStartView { + c.viewTransitionLock.Unlock() + c.Logger.Infof("Node %d collected state with view %d, which it left, so it doesn't move to it", c.ID, response.View) + saveNewView = false + } + } if saveNewView { c.Logger.Infof("Node %d collected state with view %d and sequence %d", c.ID, response.View, response.Seq) // The record of the new view replaces the state of the current view, so the // view has to be stopped first, and no other view may start until the record - // is written. Otherwise a view could append a commit after that record and - // leave a sequence that recovery rejects. - c.viewTransitionLock.Lock() + // is written (viewTransitionLock is held). Otherwise a view could append a + // commit after that record and leave a sequence that recovery rejects. c.abortCurrentView() // The fate of the in-flight proposal has to be decided before the record of the diff --git a/internal/bft/controller_sync_test.go b/internal/bft/controller_sync_test.go index 979a89b1..768652b6 100644 --- a/internal/bft/controller_sync_test.go +++ b/internal/bft/controller_sync_test.go @@ -492,6 +492,25 @@ func TestSyncAbortsViewBeforeSavingNewView(t *testing.T) { require.Equal(t, []bool{false}, fixture.state.TruncateTo) } +// TestSyncDoesNotMoveToALeftView makes sure that a sync that collects the state of a view the node left, since it sent +// its view data for a later one, neither stops the current view nor saves a record of the left view. +func TestSyncDoesNotMoveToALeftView(t *testing.T) { + // the node is in view 5 after sequence 10, and the others are in view 6 at sequence 11 + fixture := newSyncFixture(t, 10, 5, 10, 5) + running := &View{Number: 5, abortChan: make(chan struct{})} + fixture.controller.currView = running + fixture.controller.LeaveViewsBelow(7) // the node left views 5 and 6, and sent its view data for view 7 + + view, seq, _, carried := fixture.controller.sync() + require.Equal(t, uint64(5), view, "the sync moved the node to a view it left") + require.Equal(t, uint64(11), seq) + require.Nil(t, carried) + require.False(t, running.Stopped(), "the sync stopped the current view") + for _, saved := range fixture.state.SavedMessages { + require.Nil(t, saved.GetNewView(), "the sync saved a record of a view the node left") + } +} + // TestSyncHoldsViewTransitionLockUntilTheNewViewIsSaved covers the read side // of the record ordering: while sync() is between the abort and the new view // record, changeView must not start another view.