]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.10] net/http: lock the read-only mutex in shouldRedirect
authorDamien Mathieu <42@dmathieu.com>
Thu, 22 Feb 2018 08:17:15 +0000 (08:17 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 22 Aug 2018 02:03:14 +0000 (02:03 +0000)
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>

src/net/http/serve_test.go
src/net/http/server.go

index 9cbfe872afe0bc53522e32bf95879e4e72f03252..1df319ac1fc571956654ee11f96bccb320a96d43 100644 (file)
@@ -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 {
index 57e1b5dacb3faf3dbecb5bbecba6d242580164d5..88e00a2eddc62efc7c83ec977e22eac98763cdf7 100644 (file)
@@ -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 {