Skip to content

net/http: something about semantics of Server.Close #17880

Closed
@peter-mogensen

Description

@peter-mogensen

Please answer these questions before submitting your issue. Thanks!

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

Go master HEAD 8d0c105 + fix for #17878

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

What did you do?

package main

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

type server3 struct {
	http.Server
}

func (s *server3) Serve(l net.Listener) error {
	fmt.Println("Serving")
	// Remove sleep to not hit deadlock
	time.Sleep(time.Second)
	return s.Server.Serve(l)
}

func (s *server3) Shutdown() {
	fmt.Println("Closing")
	s.Close()
}

func main() {

	srv := &server3{}
	srvEnd :=  make(chan error)


	for {
		l, err := net.Listen("tcp","")
		if err != nil {
			log.Fatal(err)
		}
		
		go func() {
			err := srv.Serve(l)
			srvEnd <- err 
		}()

		time.Sleep(time.Second)
		
		srv.Shutdown()

		fmt.Println("Close done")
		
		err = <- srvEnd
		
		fmt.Println(err)
	}
}

What did you expect to see?

Serving
Closing
Close done
http: Server closed
Serving
Closing
Close done
http: Server closed
Serving
Closing
Close done
http: Server closed
Serving
Closing
Close done
http: Server closed
....

What did you see instead?

Serving
Closing
Close done
^C

Discussion

It seems that there's really no way to have guarantee whether a given Shutdown() will actually result in no running server.
The issue was discussed in this thread:
https://groups.google.com/forum/#!topic/golang-nuts/hACrtl4JoUY

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions