]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/browser: wait 3 seconds for non-zero exit codes
authorChris Broadfoot <cbro@golang.org>
Wed, 22 Feb 2017 23:50:24 +0000 (15:50 -0800)
committerChris Broadfoot <cbro@golang.org>
Fri, 24 Feb 2017 19:14:12 +0000 (19:14 +0000)
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>
src/cmd/internal/browser/browser.go

index 33b7bb90405a6ab115774b9a87a0cea4f2449d21..595c41b3dd405ced92516471e79e6edeb7a3efed 100644 (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
+       }
+}