diff options
author | Damien Mathieu <42@dmathieu.com> | 2018-02-22 08:17:15 +0000 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-08-22 02:03:14 +0000 |
commit | ba5e308ea9496e4be0bd8c59a5291359e0ea98b4 (patch) | |
tree | 1cbc584b13ee6222a5a2f358319bc1b4e5617251 | |
parent | 4d6ae73dfc5f0856e58aca2a892f27b2ab80b148 (diff) | |
download | go-ba5e308ea9496e4be0bd8c59a5291359e0ea98b4.tar.gz go-ba5e308ea9496e4be0bd8c59a5291359e0ea98b4.zip |
[release-branch.go1.10] 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.
Fixes #27129
Change-Id: I998448a6e482b5d6a1b24f3354bb824906e23172
Reviewed-on: https://go-review.googlesource.com/95775
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
-rw-r--r-- | src/net/http/serve_test.go | 10 | ||||
-rw-r--r-- | src/net/http/server.go | 3 |
2 files changed, 13 insertions, 0 deletions
diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 9cbfe872af..1df319ac1f 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 57e1b5dacb..88e00a2edd 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -2233,6 +2233,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 { |