Skip to content

net/http: client errors on server graceful shutdown #23829

Closed
@pugachAG

Description

@pugachAG

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

go version go1.10rc2 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"                                                                                                                                                
GOBIN=""
GOCACHE="/home/apuhach/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/git_tree/go/"
GORACE=""
GOROOT="/home/apuhach/sdk/go1.10rc2"
GOTMPDIR=""
GOTOOLDIR="/home/apuhach/sdk/go1.10rc2/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build506882789=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
	"context"
)

var cmd = flag.String("cmd", "", "cmd")

func serverCmd() {
	mux := http.NewServeMux()
	mux.HandleFunc("/",
		func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("hello world\n"))
		})


	for {
		server := &http.Server{
			Addr:    "127.0.0.1:9228",
			Handler: mux,
		}
		go func() {
			if err := server.ListenAndServe(); err != nil {
				fmt.Printf("ListenAndServe done: %v\n", err)
			}
		}()
		time.Sleep(2 * time.Second)
		server.Shutdown(context.Background())
	}
}

func clientCmd() {
	client := http.Client{}
	for {
		request, _ := http.NewRequest("POST", "http://127.0.0.1:9228", nil)
		resp, err := client.Do(request)
		if err != nil {
			fmt.Printf("http.Post failed: %v\n", err)
			time.Sleep(1 * time.Second)
		} else {
			ioutil.ReadAll(resp.Body)
			resp.Body.Close()
		}
	}
}

func main() {
	flag.Parse()
	switch *cmd {
	case "server":
		serverCmd()
	case "client":
		clientCmd()
	default:
		fmt.Printf("unknown cmd %v", *cmd)
	}
}

run server: go run main.go -cmd server
and client: go run main.go -cmd client

What did you expect to see?

client gets only connection refused errors:

http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused

What did you see instead?

mix of connection refused, EOF and connection reset by peer errors:

http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: read tcp 127.0.0.1:57730->127.0.0.1:9228: read: connection reset by peer                     
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: EOF                                                                                                             
http.Post failed: Post http://127.0.0.1:9228: dial tcp 127.0.0.1:9228: getsockopt: connection refused                                                         

Server suppose to shutdown gracefully without causing EOF or connection reset by peer errors on client side

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions