From 516f0d1c906b841d33ecd185cda0257dfa7e2210 Mon Sep 17 00:00:00 2001 From: Daniel Morsing Date: Mon, 11 May 2015 10:31:24 +0100 Subject: [PATCH] net/http: silence race detector on client header timeout test When running the client header timeout test, there is a race between us timing out and waiting on the remaining requests to be serviced. If the client times out before the server blocks on the channel in the handler, we will be simultaneously adding to a waitgroup with the value 0 and waiting on it when we call TestServer.Close(). This is largely a theoretical race. We have to time out before we enter the handler and the only reason we would time out if we're blocked on the channel. Nevertheless, make the race detector happy by turning the close into a channel send. This turns the defer call into a synchronization point and we can be sure that we've entered the handler before we close the server. Fixes #10780 Change-Id: Id73b017d1eb7503e446aa51538712ef49f2f5c9e Reviewed-on: https://go-review.googlesource.com/9905 Reviewed-by: Brad Fitzpatrick --- src/net/http/client_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/net/http/client_test.go b/src/net/http/client_test.go index dc499a90b6..b1d8799fa5 100644 --- a/src/net/http/client_test.go +++ b/src/net/http/client_test.go @@ -927,7 +927,14 @@ func TestClientTimeout_Headers(t *testing.T) { <-donec })) defer ts.Close() - defer close(donec) + // Note that we use a channel send here and not a close. + // The race detector doesn't know that we're waiting for a timeout + // and thinks that the waitgroup inside httptest.Server is added to concurrently + // with us closing it. If we timed out immediately, we could close the testserver + // before we entered the handler. We're not timing out immediately and there's + // no way we would be done before we entered the handler, but the race detector + // doesn't know this, so synchronize explicitly. + defer func() { donec <- true }() c := &Client{Timeout: 500 * time.Millisecond} -- 2.48.1