Skip to content

Commit

Permalink
We cannot avoid continuing to call SetReadDeadline and similar method…
Browse files Browse the repository at this point in the history
…s after the connection is closed. (#1835)

In the current implementation, there are several places where it's assumed that SetReadDeadline and similar functions logically won't affect a closed connection. However, these assumptions may not hold in certain specific situations. Therefore, instead of asserting that such errors will never occur, simply return the errors encountered during the execution of these methods.
  • Loading branch information
newacorn authored Aug 21, 2024
1 parent d29a2b9 commit 43c7b83
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1597,14 +1597,14 @@ func (s *Server) NextProto(key string, nph ServeHandler) {
func (s *Server) getNextProto(c net.Conn) (proto string, err error) {
if tlsConn, ok := c.(connTLSer); ok {
if s.ReadTimeout > 0 {
if err := c.SetReadDeadline(time.Now().Add(s.ReadTimeout)); err != nil {
panic(fmt.Sprintf("BUG: error in SetReadDeadline(%v): %v", s.ReadTimeout, err))
if err = c.SetReadDeadline(time.Now().Add(s.ReadTimeout)); err != nil {
return
}
}

if s.WriteTimeout > 0 {
if err := c.SetWriteDeadline(time.Now().Add(s.WriteTimeout)); err != nil {
panic(fmt.Sprintf("BUG: error in SetWriteDeadline(%v): %v", s.WriteTimeout, err))
if err = c.SetWriteDeadline(time.Now().Add(s.WriteTimeout)); err != nil {
return
}
}

Expand Down Expand Up @@ -2137,8 +2137,8 @@ func (s *Server) serveConn(c net.Conn) (err error) {
// 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): %v", err))
if err = c.SetDeadline(zeroTime); err != nil {
return
}
}

Expand Down Expand Up @@ -2177,7 +2177,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
// If this is a keep-alive connection set the idle timeout.
if connRequestNum > 1 {
if d := s.idleTimeout(); d > 0 {
if err := c.SetReadDeadline(time.Now().Add(d)); err != nil {
if err = c.SetReadDeadline(time.Now().Add(d)); err != nil {
break
}
}
Expand Down Expand Up @@ -2221,13 +2221,13 @@ func (s *Server) serveConn(c net.Conn) (err error) {
s.setState(c, StateActive)

if s.ReadTimeout > 0 {
if err := c.SetReadDeadline(time.Now().Add(s.ReadTimeout)); err != nil {
if err = c.SetReadDeadline(time.Now().Add(s.ReadTimeout)); err != nil {
break
}
} 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 {
if err = c.SetReadDeadline(zeroTime); err != nil {
break
}
}
Expand Down Expand Up @@ -2261,8 +2261,8 @@ func (s *Server) serveConn(c net.Conn) (err error) {
reqConf := onHdrRecv(&ctx.Request.Header)
if reqConf.ReadTimeout > 0 {
deadline := time.Now().Add(reqConf.ReadTimeout)
if err := c.SetReadDeadline(deadline); err != nil {
panic(fmt.Sprintf("BUG: error in SetReadDeadline(%v): %v", deadline, err))
if err = c.SetReadDeadline(deadline); err != nil {
break
}
}
switch {
Expand Down Expand Up @@ -2401,14 +2401,14 @@ func (s *Server) serveConn(c net.Conn) (err error) {
ctx.hijackNoResponse = false

if writeTimeout > 0 {
if err := c.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
panic(fmt.Sprintf("BUG: error in SetWriteDeadline(%v): %v", writeTimeout, err))
if err = c.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
break
}
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): %v", err))
if err = c.SetWriteDeadline(zeroTime); err != nil {
break
}
previousWriteTimeout = 0
}
Expand Down

0 comments on commit 43c7b83

Please sign in to comment.