Skip to content

Commit

Permalink
force UI teardown when event is sourced from a signal interrupt (#453)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman authored Jun 29, 2021
1 parent fb0857f commit 2de56c0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cmd/event_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// eventLoop listens to worker errors (from execution path), worker events (from a partybus subscription), and
// signal interrupts. Is responsible for handling each event relative to a given UI an to coordinate eventing until
// an eventual graceful exit.
// nolint:gocognit
// nolint:gocognit,funlen
func eventLoop(workerErrs <-chan error, signals <-chan os.Signal, subscription *partybus.Subscription, ux ui.UI, cleanupFn func()) error {
defer cleanupFn()
events := subscription.Events()
Expand All @@ -23,6 +23,7 @@ func eventLoop(workerErrs <-chan error, signals <-chan os.Signal, subscription *
}

var retErr error
var forceTeardown bool

for {
if workerErrs == nil && events == nil {
Expand Down Expand Up @@ -66,10 +67,11 @@ func eventLoop(workerErrs <-chan error, signals <-chan os.Signal, subscription *
// of processing.
events = nil
workerErrs = nil
forceTeardown = true
}
}

if err := ux.Teardown(); err != nil {
if err := ux.Teardown(forceTeardown); err != nil {
retErr = multierror.Append(retErr, err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/event_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (u *uiMock) Handle(event partybus.Event) error {
return u.Called(event).Error(0)
}

func (u *uiMock) Teardown() error {
func (u *uiMock) Teardown(_ bool) error {
u.t.Logf("UI Teardown called")
return u.Called().Error(0)
}
Expand Down
12 changes: 7 additions & 5 deletions internal/ui/ephemeral_terminal_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (h *ephemeralTerminalUI) Handle(event partybus.Event) error {
case event.Type == syftEvent.PresenterReady:
// we need to close the screen now since signaling the the presenter is ready means that we
// are about to write bytes to stdout, so we should reset the terminal state first
h.closeScreen()
h.closeScreen(false)

if err := handleCatalogerPresenterReady(event); err != nil {
log.Errorf("unable to show %s event: %+v", event.Type, err)
Expand Down Expand Up @@ -105,11 +105,13 @@ func (h *ephemeralTerminalUI) openScreen() error {
return nil
}

func (h *ephemeralTerminalUI) closeScreen() {
func (h *ephemeralTerminalUI) closeScreen(force bool) {
// we may have other background processes still displaying progress, wait for them to
// finish before discontinuing dynamic content and showing the final report
if !h.frame.IsClosed() {
h.waitGroup.Wait()
if !force {
h.waitGroup.Wait()
}
h.frame.Close()
// TODO: there is a race condition within frame.Close() that sometimes leads to an extra blank line being output
frame.Close()
Expand All @@ -130,8 +132,8 @@ func (h *ephemeralTerminalUI) flushLog() {
}
}

func (h *ephemeralTerminalUI) Teardown() error {
h.closeScreen()
func (h *ephemeralTerminalUI) Teardown(force bool) error {
h.closeScreen(force)
ansi.CursorShow()
return nil
}
2 changes: 1 addition & 1 deletion internal/ui/logger_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func (l loggerUI) Handle(event partybus.Event) error {
return l.unsubscribe()
}

func (l loggerUI) Teardown() error {
func (l loggerUI) Teardown(_ bool) error {
return nil
}
2 changes: 1 addition & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
type UI interface {
Setup(unsubscribe func() error) error
partybus.Handler
Teardown() error
Teardown(force bool) error
}

0 comments on commit 2de56c0

Please sign in to comment.