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.Close does defer Lock #17878

Closed
peter-mogensen opened this issue Nov 10, 2016 · 3 comments
Closed

net/http: Server.Close does defer Lock #17878

peter-mogensen opened this issue Nov 10, 2016 · 3 comments
Milestone

Comments

@peter-mogensen
Copy link

peter-mogensen commented Nov 10, 2016

Please answer these questions before submitting your issue. Thanks!

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

Go master HEAD 8d0c105

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")
	time.Sleep(time.Second)
	return s.Server.Serve(l)
}

// naming is a bitch
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
^C

What did you see instead?

Serving
Closing
^C

@peter-mogensen
Copy link
Author

$ git diff
diff --git a/src/net/http/server.go b/src/net/http/server.go
index 0959ac6..d78fd71 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -2362,7 +2362,7 @@ func (s *Server) closeDoneChanLocked() {
 // regardless of their state. For a graceful shutdown, use Shutdown.
 func (s *Server) Close() error {
        s.mu.Lock()
-       defer s.mu.Lock()
+       defer s.mu.Unlock()
        s.closeDoneChanLocked()
        err := s.closeListenersLocked()
        for c := range s.activeConn {

@bradfitz bradfitz self-assigned this Nov 10, 2016
@bradfitz bradfitz changed the title http.Server.Close() does defer s.mu.Lock() ... instead of Unlock() net/http: Server.Close does defer Lock Nov 10, 2016
@bradfitz
Copy link
Contributor

That's a very wordy bug report. Thanks to @peter-mogensen for summarizing.

@bradfitz bradfitz added this to the Go1.8 milestone Nov 10, 2016
@gopherbot
Copy link

CL https://golang.org/cl/33026 mentions this issue.

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

3 participants