Skip to content

Commit 9881628

Browse files
Don't try killing processes if we already know the command finished + reduce error logging noise on Windows (#4231)
- **PR Description** I was tinkering around with the code and then checking associated logs but even with LOG_LEVEL=error, I found there was a lot of noise on Windows. This PR fixes two such sources: 1. Navigating through files in the Files panel 1. Navigating through branches in the Branches panel when there are a lot of commits (e.g. this repo) More details in the comments below. - **Please check if the PR fulfills these requirements** * [ ] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [ ] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [ ] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [ ] If a new UserConfig entry was added, make sure it can be hot-reloaded (see [here](https://github.com/jesseduffield/lazygit/blob/master/docs/dev/Codebase_Guide.md#using-userconfig)) * [ ] Docs have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc <!-- Be sure to name your PR with an imperative e.g. 'Add worktrees view' see https://github.com/jesseduffield/lazygit/releases/tag/v0.40.0 for examples -->
2 parents 853a04d + ed9519a commit 9881628

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

pkg/tasks/tasks.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,30 +137,38 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
137137
cmd, r := start()
138138
timeToStart := time.Since(startTime)
139139

140+
done := make(chan struct{})
141+
140142
go utils.Safe(func() {
141-
<-opts.Stop
142-
// we use the time it took to start the program as a way of checking if things
143-
// are running slow at the moment. This is admittedly a crude estimate, but
144-
// the point is that we only want to throttle when things are running slow
145-
// and the user is flicking through a bunch of items.
146-
self.throttle = time.Since(startTime) < THROTTLE_TIME && timeToStart > COMMAND_START_THRESHOLD
147-
if err := oscommands.Kill(cmd); err != nil {
148-
if !strings.Contains(err.Error(), "process already finished") {
149-
self.Log.Errorf("error when running cmd task: %v", err)
143+
select {
144+
case <-done:
145+
// The command finished and did not have to be preemptively stopped before the next command.
146+
// No need to throttle.
147+
self.throttle = false
148+
case <-opts.Stop:
149+
// we use the time it took to start the program as a way of checking if things
150+
// are running slow at the moment. This is admittedly a crude estimate, but
151+
// the point is that we only want to throttle when things are running slow
152+
// and the user is flicking through a bunch of items.
153+
self.throttle = time.Since(startTime) < THROTTLE_TIME && timeToStart > COMMAND_START_THRESHOLD
154+
155+
// Kill the still-running command.
156+
if err := oscommands.Kill(cmd); err != nil {
157+
if !strings.Contains(err.Error(), "process already finished") {
158+
self.Log.Errorf("error when trying to kill cmd task: %v; Command: %v %v", err, cmd.Path, cmd.Args)
159+
}
150160
}
151-
}
152161

153-
// for pty's we need to call onDone here so that cmd.Wait() doesn't block forever
154-
onDone()
162+
// for pty's we need to call onDone here so that cmd.Wait() doesn't block forever
163+
onDone()
164+
}
155165
})
156166

157167
loadingMutex := deadlock.Mutex{}
158168

159169
// not sure if it's the right move to redefine this or not
160170
self.readLines = make(chan LinesToRead, 1024)
161171

162-
done := make(chan struct{})
163-
164172
scanner := bufio.NewScanner(r)
165173
scanner.Split(utils.ScanLinesAndTruncateWhenLongerThanBuffer(bufio.MaxScanTokenSize))
166174

@@ -269,8 +277,10 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
269277
refreshViewIfStale()
270278

271279
if err := cmd.Wait(); err != nil {
272-
// it's fine if we've killed this program ourselves
273-
if !strings.Contains(err.Error(), "signal: killed") {
280+
select {
281+
case <-opts.Stop:
282+
// it's fine if we've killed this program ourselves
283+
default:
274284
self.Log.Errorf("Unexpected error when running cmd task: %v; Failed command: %v %v", err, cmd.Path, cmd.Args)
275285
}
276286
}

0 commit comments

Comments
 (0)