mirror of
https://github.com/golang/go
synced 2024-11-19 06:34:42 -07:00
305a363bdd
1) Killing proceses on timeout was wrong: the os/exec package will never return our package's error type from its Wait. 2) fix a goroutine leak on timeout. 3) unexported an undocumented and elsewhere-unused type. 4) rename timeout type to end in "Error", per convention, not Err. LGTM=adg R=adg CC=golang-codereviews https://golang.org/cl/74290043
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
// Copyright 2011 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
// run is a simple wrapper for exec.Run/Close
|
|
func run(d time.Duration, envv []string, dir string, argv ...string) error {
|
|
if *verbose {
|
|
log.Println("run", argv)
|
|
}
|
|
cmd := exec.Command(argv[0], argv[1:]...)
|
|
cmd.Dir = dir
|
|
cmd.Env = envv
|
|
cmd.Stderr = os.Stderr
|
|
if err := cmd.Start(); err != nil {
|
|
return err
|
|
}
|
|
err := timeout(d, cmd.Wait)
|
|
if _, ok := err.(timeoutError); ok {
|
|
cmd.Process.Kill()
|
|
}
|
|
return err
|
|
}
|
|
|
|
// runLog runs a process and returns the combined stdout/stderr. It returns
|
|
// process combined stdout and stderr output, exit status and error. The
|
|
// error returned is nil, if process is started successfully, even if exit
|
|
// status is not successful.
|
|
func runLog(timeout time.Duration, envv []string, dir string, argv ...string) (string, bool, error) {
|
|
var b bytes.Buffer
|
|
ok, err := runOutput(timeout, envv, &b, dir, argv...)
|
|
return b.String(), ok, err
|
|
}
|
|
|
|
// runOutput runs a process and directs any output to the supplied writer.
|
|
// It returns exit status and error. The error returned is nil, if process
|
|
// is started successfully, even if exit status is not successful.
|
|
func runOutput(d time.Duration, envv []string, out io.Writer, dir string, argv ...string) (bool, error) {
|
|
if *verbose {
|
|
log.Println("runOutput", argv)
|
|
}
|
|
|
|
cmd := exec.Command(argv[0], argv[1:]...)
|
|
cmd.Dir = dir
|
|
cmd.Env = envv
|
|
cmd.Stdout = out
|
|
cmd.Stderr = out
|
|
|
|
startErr := cmd.Start()
|
|
if startErr != nil {
|
|
return false, startErr
|
|
}
|
|
|
|
if err := timeout(d, cmd.Wait); err != nil {
|
|
if _, ok := err.(timeoutError); ok {
|
|
cmd.Process.Kill()
|
|
}
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// timeout runs f and returns its error value, or if the function does not
|
|
// complete before the provided duration it returns a timeout error.
|
|
func timeout(d time.Duration, f func() error) error {
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
errc <- f()
|
|
}()
|
|
t := time.NewTimer(d)
|
|
defer t.Stop()
|
|
select {
|
|
case <-t.C:
|
|
return timeoutError(d)
|
|
case err := <-errc:
|
|
return err
|
|
}
|
|
}
|
|
|
|
type timeoutError time.Duration
|
|
|
|
func (e timeoutError) Error() string {
|
|
return fmt.Sprintf("timed out after %v", time.Duration(e))
|
|
}
|