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: os Process.Kill() not working on windows #51151

Closed
gianniLesl opened this issue Feb 11, 2022 · 5 comments
Closed

os: os Process.Kill() not working on windows #51151

gianniLesl opened this issue Feb 11, 2022 · 5 comments

Comments

@gianniLesl
Copy link

gianniLesl commented Feb 11, 2022

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

$ go version

Tested with go version 1.17, 1.17.1, 1.17.3, 1.17.6, and 1.17.7

The feature is working on 1.16.14 and earlier.

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env

GOOS="windows" GOARCH="amd64"

What did you do?

I created a small script to use to kill particular processes on windows. When I compile and run the executable though I get the error DuplicateHandle: The handle is invalid.

Source code: https://go.dev/play/p/Ik3UMsABNQI

What did you expect to see?

Expected Process.Kill to kill the running process without error

What did you see instead?

DuplicateHandle: The handle is invalid.

@bcmills bcmills changed the title affected/package: os Process.Kill() not working on windows os: os Process.Kill() not working on windows Feb 11, 2022
@bcmills
Copy link
Contributor

bcmills commented Feb 11, 2022

The os.Process struct has unexported fields, so you can't expect to create a valid one using a struct literal.

Instead, look up the desired PID using os.FindProcess.

@bcmills bcmills closed this as completed Feb 11, 2022
@gianniLesl
Copy link
Author

gianniLesl commented Feb 11, 2022

We expected this to work because it was working in 1.16.14, so 1.17 breaks backwards compatibility. This seems to be the change that broke us 105c5b5?diff=split . When I run with the function

func TerminateProcess(process os.Process, exitCode int) error {
	h, e := syscall.OpenProcess(syscall.PROCESS_TERMINATE, false, uint32(process.Pid))
	if e != nil {
		return os.NewSyscallError("OpenProcess", e)
	}
	defer syscall.CloseHandle(h)
	e = syscall.TerminateProcess(h, uint32(exitCode))
	if e != nil {
		return os.NewSyscallError("TerminateProcess", e)
	}

	runtime.KeepAlive(process)
	return nil
}

execution succeeds.

Source Code: https://go.dev/play/p/mELlwQqiBZa

(Note: When we run os.FindProcess on 1.16 and 1.17 it returns "OpenProcess: Access is denied." for our processes)

@ianlancetaylor
Copy link
Contributor

Writing

	process := os.Process{
		Pid: pid,
	}

was never supposed to work. I'm sorry that it did.

@beoran
Copy link

beoran commented Feb 12, 2022

In this case, it should have been documented that it cannot work. While there are unexported members, it isn't immediately clear from the docs that Kill will not work.

@ianlancetaylor
Copy link
Contributor

To me it seems clear from the documentation which explicitly says that Process stores information about a process created by StartProcess. But if someone wants to send a brief and to the point update to the docs we can certainly take a look. Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants