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

runtime: golang Automatically Closed fd 1 and fd 2 #58424

Closed
hengwu0 opened this issue Feb 9, 2023 · 3 comments
Closed

runtime: golang Automatically Closed fd 1 and fd 2 #58424

hengwu0 opened this issue Feb 9, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@hengwu0
Copy link
Contributor

hengwu0 commented Feb 9, 2023

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

$ go version
go version go1.17.13 linux/arm64

Does this issue reproduce with the latest release?

Not tried.

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

go env Output
```sh
$ go env
jim@localhost:~/smb$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/home/jim/.cache/go-build"
GOENV="/home/jim/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jim/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jim/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/go/pkg/tool/linux_arm64"
GOVCS=""
GOVERSION="go1.17.13"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1921286011=/tmp/go-build -gno-record-gcc-switches"
```

What did you do?

my code is like:

	stdoutPath := "./aaa.log"
	if f, err := os.OpenFile(stdoutPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err == nil {
		syscall.Dup3(int(f.Fd()), syscall.Stdout, 0)
		if err != nil {
			return
		}
		os.Stdout = os.NewFile(uintptr(syscall.Stdout), stdoutPath)
		fmt.Printf("Stdout(%#v) reopened: %s.\n", os.Stdout.Fd(), stdoutPath)
		f.Close()
	}

What did you expect to see?

stdout fd will never be closed. And the child process can use it to print log.

What did you see instead?

I found the program automatically closed stdout (fd 1) after a long time(like one day), which can't be seen in /proc/${pid}/fd.
And this makes the child process missing stdout, and can't print anything.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 9, 2023
@ianlancetaylor
Copy link
Contributor

Go will close a file descriptor if the os.File is garbage collected. Is it possible that your program changed os.Stdout to something else? Other than that, Go won't close file descriptors.

Otherwise, can you show us a small complete program that demonstrates the problem? Thanks.

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 9, 2023
@rittneje
Copy link

rittneje commented Feb 9, 2023

When you replace os.Stdout, the original value is eligible for garbage collection at some point in the future. When this happens, its finalizer will run, which in turn closes its fd (1). Since that is what you dup'ed into, it ends up closing your log file.

To fix this, remove the part where you replace os.Stdout. There is no need for this, as os.Stdout already uses fd 1.

@ianlancetaylor
Copy link
Contributor

@rittneje Thanks, good point.

Closing because I don't think there is anything to do here.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Feb 9, 2023
@golang golang locked and limited conversation to collaborators Feb 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. 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