1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:04:57 -07:00

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 <josharian@gmail.com>
This commit is contained in:
Chris Broadfoot 2017-02-22 15:50:24 -08:00
parent d9270ecb3a
commit 2818cb5c9e

View File

@ -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
}
}