From f34c7b90580c4bba255634daee98f55c1387c74f Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 23 Jun 2019 19:56:23 +1000 Subject: [PATCH] close goEvery goroutines when switching to a subprocess --- pkg/gui/gui.go | 10 ++++++++++ pkg/gui/subprocess.go | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index f47c7a7a5..c2eb2c114 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -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 @@ -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"} @@ -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() } }() diff --git a/pkg/gui/subprocess.go b/pkg/gui/subprocess.go index ea8f46361..37726e65b 100644 --- a/pkg/gui/subprocess.go +++ b/pkg/gui/subprocess.go @@ -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 }