Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various deadline fixes #1081

Merged
merged 1 commit into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,14 @@ func (s *Server) serveConn(c net.Conn) (err error) {
return
}
if handler, ok := s.nextProtos[proto]; ok {
// Remove read or write deadlines that might have previously been set.
// The next handler is responsible for setting its own deadlines.
if s.ReadTimeout > 0 || s.WriteTimeout > 0 {
if err := c.SetDeadline(zeroTime); err != nil {
panic(fmt.Sprintf("BUG: error in SetDeadline(zeroTime): %s", err))
}
}

return handler(c)
}

Expand All @@ -2029,6 +2037,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
maxRequestBodySize = DefaultMaxRequestBodySize
}
writeTimeout := s.WriteTimeout
previousWriteTimeout := time.Duration(0)

ctx := s.acquireCtx(c)
ctx.connTime = connTime
Expand Down Expand Up @@ -2098,6 +2107,12 @@ func (s *Server) serveConn(c net.Conn) (err error) {
if err := c.SetReadDeadline(time.Now().Add(s.ReadTimeout)); err != nil {
panic(fmt.Sprintf("BUG: error in SetReadDeadline(%s): %s", s.ReadTimeout, err))
}
} else if s.IdleTimeout > 0 && connRequestNum > 1 {
// If this was an idle connection and the server has an IdleTimeout but
// no ReadTimeout then we should remove the ReadTimeout.
if err := c.SetReadDeadline(zeroTime); err != nil {
panic(fmt.Sprintf("BUG: error in SetReadDeadline(zeroTime): %s", err))
}
}
if s.DisableHeaderNamesNormalizing {
ctx.Request.Header.DisableNormalizing()
Expand Down Expand Up @@ -2277,6 +2292,13 @@ func (s *Server) serveConn(c net.Conn) (err error) {
if err := c.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
panic(fmt.Sprintf("BUG: error in SetWriteDeadline(%s): %s", writeTimeout, err))
}
previousWriteTimeout = writeTimeout
} else if previousWriteTimeout > 0 {
// We don't want a write timeout but we previously set one, remove it.
if err := c.SetWriteDeadline(zeroTime); err != nil {
panic(fmt.Sprintf("BUG: error in SetWriteDeadline(zeroTime): %s", err))
}
previousWriteTimeout = 0
}

connectionClose = connectionClose || ctx.Response.ConnectionClose()
Expand Down Expand Up @@ -2339,11 +2361,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
releaseWriter(s, bw)
bw = nil
}
err = c.SetReadDeadline(zeroTime)
if err != nil {
break
}
err = c.SetWriteDeadline(zeroTime)
err = c.SetDeadline(zeroTime)
if err != nil {
break
}
Expand Down
4 changes: 4 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3964,6 +3964,10 @@ func (rw *readWriter) LocalAddr() net.Addr {
return zeroTCPAddr
}

func (rw *readWriter) SetDeadline(t time.Time) error {
return nil
}

func (rw *readWriter) SetReadDeadline(t time.Time) error {
return nil
}
Expand Down