Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: Process.Exited returns false after exiting on signal #19798

Closed
bombela opened this issue Mar 31, 2017 · 4 comments
Closed

os: Process.Exited returns false after exiting on signal #19798

bombela opened this issue Mar 31, 2017 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@bombela
Copy link

bombela commented Mar 31, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.7.5 linux/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/bombela/u/go"
GORACE=""
GOROOT="/home/bombela/u/go/go1.7.5"
GOTOOLDIR="/home/bombela/u/go/go1.7.5/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build016673509=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

package main

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

func main() {
	cmd := exec.Command("sleep", "10")
	if err := cmd.Start(); err != nil {
		panic(err)
	}
	time.Sleep(time.Second * 2)
	cmd.Process.Signal(os.Interrupt)
	err := cmd.Wait()
	fmt.Println(err) // prints signal interrupt error
	fmt.Printf("ProcessState: Exited=%v, Success=%v\n", cmd.ProcessState.Exited(),
		cmd.ProcessState.Success()) // Exited should be true.
	waitstatus := cmd.ProcessState.Sys().(syscall.WaitStatus)
	fmt.Printf("WaitStatus:   Exited=%v, Signaled=%v\n",
		waitstatus.Exited(), waitstatus.Signaled())
}

What did you expect to see?

$ go run main.go 
signal: interrupt
ProcessState: Exited=true, Success=false
WaitStatus:   Exited=false, Signaled=true

What did you see instead?

$ go run main.go 
signal: interrupt
ProcessState: Exited=false, Success=false # I expect exited == true here,
WaitStatus:   Exited=false, Signaled=true    # and here

It looks like Exited() returns true only if the process exited via exit(), not when it exited via signal. It could be that Exited() should return true on signal.

Casting to syscall.WaitStatus allows to check for both Exited() and Signaled(), making it possible to know if the process is running or not. It seems to be impossible to know if the process is still running via the Command abstraction.

Naive suggestion: maybe a IsRunning() should be provided on Command.

@ianlancetaylor ianlancetaylor changed the title os.Command.ProcessState.Exited() returns false after exiting on signal os: Process.Exited returns false after exiting on signal Mar 31, 2017
@ianlancetaylor
Copy link
Member

I'm not sure we can change Exited now.

Perhaps we should simply add Signaled.

@andybons andybons added NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Apr 11, 2018
@andybons andybons added this to the Unplanned milestone Apr 11, 2018
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/135515 mentions this issue: os/exec: add method Signaled to ProcessState

@andybons andybons added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Sep 20, 2018
@andybons
Copy link
Member

This needs to be a formal proposal (https://golang.org/s/proposal) before we can move forward.

@andybons andybons added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Sep 20, 2018
@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Oct 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants