Skip to content

Commit

Permalink
close goEvery goroutines when switching to a subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Jun 23, 2019
1 parent 54a8068 commit f34c7b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ type guiState struct {
Panels *panelStates
SubProcessOutput string
Stats map[string]commands.ContainerStats

// SessionIndex tells us how many times we've come back from a subprocess.
// We increment it each time we switch to a new subprocess
// Every time we go to a subprocess we need to close a few goroutines so this index is used for that purpose
SessionIndex int
}

// NewGui builds a new gui handler
Expand All @@ -144,6 +149,7 @@ func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand
},
Status: &statusState{ContextIndex: 0},
},
SessionIndex: 0,
}

cyclableViews := []string{"status", "containers", "images", "volumes"}
Expand Down Expand Up @@ -230,9 +236,13 @@ func (gui *Gui) renderGlobalOptions() error {
}

func (gui *Gui) goEvery(interval time.Duration, function func() error) {
currentSessionIndex := gui.State.SessionIndex
_ = function() // time.Tick doesn't run immediately so we'll do that here // TODO: maybe change
go func() {
for range time.Tick(interval) {
if gui.State.SessionIndex > currentSessionIndex {
return
}
_ = function()
}
}()
Expand Down
6 changes: 5 additions & 1 deletion pkg/gui/subprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ func (gui *Gui) RunWithSubprocesses() error {
} else if err == gui.Errors.ErrSubProcess {
// preparing the state for when we return
gui.State.PreviousView = gui.currentViewName()
gui.State.Panels.Main.ObjectKey = ""
// giving goEvery goroutines time to finish
gui.State.SessionIndex++

if err := gui.runCommand(); err != nil {
return err
}

// ensuring we render e.g. the logs of the currently selected item upon return
gui.State.Panels.Main.ObjectKey = ""
} else {
return err
}
Expand Down

0 comments on commit f34c7b9

Please sign in to comment.