Skip to content

Commit

Permalink
Fix panic getting terminal width in some CI environments
Browse files Browse the repository at this point in the history
In some CI environments like Codefresh, k6 detects that it's running in
a tty, the err returned by `terminal.GetSize()` is `nil`, yet
`termWidth` is 0. Strange scenario, and we should fix our TTY check if
possible, but it shouldn't panic with `divide by zero` anymore.

Closes #1579
  • Loading branch information
Ivan Mirić authored and imiric committed Aug 10, 2020
1 parent b83dbbd commit 3a713ec
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,15 @@ func showProgress(
}

var errTermGetSize bool
termWidth, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil && stdoutTTY {
logger.WithError(err).Warn("error getting terminal size")
termWidth = defaultTermWidth
errTermGetSize = true
termWidth := defaultTermWidth
if stdoutTTY {
tw, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if !(tw > 0) || err != nil {
errTermGetSize = true
logger.WithError(err).Warn("error getting terminal size")
} else {
termWidth = tw
}
}

// Get the longest left side string length, to align progress bars
Expand Down Expand Up @@ -333,19 +337,19 @@ func showProgress(
outMutex.Unlock()
return
case <-winch:
if !errTermGetSize {
if stdoutTTY && !errTermGetSize {
// More responsive progress bar resizing on platforms with SIGWINCH (*nix)
termWidth, _, err = terminal.GetSize(fd)
if err != nil {
termWidth = defaultTermWidth
tw, _, err := terminal.GetSize(fd)
if tw > 0 && err == nil {
termWidth = tw
}
}
case <-ticker.C:
// Default ticker-based progress bar resizing
if !errTermGetSize && winch == nil {
termWidth, _, err = terminal.GetSize(fd)
if err != nil {
termWidth = defaultTermWidth
if stdoutTTY && !errTermGetSize && winch == nil {
tw, _, err := terminal.GetSize(fd)
if tw > 0 && err == nil {
termWidth = tw
}
}
}
Expand Down

0 comments on commit 3a713ec

Please sign in to comment.