net/http: fix data race in TestServerNoWriteTimeout/h2

In TestServerNoWriteTimeout, shutting down the server seems insufficient
to guarantee that the server handler is done by the time the test exits
for HTTP/2. As the test shuts down, t.done is written within
testing.tRunner, while the server handler goroutine simultaneously reads
t.done while executing t.Logf.

For #79409

Change-Id: I036ed8a4f47de7ed1bbfeeb482bb562c6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/779120
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Nicholas Husin <husin@google.com>
This commit is contained in:
Nicholas S. Husin 2026-05-17 23:49:24 -04:00 committed by Nicholas Husin
parent 71c7ea1c6c
commit 2e67b18935

View file

@ -971,7 +971,9 @@ func testServerWriteTimeout(t *testing.T, mode testMode) {
func TestServerNoWriteTimeout(t *testing.T) { run(t, testServerNoWriteTimeout) }
func testServerNoWriteTimeout(t *testing.T, mode testMode) {
for _, timeout := range []time.Duration{0, -1} {
handlerDone := make(chan struct{})
cst := newClientServerTest(t, mode, HandlerFunc(func(res ResponseWriter, req *Request) {
defer close(handlerDone)
_, err := io.Copy(res, neverEnding('a'))
t.Logf("server write response: %v", err)
}), func(ts *httptest.Server) {
@ -983,15 +985,14 @@ func testServerNoWriteTimeout(t *testing.T, mode testMode) {
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
n, err := io.CopyN(io.Discard, res.Body, 1<<20) // 1MB should be sufficient to prove the point
if n != 1<<20 || err != nil {
t.Errorf("client read response body: %d, %v", n, err)
}
// This shutdown really should be automatic, but it isn't right now.
// Shutdown (rather than Close) ensures the handler is done before we return.
res.Body.Close()
// This shutdown really should be automatic, but it isn't right now.
cst.ts.Config.Shutdown(context.Background())
<-handlerDone
}
}