Skip to content

Commit

Permalink
misc/android: use adb exec-out instead of adb shell to avoid buffering
Browse files Browse the repository at this point in the history
According to

https://stackoverflow.com/questions/46233200/stop-buffering-of-adb-shell-output

the adb exec-out commands avoids the buffering inherent to adb shell.

Let's see if using exec-out will fix the android builder flakyness where
exitcodes or output were sometimes missing.

Updates golang#30512 (perhaps fixes it).

Change-Id: Ib953ef0262b20730e0d4c332058d29c5066bfeb2
Reviewed-on: https://go-review.googlesource.com/c/164661
Run-TryBot: Elias Naur <mail@eliasnaur.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
eliasnaur committed Mar 2, 2019
1 parent e0ff4e6 commit 3de2fb2
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions misc/android/go_android_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func main() {
// In case we're booting a device or emulator alongside all.bash, wait for
// it to be ready. adb wait-for-device is not enough, we have to
// wait for sys.boot_completed.
run("wait-for-device", "shell", "while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;")
run("wait-for-device", "exec-out", "while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;")

// Prepare a temporary directory that will be cleaned up at the end.
deviceGotmp := fmt.Sprintf("/data/local/tmp/%s-%d",
filepath.Base(os.Args[1]), os.Getpid())
run("shell", "mkdir", "-p", deviceGotmp)
run("exec-out", "mkdir", "-p", deviceGotmp)

// Determine the package by examining the current working
// directory, which will look something like
Expand All @@ -94,7 +94,7 @@ func main() {
} else {
adbSyncGoroot()
}
run("shell", "mkdir", "-p", deviceCwd)
run("exec-out", "mkdir", "-p", deviceCwd)

// Binary names can conflict.
// E.g. template.test from the {html,text}/template packages.
Expand All @@ -114,28 +114,25 @@ func main() {
for range quit {
// We don't have the PID of the running process; use the
// binary name instead.
run("shell", "killall -QUIT "+binName)
run("exec-out", "killall -QUIT "+binName)
}
}()
// The adb shell command will return an exit code of 0 regardless
// of the command run. E.g.
// $ adb shell false
// $ echo $?
// 0
// In light of
// https://code.google.com/p/android/issues/detail?id=3254
// So we append the exitcode to the output and parse it from there.
// dont trust the exitcode of adb. Instead, append the exitcode to
// the output and parse it from there.
const exitstr = "exitcode="
cmd := `export TMPDIR="` + deviceGotmp + `"` +
`; export GOROOT="` + deviceGoroot + `"` +
`; export GOPATH="` + deviceGopath + `"` +
`; cd "` + deviceCwd + `"` +
"; '" + deviceBin + "' " + strings.Join(os.Args[2:], " ") +
"; echo -n " + exitstr + "$?"
output := run("shell", cmd)
output := run("exec-out", cmd)
signal.Reset(syscall.SIGQUIT)
close(quit)

run("shell", "rm", "-rf", deviceGotmp) // Clean up.
run("exec-out", "rm", "-rf", deviceGotmp) // Clean up.

exitIdx := strings.LastIndex(output, exitstr)
if exitIdx == -1 {
Expand Down Expand Up @@ -211,8 +208,8 @@ func adbSyncGoroot() {
return
}
devRoot := "/data/local/tmp/goroot"
run("shell", "rm", "-rf", devRoot)
run("shell", "mkdir", "-p", devRoot+"/pkg")
run("exec-out", "rm", "-rf", devRoot)
run("exec-out", "mkdir", "-p", devRoot+"/pkg")
goroot := runtime.GOROOT()
goCmd := filepath.Join(goroot, "bin", "go")
runtimea, err := exec.Command(goCmd, "list", "-f", "{{.Target}}", "runtime").Output()
Expand Down

0 comments on commit 3de2fb2

Please sign in to comment.