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

server: add missing conn.Close if the connection dies before reading the HTTP/2 preface #4837

Merged
merged 5 commits into from Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/transport/http2_server.go
Expand Up @@ -293,7 +293,7 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
// closed immediately by the latter. Skipping the error here will help
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the comments still valid? Delete them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was mostly still relevant, but not 100% accurate after the change. Reworded.

// reduce log clutter.
if err == io.EOF {
return nil, nil
return nil, io.EOF
}
return nil, connectionErrorf(false, err, "transport: http2Server.HandleStreams failed to receive the preface from client: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions server.go
Expand Up @@ -885,13 +885,11 @@ func (s *Server) newHTTP2Transport(c net.Conn) transport.ServerTransport {
// ErrConnDispatched means that the connection was dispatched away from
// gRPC; those connections should be left open.
if err != credentials.ErrConnDispatched {
c.Close()
}
// Don't log on ErrConnDispatched and io.EOF to prevent log spam.
if err != credentials.ErrConnDispatched {
// Don't log on ErrConnDispatched and io.EOF to prevent log spam.
if err != io.EOF {
channelz.Warning(logger, s.channelzID, "grpc: Server.Serve failed to create ServerTransport: ", err)
}
c.Close()
}
return nil
}
Expand Down
58 changes: 58 additions & 0 deletions test/end2end_test.go
Expand Up @@ -7990,3 +7990,61 @@ func (s) TestAuthorityHeader(t *testing.T) {
})
}
}

// wrapCloseListener tracks Accepts/Closes and maintains a counter of the
// number of open connections.
type wrapCloseListener struct {
net.Listener
connsOpen int32
}

// wrapCloseListener is returned by wrapCloseListener.Accept and decrements its
// connsOpen when Close is called.
type wrapCloseConn struct {
net.Conn
lis *wrapCloseListener
closeOnce sync.Once
}

func (w *wrapCloseListener) Accept() (net.Conn, error) {
conn, err := w.Listener.Accept()
if err != nil {
return nil, err
}
atomic.AddInt32(&w.connsOpen, 1)
return &wrapCloseConn{Conn: conn, lis: w}, nil
}

func (w *wrapCloseConn) Close() error {
defer w.closeOnce.Do(func() { atomic.AddInt32(&w.lis.connsOpen, -1) })
return w.Conn.Close()
}

// TestServerClosesConn ensures conn.Close is always closed even if the client
// doesn't complete the HTTP/2 handshake.
func (s) TestServerClosesConn(t *testing.T) {
lis := bufconn.Listen(20)
wrapLis := &wrapCloseListener{Listener: lis}

s := grpc.NewServer()
go s.Serve(wrapLis)
defer s.Stop()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

for i := 0; i < 10; i++ {
conn, err := lis.DialContext(ctx)
if err != nil {
t.Fatalf("Dial = _, %v; want _, nil", err)
}
conn.Close()
}
for ctx.Err() == nil {
if atomic.LoadInt32(&wrapLis.connsOpen) == 0 {
return
}
time.Sleep(50 * time.Millisecond)
}
t.Fatalf("timed out waiting for conns to be closed by server; still open: %v", atomic.LoadInt32(&wrapLis.connsOpen))
}