Skip to content

net/http: Server BaseContext & ConnContext not respected for HTTP2 #32476

Closed
@tmthrgd

Description

@tmthrgd

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

$ go version
go version devel +eb2fabf2fa Fri Jun 7 13:56:20 2019 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tom/.cache/go-build"
GOENV="/home/tom/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/tom/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/tom/go/gotip-eb2fabf2fa"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/tom/go/gotip-eb2fabf2fa/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/tom/go/gotip-eb2fabf2fa/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build584953743=/tmp/go-build -gno-record-gcc-switches"

What did you do?

CL 167681 (for #30694) introduced the BaseContext & ConnContext fields on http.Server, but they were never wired up in x/net/http2.

The following is a slight modification of the TestServerContexts test in serve_test.go that uses HTTP2:

func TestServerContextsHTTP2(t *testing.T) {
	setParallel(t)
	defer afterTest(t)
	type baseKey struct{}
	type connKey struct{}
	ch := make(chan context.Context, 1)
	ts := httptest.NewUnstartedServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
		if r.ProtoMajor != 2 {
			t.Errorf("unexpected HTTP/1.x request")
		}
		ch <- r.Context()
	}))
	ts.Config.BaseContext = func(ln net.Listener) context.Context {
		if strings.Contains(reflect.TypeOf(ln).String(), "onceClose") {
			t.Errorf("unexpected onceClose listener type %T", ln)
		}
		return context.WithValue(context.Background(), baseKey{}, "base")
	}
	ts.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context {
		if got, want := ctx.Value(baseKey{}), "base"; got != want {
			t.Errorf("in ConnContext, base context key = %#v; want %q", got, want)
		}
		return context.WithValue(ctx, connKey{}, "conn")
	}
	ts.TLS = &tls.Config{
		NextProtos: []string{"h2", "http/1.1"},
	}
	ts.StartTLS()
	defer ts.Close()
	ts.Client().Transport.(*Transport).ForceAttemptHTTP2 = true
	res, err := ts.Client().Get(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	res.Body.Close()
	ctx := <-ch
	if got, want := ctx.Value(baseKey{}), "base"; got != want {
		t.Errorf("base context key = %#v; want %q", got, want)
	}
	if got, want := ctx.Value(connKey{}), "conn"; got != want {
		t.Errorf("conn context key = %#v; want %q", got, want)
	}
}

Then go test:

$ go test -v -run TestServerContexts net/http

What did you expect to see?

=== RUN   TestServerContexts
--- PASS: TestServerContexts (0.00s)
=== RUN   TestServerContextsHTTP2
--- PASS: TestServerContextsHTTP2 (0.00s)
PASS
ok  	net/http	0.005s

What did you see instead?

=== RUN   TestServerContexts
--- PASS: TestServerContexts (0.00s)
=== RUN   TestServerContextsHTTP2
--- FAIL: TestServerContextsHTTP2 (0.00s)
    serve_test.go:6106: base context key = <nil>; want "base"
    serve_test.go:6109: conn context key = <nil>; want "conn"
FAIL
FAIL	net/http	0.005s

x/net/http2 doesn't use the http.Server context–as it isn't provided to the TLSNextProto handler–instead creating it's own in serverConnBaseContext. This code currently lacks the BaseContext & ConnContext calls.

Also while I'm here, the two panics introduced by CL 167681 (here and here) should have a http: prefix.

/cc @bradfitz

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions