Commit 2818cb5c authored by Chris Broadfoot's avatar Chris Broadfoot

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/37391Reviewed-by: default avatarJosh Bleecher Snyder <josharian@gmail.com>
parent d9270ecb
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"time"
) )
// Commands returns a list of possible commands to use to open a url. // Commands returns a list of possible commands to use to open a url.
...@@ -41,9 +42,26 @@ func Commands() [][]string { ...@@ -41,9 +42,26 @@ func Commands() [][]string {
func Open(url string) bool { func Open(url string) bool {
for _, args := range Commands() { for _, args := range Commands() {
cmd := exec.Command(args[0], append(args[1:], url)...) 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 true
} }
} }
return false 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
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment