From de5d3de2fe7da618cd9bdb1bac5d6bc0ddcff771 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Tue, 17 May 2016 14:06:45 -0700 Subject: [PATCH 1/2] Disable grpc tracing in benchmark --- benchmark/worker/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/benchmark/worker/main.go b/benchmark/worker/main.go index 8fc2160c..c8815b0e 100644 --- a/benchmark/worker/main.go +++ b/benchmark/worker/main.go @@ -207,6 +207,8 @@ func (s *workerServer) QuitWorker(ctx context.Context, in *testpb.Void) (*testpb } func main() { + grpc.EnableTracing = false + flag.Parse() lis, err := net.Listen("tcp", ":"+strconv.Itoa(*driverPort)) if err != nil { From bab01e8e85235b06e1da34c9c9b677f291d2dadb Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Mon, 16 May 2016 19:11:17 -0400 Subject: [PATCH 2/2] Fix a listener leak when a server is stopped before it starts If server.Stop() and server.Serve() race, Serve() can return without closing the listener. This in turn can lead to clients timing out trying to connect to a server that is neither accepting nor rejecting connections. --- server.go | 4 +++- server_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 server_test.go diff --git a/server.go b/server.go index d3a8073d..2df90dbf 100644 --- a/server.go +++ b/server.go @@ -257,12 +257,14 @@ func (s *Server) useTransportAuthenticator(rawConn net.Conn) (net.Conn, credenti // Serve accepts incoming connections on the listener lis, creating a new // ServerTransport and service goroutine for each. The service goroutines // read gRPC requests and then call the registered handlers to reply to them. -// Service returns when lis.Accept fails. +// Service returns when lis.Accept fails. lis will be closed when +// this method returns. func (s *Server) Serve(lis net.Listener) error { s.mu.Lock() s.printf("serving") if s.lis == nil { s.mu.Unlock() + lis.Close() return ErrServerStopped } s.lis[lis] = true diff --git a/server_test.go b/server_test.go new file mode 100644 index 00000000..bf232375 --- /dev/null +++ b/server_test.go @@ -0,0 +1,61 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +package grpc + +import ( + "net" + "strings" + "testing" +) + +func TestStopBeforeServe(t *testing.T) { + lis, err := net.Listen("tcp", "localhost:0") + if err != nil { + t.Fatalf("failed to create listener: %v", err) + } + + server := NewServer() + server.Stop() + err = server.Serve(lis) + if err != ErrServerStopped { + t.Fatalf("server.Serve() error = %v, want %v", err, ErrServerStopped) + } + + // server.Serve is responsible for closing the listener, even if the + // server was already stopped. + err = lis.Close() + if got, want := ErrorDesc(err), "use of closed network connection"; !strings.Contains(got, want) { + t.Errorf("Close() error = %q, want %q", got, want) + } +}