Navigation Menu

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: Document Exit() behavior with code outside 0 - 255 #30959

Closed
cuonglm opened this issue Mar 20, 2019 · 3 comments
Closed

os: Document Exit() behavior with code outside 0 - 255 #30959

cuonglm opened this issue Mar 20, 2019 · 3 comments
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@cuonglm
Copy link
Member

cuonglm commented Mar 20, 2019

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

$ go version
go version go1.12.1 linux/amd64

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
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/cuonglm/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/cuonglm/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/stow/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/stow/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/cuonglm/sources/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build303772017=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import "os"

func main() {
	os.Exit(256)
}

Compile program then run:

./main || echo ./main should return failure code

What did you expect to see?

./main should return failure code is printed in terminal.

What did you see instead?

No output.

The number 256 is passed as-is to _exit(), or in my case exit_group() on Linux.

But on Linux, and other systems which use traditional wait*() system call, only the lower 8 bits of that number are available to the parent. it means the program above appears as success.

If the parent uses waitid(), the number won't be truncated and return as-is to the parent (Though not on Linux, which still truncate the number even waitid() was used. The behavior maybe change in the future).

So the behavior of os.Exit(code) where code > 255 or code < 0 can not be consistent. I propose one of these changes:

  • Always transform the code like wait*() does, mean a value between 0-255 always returns to parent.

  • Limit the code to value between 0 - 255, do something in case the code out of range:

    • warning message that value outside of 0 - 255 can be truncated
    • warning message that value outside of 0 - 255 is bad number (like dash, yash does)
    • panic, so user knows the problem, and get a standard error code from panic. It also does not break any program. (like m4 does)
  • Document that only value between 0 - 255 is consistent and should be used. Value greater than 255 may or may not be truncated, so the exit code is unspecified.

@cuonglm cuonglm changed the title os: Document code parameter of Exit() os: Document Exit(code) behavior with code out of range 0 - 255 Mar 20, 2019
@cuonglm cuonglm changed the title os: Document Exit(code) behavior with code out of range 0 - 255 os: Document Exit() behavior with code out of range 0 - 255 Mar 20, 2019
@cuonglm cuonglm changed the title os: Document Exit() behavior with code out of range 0 - 255 os: Document Exit() behavior with code outside 0 - 255 Mar 20, 2019
@ALTree ALTree added this to the Go1.13 milestone Mar 21, 2019
@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 21, 2019
@cuonglm
Copy link
Member Author

cuonglm commented Mar 25, 2019

@robpike
Copy link
Contributor

robpike commented Mar 26, 2019

I resist documenting the operating systems themselves, but in this case it seems reasonable to document that valid values range from 0 to 255. There's nothing to do in the implementation that makes any sense: we're exiting anyway.

@gopherbot
Copy link

Change https://golang.org/cl/169357 mentions this issue: os: document exit status range value

@golang golang locked and limited conversation to collaborators Mar 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants