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
net/http: ReadHeaderTimeout didn't work proper with KeepAlive enabled #47007
Comments
@molchalin Where did you set the IdleTimeout? |
IdleTimeout has default value, so i suppose that server will not close connection |
It has a default value of 0 which means no timeout. |
I've encountered the same issue with |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
Is it possible to reopen an issue here? I believe the documentation needs to be clarified that if IdleTimeout and ReadTimeout are not set, it commences reading the next header, and thus will close the connection after ReadHeaderTimeout. The way it reads now, it appears that if no IdleTimeout or ReadTimeout is set, the connection will be kept open until the remote side closes the connection, which is not true if ReadHeaderTimeout is set. This is the relevant section of documentation: https://pkg.go.dev/net/http#Server |
I found the same thing as @AusIV. I also found that this was fixed already in #20383, but then the fix was reverted in #32053. example: package main
import (
"fmt"
"io"
"net"
"net/http"
"time"
)
func main() {
t0 := time.Now()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
fmt.Println("listen", err)
return
}
fmt.Println("listening on", l.Addr())
go func() {
r, err := http.Get(fmt.Sprintf("http://%s/", l.Addr()))
if err != nil {
fmt.Println("request error", err)
return
}
_, _ = io.Copy(io.Discard, r.Body)
}()
shutdown := make(chan struct{})
srv := http.Server{
ReadHeaderTimeout: 2 * time.Second,
ConnState: func(conn net.Conn, state http.ConnState) {
fmt.Println(time.Since(t0), conn.RemoteAddr(), "ConnState", state)
if state == http.StateClosed {
close(shutdown)
}
},
}
go func() {
<-shutdown
srv.Close()
}()
if err := srv.Serve(l); err != http.ErrServerClosed {
fmt.Println("serve", err)
}
} example output:
Note the connection was closed 2 seconds after becoming idle (the value of
If we removed |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/8Za_9D3QqXC
I've sent http request with Keep Alive enabled(IdleConnTimeout = 90s) to server with ReadHeaderTimeout = 3s.
What did you expect to see?
no connection close
What did you see instead?
connection closed after 3s from last request
The text was updated successfully, but these errors were encountered: