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.
This commit is contained in:
Damien Mathieu 2018-02-21 11:32:23 +01:00
parent f6c6781302
commit 163a7d4942
2 changed files with 13 additions and 0 deletions

View 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 {

View File

@ -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 {