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

Inconsistent error from http.ListenAndServe #35796

Closed
PumpkinSeed opened this issue Nov 23, 2019 · 4 comments
Closed

Inconsistent error from http.ListenAndServe #35796

PumpkinSeed opened this issue Nov 23, 2019 · 4 comments

Comments

@PumpkinSeed
Copy link

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

$ go version
go version go1.13.1 linux/amd64

Does this issue reproduce with the latest release?

I didn't see any change on this side in the changelog, so I think yes.

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/loow/.cache/go-build"
GOENV="/home/loow/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY="bitbucket.org/fluidpay"
GONOSUMDB="bitbucket.org/fluidpay"
GOOS="linux"
GOPATH="/home/loow/go"
GOPRIVATE="bitbucket.org/fluidpay"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/loow/go/src/github.com/PumpkinSeed/invoker/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-build232012226=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I wanted to shutdown the http server based on the Version change and then immediately start it again, so basically restart it. After the shutdown it starts and throw me an error http: Server closed, however the server starts properly and starting to serve requests. This is the reproduce of the issue, where I can send requests to the server after the error, and getting back valid responses.

package main

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

var (
Version = 0
changeHappened = make(chan bool)
)

func main() {
Serve()
}

func Serve() {
http.HandleFunc("/", handler)

for {
	hs := serve()

	<-changeHappened
	log.Print("Change happened")

	if err := hs.Shutdown(context.Background()); err != nil {
		log.Println("Shutdown", err)
	}
}

}

func serve() *http.Server {
log.Printf("Listening on %s", ":3000")
hs := &http.Server{Addr: ":3000"}
go func() {
if err := hs.ListenAndServe(); err != nil {
log.Println("ListenAndServe", err)
}
}()

return hs

}

func handler(w http.ResponseWriter, r *http.Request) {
defer func () {
if Version != 1 {
changeHappened <- true
}
} ()
w.Write([]byte("random\n"))
}

What did you expect to see?

$ go run main.go 
2019/11/23 10:30:15 Listening on :3000
2019/11/23 10:30:17 Change happened
2019/11/23 10:30:17 Listening on :3000

What did you see instead?

$ go run main.go 
2019/11/23 10:30:15 Listening on :3000
2019/11/23 10:30:17 Change happened
2019/11/23 10:30:17 Listening on :3000
2019/11/23 10:30:17 ListenAndServe http: Server closed
@seankhliao
Copy link
Member

this is a logging issue: the ListenAndServe http: Server closed error is from the first server shutting down, not from the new server

@agnivade
Copy link
Contributor

Thanks for assisting with the diagnosis @seankhliao. I will go ahead and close the issue.

@PumpkinSeed, if you have further questions, please feel free to ask it in any of these forums below:

Thanks

@PumpkinSeed
Copy link
Author

PumpkinSeed commented Nov 25, 2019

I got what you told me. In this case the issue with this, why I get error about Server Closed when I initiate the HTTP close by myself. So for example I want to use log.Fatal when the ListenAndServe throw me any error, but if I do shutdown on my side it will break the code always. It's not an error if I call the Shutdown of the http.Server and it's do what I told to it.

@agnivade
Copy link
Contributor

Please see the documentation:
https://golang.org/pkg/net/http/#Server.Shutdown
https://golang.org/pkg/net/http/#ListenAndServe

ListenAndServe always returns a non-nil error.

So you need to check for err != http.ErrServerClosed and then do a log.Fatal.

For further questions, please use the links above.

@golang golang locked and limited conversation to collaborators Nov 24, 2020
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

4 participants