From f9c81b655d1960d9dde52c25b69df3ea8373190a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 29 May 2026 18:05:28 +0200 Subject: [PATCH] Make background-refresh pausing reentrant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the pauseBackgroundRefreshes bool with a count. The single existing caller (subprocess suspend/resume) is unaffected, but we're about to add a second, independent reason to pause — lazygit driving a git operation that the background routines would otherwise catch mid-flight — and the two scopes can overlap. A bool can't represent "two things both want refreshes paused"; a count can. --- pkg/gui/background.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/gui/background.go b/pkg/gui/background.go index 8795b49aa..2575aedd1 100644 --- a/pkg/gui/background.go +++ b/pkg/gui/background.go @@ -3,6 +3,7 @@ package gui import ( "fmt" "runtime" + "sync/atomic" "time" "github.com/jesseduffield/lazygit/pkg/gocui" @@ -13,17 +14,27 @@ import ( type BackgroundRoutineMgr struct { gui *Gui - // if we've suspended the gui (e.g. because we've switched to a subprocess) - // we typically want to pause some things that are running like background - // file refreshes - pauseBackgroundRefreshes bool + // When this is greater than zero, the background routines (e.g. file refresh) + // skip their work. We pause them while the gui is suspended (e.g. for a + // subprocess) and while lazygit is itself driving a git operation that would + // otherwise be caught mid-flight (see the waiting-status helpers). It's a + // count rather than a bool because these pause scopes can overlap. + pauseRefreshesCount atomic.Int32 // a channel to trigger an immediate background fetch; we use this when switching repos triggerFetch chan struct{} } func (self *BackgroundRoutineMgr) PauseBackgroundRefreshes(pause bool) { - self.pauseBackgroundRefreshes = pause + if pause { + self.pauseRefreshesCount.Add(1) + } else { + self.pauseRefreshesCount.Add(-1) + } +} + +func (self *BackgroundRoutineMgr) backgroundRefreshesPaused() bool { + return self.pauseRefreshesCount.Load() > 0 } func (self *BackgroundRoutineMgr) startBackgroundRoutines() { @@ -124,7 +135,7 @@ func (self *BackgroundRoutineMgr) goEvery(interval time.Duration, stop chan stru ticker := time.NewTicker(interval) defer ticker.Stop() doit := func(retriggered bool) { - if self.pauseBackgroundRefreshes { + if self.backgroundRefreshesPaused() { return } self.gui.c.OnWorker(func(gocui.Task) error {