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

Comments

@bombela
Copy link

@bombela 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

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 31, 2017

I'm not sure we can change Exited now.

Perhaps we should simply add Signaled.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Sep 14, 2018

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

@andybons

This comment has been minimized.

Copy link
Member

@andybons andybons commented Sep 20, 2018

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

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Oct 20, 2018

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.)

@gopherbot gopherbot closed this Oct 20, 2018
@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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.