From 2fd1b523952727b91e5eca76ff1f8b8af8746f7a Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Fri, 2 Mar 2018 08:42:02 +0000 Subject: [PATCH] net/http: lock the read-only mutex in shouldRedirect Since that method uses 'mux.m', we need to lock the mutex to avoid data races. Change-Id: I998448a6e482b5d6a1b24f3354bb824906e23172 GitHub-Last-Rev: 163a7d4942e793b328e05a7eb91f6d3fdc4ba12b GitHub-Pull-Request: golang/go#23994 Reviewed-on: https://go-review.googlesource.com/96575 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick --- src/net/http/serve_test.go | 10 ++++++++++ src/net/http/server.go | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 5ff08b4036aa7c..2fa3bc664fabfc 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -581,6 +581,16 @@ func TestServeWithSlashRedirectForHostPatterns(t *testing.T) { } } +func TestShouldRedirectConcurrency(t *testing.T) { + setParallel(t) + defer afterTest(t) + + mux := NewServeMux() + ts := httptest.NewServer(mux) + defer ts.Close() + mux.HandleFunc("/", func(w ResponseWriter, r *Request) {}) +} + func BenchmarkServeMux(b *testing.B) { type test struct { diff --git a/src/net/http/server.go b/src/net/http/server.go index c4377e8ce5db2e..a7ba753bf50812 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -2223,6 +2223,9 @@ func (mux *ServeMux) redirectToPathSlash(host, path string, u *url.URL) (*url.UR // path+"/". This should happen if a handler is registered for path+"/" but // not path -- see comments at ServeMux. func (mux *ServeMux) shouldRedirect(host, path string) bool { + mux.mu.RLock() + defer mux.mu.RUnlock() + p := []string{path, host + path} for _, c := range p {