-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: Server negative timeouts are accepted and break silently #39177
Comments
For an isolated and runnable repro, please see https://play.golang.org/p/uD5-UNDhTyN or inlined below package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"time"
)
func main() {
cst := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, issue!"))
}))
cst.Config.WriteTimeout = -time.Second
cst.Config.ReadTimeout = -time.Second
cst.Start()
defer cst.Close()
for i := 0; i < 10; i++ {
res, err := cst.Client().Get(cst.URL)
if err != nil {
panic(err)
}
_, _ = ioutil.ReadAll(res.Body)
res.Body.Close()
}
} |
I'm not sure if setting a negative value for those fields has any practical use. Perhaps testing? I could see documentation being enough for this but it's also a small code change to not add negative values to the timeouts. |
I don't see any use of negative values there either. For testing very small, positive values could achieve the same result. I believe not accepting negative values (or setting them to zero) would make |
Change https://golang.org/cl/235437 mentions this issue: |
I believe the current behavior is consistent with the documentation: A negative timeout gives less than no time in which to perform the operation. This isn't useful, but a 1ns timeout isn't really useful either. I don't think there's any need to change the package behavior or documentation with regard to negative timeouts. I do note that there is no documentation of what a zero |
…r fails without any error log. Issue:golang/go#39177
Sometimes we use negative values to mean "disabled, no timeout" when 0 means "pick some default timeout value for me". I'm fine with making negative values an error or disabled. I agree we should say what 0 means for ReadTimeout. |
I believe we should say what 0 means for both of them (ReadTimeout and WriteTimeout). Making negative values an error or disabled are both fine for me. However, I believe that making negative values an error is "safer" in the sense that it's easier to spot errors. What lead me to open this issue was an overflow of time.Duration (int64) that make the timeouts negative without nobody noticing it. |
@belimawr given that negative values already have a well-defined behavior, that behavior arguably cannot be changed due to the Go 1 compatibility policy. We can certainly improve the documentation, though. |
Arguably the defect there lies with |
I am not completely sure whether it is a bug, an expected behaviour or just a lack of documentation, however, I believe that this behaviour should at least be documented, ideally have the application explicitly failing.
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?
Started an HTTP server with negative values for
WriteTimeout
andReadTimeout
, then executed requests to this HTTP server.Here is a super simple code that reproduces the issue:
What did you expect to see?
One of:
net/http.Server
explaining the behaviour when the timeout is negativeWhat did you see instead?
Nothing on application side, for any caller the only kind of error we see is a "connection reset by peer".
The text was updated successfully, but these errors were encountered: