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

x/net/http2: call conn.Handshake before accessing ConnectionState #33304

sebsebmc opened this issue Jul 26, 2019 · 4 comments


Copy link

commented Jul 26, 2019

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

$ go version
go1.12.7 windows/amd64

Does this issue reproduce with the latest release?


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

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\schlopeki\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\schlopeki\go
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set GOMOD=C:\Users\schlopeki\Projects\doh\go.mod
set CGO_CFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\SCHLOP~1\AppData\Local\Temp\go-build767447546=/tmp/go-build -gno-record-gcc-switches

What did you do?

I attempted to make a simple http2 only client/server but the server rejects all connections as having "INADEQUATE_SECURITY, TLS version too low". When inspecting the TLS version connection claims to have a TLS version of 0 but in Wireshark I see that a TLS 1.2 negotiation has already started.

If I manually perform the TLS handshake the http2.Server.ServeConn no longer hangs up the connection.

Here is the minimal server:

package main

import (


func main() {
	cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
	if err != nil {
		panic("Cannot load cert")
	tlsCfg := &tls.Config{
		Certificates: []tls.Certificate{cert},
		NextProtos:   []string{http2.NextProtoTLS},

	srv := http2.Server{}
	srvOpt := &http2.ServeConnOpts{
		Handler:    http.HandlerFunc(handler),
		BaseConfig: &http.Server{TLSConfig: tlsCfg},
	listener, err := tls.Listen("tcp", "", tlsCfg)
	if err != nil {
		panic("Cant listen")

	for {
		conn, err := listener.Accept()
		if err != nil {
			panic("connection error")
		// Uncommenting the next 2 lines make the server work
		// tlsConn := conn.(*tls.Conn)
		// tlsConn.Handshake()

		go func() { srv.ServeConn(conn, srvOpt) }()

func handler(wrt http.ResponseWriter, req *http.Request) {
	if req.ProtoMajor != 2 {
		flusher, _ := wrt.(http.Flusher)
		log.Printf("Not HTTP2")
	flusher, _ := wrt.(http.Flusher)

Modern versions of Chrome/Firefox should be able to test this server. Testing server.crt and server.key need to be created and I used a host file to point test.local at the loopback address.

What did you expect to see?

The connection would have already completed the TLS handshake once listen.Accept returned it for use, or http2.Server.ServerConn would be able to see that the TLS version is at least negotiated.

What did you see instead?

When passing the connection directly from Accept to the http2.Server.ServeConn, the TLS state of the connection is not set. This results in ServeConn seeing a TLS version of 0 and therefore it sends GOAWAY and kills the connection.

@julieqiu julieqiu changed the title x/net/http2 crypto/tls Fails to complete TLS handshake before http2 server kills the connection crypto/tls: TLS handshake fails before http2 server kills the connection Jul 29, 2019


This comment has been minimized.

Copy link

commented Jul 29, 2019


This comment has been minimized.

Copy link

commented Jul 30, 2019

The TLS handshake doesn't fail as far as I can tell, it just isn't complete in the tls.Conn I get from Accept.

ServeConn looks at the TLS ConnectionState and checks the HandshakeComplete, which is false and then ServeConn has to drop the connection according to the spec because the TLS version doesn't appear to be TLS 1.2 or higher.

If it would help I can provide a packet capture as well.

@sebsebmc sebsebmc changed the title crypto/tls: TLS handshake fails before http2 server kills the connection crypto/tls: TLS handshake doesn't complete before http2 server kills the connection Aug 7, 2019


This comment has been minimized.

Copy link

commented Aug 23, 2019

I have the same issue. It seems it is not related to http2, but has something to do with how tls connections are established. Here is a simple server program to demonstrate the issue.

package main

import (

func main() {
	cert, _ := tls.X509KeyPair(certPEMBlock, keyPEMBlock)

	tlsConfig := tls.Config{
		Certificates: []tls.Certificate{cert},

	listener, _ := tls.Listen("tcp", ":8443", &tlsConfig)
	for {
		conn, _ := listener.Accept()
		defer conn.Close()
		log.Printf("server: accepted from %s", conn.RemoteAddr())

		tlsConn := *conn.(*tls.Conn)
		//If you uncomment the line below it works
		log.Printf("server: handshake complete: %t", tlsConn.ConnectionState().HandshakeComplete)

This comment has been minimized.

Copy link

commented Aug 23, 2019

No reads can happen in Accept(), as that would block the accept loop. That means the Conn returned by Accept is not initialized: HandshakeComplete is false, Version is unset.

What looks like a problem here is that ServeConn should know to run a Read or call Handshake before reaching into ConnectionState.

@FiloSottile FiloSottile changed the title crypto/tls: TLS handshake doesn't complete before http2 server kills the connection x/net/http2: call conn.Handshake before accessing ConnectionState Aug 23, 2019

@FiloSottile FiloSottile added this to the Go1.14 milestone Aug 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
5 participants
You can’t perform that action at this time.