Skip to content

os/exec: CommandContext with timeout with multiple subprocesses isn't canceled #22485

@msoap

Description

@msoap

What version of Go are you using (go version)?

  • go version go1.7.6 darwin/amd64
  • go version go1.8.5 darwin/amd64
  • go version go1.9.2 darwin/amd64
  • go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/msa/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nf/3r6ynmsn52ndstns_gkkfy580000gn/T/go-build091401128=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

I'm run the exec.CommandContext with 1 second timeout with a shell command that is delayed 5 seconds.

package main

import (
	"context"
	"fmt"
	"os/exec"
	"time"
)

func main() {
	ctx, cancelFn := context.WithTimeout(context.Background(), time.Second)
	defer cancelFn()

	t := time.Now()
	_, err := exec.CommandContext(ctx, "sh", "-c", `sleep 5; echo ok`).CombinedOutput()
	fmt.Printf("Error: %q, time: %s\n", err, time.Now().Sub(t))
}

What did you expect to see?

The command must be canceled after 1 second.

What did you see instead?

Command is delayed 5 seconds.

Error: "signal: killed", time: 5.011181921s:

I assume that this is due to the number of processes that are started.
That's few examples (for go version go1.9.2 darwin/amd64):

For go version go1.9.2 linux/amd64:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions