From 2818cb5c9e183aed539d6a539a821e229671fe56 Mon Sep 17 00:00:00 2001 From: Chris Broadfoot Date: Wed, 22 Feb 2017 15:50:24 -0800 Subject: [PATCH] cmd/internal/browser: wait 3 seconds for non-zero exit codes Wait a short period between trying commands. Many commands will return a non-zero exit code if the browser couldn't be launched. For example, google-chrome returns quickly with a non-zero exit code in a headless environment. Updates #19131. Change-Id: I0ae5356dd4447969d9e216615449cead7a8fd5c9 Reviewed-on: https://go-review.googlesource.com/37391 Reviewed-by: Josh Bleecher Snyder --- src/cmd/internal/browser/browser.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/cmd/internal/browser/browser.go b/src/cmd/internal/browser/browser.go index 33b7bb9040..595c41b3dd 100644 --- a/src/cmd/internal/browser/browser.go +++ b/src/cmd/internal/browser/browser.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "runtime" + "time" ) // Commands returns a list of possible commands to use to open a url. @@ -41,9 +42,26 @@ func Commands() [][]string { func Open(url string) bool { for _, args := range Commands() { cmd := exec.Command(args[0], append(args[1:], url)...) - if cmd.Start() == nil { + if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) { return true } } return false } + +// appearsSuccessful reports whether the command appears to have run succesfully. +// If the command runs longer than the timeout, it's deemed successful. +// If the command runs within the timeout, it's deemed successful if it exited cleanly. +func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool { + errc := make(chan error, 1) + go func() { + errc <- cmd.Wait() + }() + + select { + case <-time.After(timeout): + return true + case err := <-errc: + return err == nil + } +}