Skip to content

Commit

Permalink
http2: avoid race in server handler SetReadDeadine/SetWriteDeadline
Browse files Browse the repository at this point in the history
Can't safely access responseWriter.rws from on the server's serve loop.

Change-Id: I477abe58cf9dd23813a0c5507aed2319696fdfaf
Reviewed-on: https://go-review.googlesource.com/c/net/+/589856
Reviewed-by: Brad Fitzpatrick <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
Auto-Submit: Damien Neil <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
neild authored and gopherbot committed Jun 3, 2024
1 parent 603e3e6 commit 6249541
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions http2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2821,9 +2821,9 @@ func (w *responseWriter) SetReadDeadline(deadline time.Time) error {
if deadline.IsZero() {
st.readDeadline = nil
} else if st.readDeadline == nil {
st.readDeadline = sc.srv.afterFunc(deadline.Sub(w.rws.conn.srv.now()), st.onReadTimeout)
st.readDeadline = sc.srv.afterFunc(deadline.Sub(sc.srv.now()), st.onReadTimeout)
} else {
st.readDeadline.Reset(deadline.Sub(w.rws.conn.srv.now()))
st.readDeadline.Reset(deadline.Sub(sc.srv.now()))
}
})
return nil
Expand All @@ -2847,9 +2847,9 @@ func (w *responseWriter) SetWriteDeadline(deadline time.Time) error {
if deadline.IsZero() {
st.writeDeadline = nil
} else if st.writeDeadline == nil {
st.writeDeadline = sc.srv.afterFunc(deadline.Sub(w.rws.conn.srv.now()), st.onWriteTimeout)
st.writeDeadline = sc.srv.afterFunc(deadline.Sub(sc.srv.now()), st.onWriteTimeout)
} else {
st.writeDeadline.Reset(deadline.Sub(w.rws.conn.srv.now()))
st.writeDeadline.Reset(deadline.Sub(sc.srv.now()))
}
})
return nil
Expand Down
13 changes: 13 additions & 0 deletions http2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4661,3 +4661,16 @@ func TestServerRequestCancelOnError(t *testing.T) {
})
<-donec
}

func TestServerSetReadWriteDeadlineRace(t *testing.T) {
ts := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {
ctl := http.NewResponseController(w)
ctl.SetReadDeadline(time.Now().Add(3600 * time.Second))
ctl.SetWriteDeadline(time.Now().Add(3600 * time.Second))
})
resp, err := ts.Client().Get(ts.URL)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}

0 comments on commit 6249541

Please sign in to comment.