Skip to content

Commit d2995aa

Browse files
committed
Make background-refresh pausing reentrant
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.
1 parent 68f3bcf commit d2995aa

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

pkg/gui/background.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gui
33
import (
44
"fmt"
55
"runtime"
6+
"sync/atomic"
67
"time"
78

89
"github.com/jesseduffield/lazygit/pkg/gocui"
@@ -13,17 +14,27 @@ import (
1314
type BackgroundRoutineMgr struct {
1415
gui *Gui
1516

16-
// if we've suspended the gui (e.g. because we've switched to a subprocess)
17-
// we typically want to pause some things that are running like background
18-
// file refreshes
19-
pauseBackgroundRefreshes bool
17+
// When this is greater than zero, the background routines (e.g. file refresh)
18+
// skip their work. We pause them while the gui is suspended (e.g. for a
19+
// subprocess) and while lazygit is itself driving a git operation that would
20+
// otherwise be caught mid-flight (see the waiting-status helpers). It's a
21+
// count rather than a bool because these pause scopes can overlap.
22+
pauseRefreshesCount atomic.Int32
2023

2124
// a channel to trigger an immediate background fetch; we use this when switching repos
2225
triggerFetch chan struct{}
2326
}
2427

2528
func (self *BackgroundRoutineMgr) PauseBackgroundRefreshes(pause bool) {
26-
self.pauseBackgroundRefreshes = pause
29+
if pause {
30+
self.pauseRefreshesCount.Add(1)
31+
} else {
32+
self.pauseRefreshesCount.Add(-1)
33+
}
34+
}
35+
36+
func (self *BackgroundRoutineMgr) backgroundRefreshesPaused() bool {
37+
return self.pauseRefreshesCount.Load() > 0
2738
}
2839

2940
func (self *BackgroundRoutineMgr) startBackgroundRoutines() {
@@ -124,7 +135,7 @@ func (self *BackgroundRoutineMgr) goEvery(interval time.Duration, stop chan stru
124135
ticker := time.NewTicker(interval)
125136
defer ticker.Stop()
126137
doit := func(retriggered bool) {
127-
if self.pauseBackgroundRefreshes {
138+
if self.backgroundRefreshesPaused() {
128139
return
129140
}
130141
self.gui.c.OnWorker(func(gocui.Task) error {

0 commit comments

Comments
 (0)