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

net/http: imprecise description of the return values of HTTP serve functions #26267

Closed
yaojingguo opened this issue Jul 8, 2018 · 6 comments

Comments

Projects
None yet
5 participants
@yaojingguo
Copy link
Contributor

commented Jul 8, 2018

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

go version go1.10.3 darwin/amd64

In net.http package, there are following fucntions/methods related to serve HTTP:

  1. http.Serve
  2. http.ListenAndServe
  3. http.ListenAndServeTLS
  4. http.Server.Serve
  5. http.Server.ListenAndServe
  6. http.Server.ListenAndServeTLS

Except the first one, all docs for those functiosn/methods say something like always returns a non-nil error. But if such a function/method is terminated by a signal which is a common way to terminate a HTTP server, it will not return. For example, consider the following go code http.go:

package main

import (
	"log"
	"net/http"
)

func main() {
	var srv http.Server
	srv.Addr = ":12345"

	if err := srv.ListenAndServe(); err != http.ErrServerClosed {
		log.Printf("http error: %s", err)
	}
}

A SIGINT signal will terminate the program and http.ListenAndServe will not return.

➜  local go run http.go
^Csignal: interrupt

Only if srv is teriminated by Server.Shutdown, srv.ListenAndServe will return.

I think that the descripion always returns a non-nil error should mention this behaviour. Otherwise, people will tend to write the following code:

	err = (&http.Server{Handler: rc.transport.Handler()}).Serve(ln)
	select {
	case <-rc.httpstopc:
	default:
		log.Fatalf("raftexample: Failed to serve rafthttp (%v)", err)
	}

They are hoping that Serve will return. But it will not since Shutdown is not used in a way descripted in Shutdown's example code.

@meirf

This comment has been minimized.

Copy link
Contributor

commented Jul 8, 2018

I think "always" is a description of the "non-nil" quality of the error as opposed to a description of the "return" of the error. In other words, any error that is returned is guaranteed to be non-nil. It is probably not meant to guarantee that the function returns - I don't know if we can guarantee that any function in the standard library returns.

But perhaps there's a way to remove this ambiguity in a similarly concise fashion.

@mvdan

This comment has been minimized.

Copy link
Member

commented Jul 8, 2018

Perhaps the sentences could be reworded, like:

The returned error will always be non-nil.

@agnivade

This comment has been minimized.

Copy link
Member

commented Jul 8, 2018

Yep, it just says that the returned error will always be non-nil. It does not say anything about the scenarios in which the function will return. Seems clear enough to me, but maybe we can be more explicit.

EDIT: @mvdan - our responses crossed :), I like your suggestion.

/cc @bradfitz

@gopherbot

This comment has been minimized.

Copy link

commented Jul 8, 2018

Change https://golang.org/cl/122465 mentions this issue: net/http: tweak "always returns" godoc wording

@mvdan mvdan changed the title documentation: inprecise description of the return value of HTTP serve functions net/http: imprecise description of the return values of HTTP serve functions Jul 8, 2018

@mvdan

This comment has been minimized.

Copy link
Member

commented Jul 8, 2018

I've sent a small CL above with the proposed change.

More broadly, I've filed #26268 to look into all the other cases of "always returns X" in the standard library.

@mvdan mvdan added this to the Go1.12 milestone Jul 11, 2018

@mvdan

This comment has been minimized.

Copy link
Member

commented Aug 20, 2018

Closing, just like #26268. See Russ's point there. We didn't reach consensus, and it doesn't look like we found a wording that was considerably better.

@mvdan mvdan closed this Aug 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.