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

net/http/server.go: the server doesn't exit when the listener socket fd closed #68175

Closed
lianghao208 opened this issue Jun 25, 2024 · 5 comments

Comments

@lianghao208
Copy link

lianghao208 commented Jun 25, 2024

Go version

go version go1.16 x86/amd64

Output of go env in your module/workspace:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build417955797=/tmp/go-build -gno-record-gcc-switches"

What did you do?

// Start initializes and starts components in dockerService.
func (ds *dockerService) Start() error {
         ....

        // start the server in goroutine
	go func() {
		if err := ds.streamingServer.Start(true); err != nil {
			klog.ErrorS(err, "Streaming server stopped unexpectedly")
			os.Exit(1)
		}
	}()
       ....
}

func (s *server) Start() error {

	listener, err := net.Listen("tcp", s.config.Addr)
	if err != nil {
		return err
	}
	return s.server.Serve(listener)
}

What did you see happen?

The server listens a random tcp port
image
after running a few days, the listen port is gone(can not find by netstat) and the fd socket is closed, but the process is still running and the goroutine still exists
image
image

What did you expect to see?

  1. the s.server.Serve(listener) should return immediately after the socket fd closed
  2. the socket fd should not be closed, there is no close() be called in the code
@seankhliao
Copy link
Member

1.16 isn't a supported version.

As far as we're aware, Serve does return on a closed listener:

package main

import (
	"log"
	"net"
	"net/http"
	"time"
)

func main() {
	lis, err := net.Listen("tcp", ":0")
	if err != nil {
		log.Fatalln(err)
	}
	svr := &http.Server{}
	errc := make(chan error)
	go func() {
		errc <- svr.Serve(lis)
	}()
	time.Sleep(time.Second)
	log.Println("close lis", lis.Close())
	log.Println("serve err", <-errc)
}

results in:

2024/06/25 16:20:15 close lis <nil>
2024/06/25 16:20:15 serve err accept tcp [::]:44157: use of closed network connection

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
@lianghao208
Copy link
Author

1.16 isn't a supported version.

As far as we're aware, Serve does return on a closed listener:

package main

import (
	"log"
	"net"
	"net/http"
	"time"
)

func main() {
	lis, err := net.Listen("tcp", ":0")
	if err != nil {
		log.Fatalln(err)
	}
	svr := &http.Server{}
	errc := make(chan error)
	go func() {
		errc <- svr.Serve(lis)
	}()
	time.Sleep(time.Second)
	log.Println("close lis", lis.Close())
	log.Println("serve err", <-errc)
}

results in:

2024/06/25 16:20:15 close lis <nil>
2024/06/25 16:20:15 serve err accept tcp [::]:44157: use of closed network connection

Yes, mostly the 1.16 behaves the same, the server will close after the listener was closed, but sometimes it won't. I can't reproduce it but it keeps happening.

@lianghao208
Copy link
Author

@seankhliao I know 1.16 is old, but does this issue remind you of any related issues?

@ianlancetaylor
Copy link
Member

There are a bunch of possibly-related issued in the comment by gabyhelp above.

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

No branches or pull requests

4 participants