Skip to content

Commit

Permalink
net/http: lock the read-only mutex in shouldRedirect
Browse files Browse the repository at this point in the history
Since that method uses 'mux.m', we need to lock the mutex to avoid data races.

Change-Id: I998448a6e482b5d6a1b24f3354bb824906e23172
GitHub-Last-Rev: 163a7d4
GitHub-Pull-Request: #23994
Reviewed-on: https://go-review.googlesource.com/96575
Reviewed-by: Brad Fitzpatrick <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
  • Loading branch information
dmathieu authored and bradfitz committed Mar 2, 2018
1 parent 1c9297c commit 2fd1b52
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2fd1b52

Please sign in to comment.