Skip to content

net/http: server idleTimeout documentation issue  #20383

@prabrisat1

Description

@prabrisat1

What version of Go are you using (go version)?

go version go1.8 darwin/amd64

What operating system and processor architecture are you using (go env)?

not really relevant but listed below:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/puvvadip/Documents/work"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4r/1g2c4css78b1yn2q32dmqqm0l05w9h/T/go-build790682604=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

Issue

net/http's server documentation states

 // IdleTimeout is the maximum amount of time to wait for the
 // next request when keep-alives are enabled. If IdleTimeout
 // is zero, the value of ReadTimeout is used. If both are
 // zero, there is no timeout.
 IdleTimeout time.Duration

However, if no conn ReadDeadline is set because idleTimeout is zero at the bottom of the main loop of net/http.(*conn).serve (the relevant code is below)

    if d := c.server.idleTimeout(); d != 0 {
        c.rwc.SetReadDeadline(time.Now().Add(d))
        if _, err := c.bufr.Peek(4); err != nil {
            return
        }
    }
    c.rwc.SetReadDeadline(time.Time{})

Then the next iteration of the loop starts with

   w, err := c.readRequest(ctx)

Which starts with

  func (c *conn) readRequest(ctx context.Context) (w *response, err error) {
  	if c.hijacked() {
  		return nil, ErrHijacked
  	}
  
  	var (
  		wholeReqDeadline time.Time // or zero if none
  		hdrDeadline      time.Time // or zero if none
  	)
  	t0 := time.Now()
  	if d := c.server.readHeaderTimeout(); d != 0 {
  		hdrDeadline = t0.Add(d)
  	}
  	if d := c.server.ReadTimeout; d != 0 {
  		wholeReqDeadline = t0.Add(d)
  	}
  	c.rwc.SetReadDeadline(hdrDeadline)

Therefore in the case where both idleTimeout and readTimeout are not set, the readHeaderTimeout is used. The documentation for idleTimeout should state, “If both are zero, ReadHeaderTimeout is used.”

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions