Description
What version of Go are you using (go version
)?
1.8.1
What operating system and processor architecture are you using (go env
)?
darwin amd64, but this is a general issue I also encounter in docker containers
What did you do?
I'm trying to reliably gracefully shut down an http Server: https://play.golang.org/p/ESmgJ6O_WU
However, because ListenAndServe()
blocks while the Server is running, there is no way to ensure its call happens before a call to Shutdown()
, so Shutdown()
may be called before ListenAndServe()
, in which case it returns nil
and the later call to ListenAndServe()
still starts a server that is then never shut down.
What did you expect to see?
I expect to have some way of knowing that the server is now up and running such that Shutdown()
will stop it, short of sending it http requests. Maybe an interface closer to httptest.Server
.
What did you see instead?
It seems I either need to repeatedly do http requests to the server to ensure it is up before calling Shutdown()
or repeatedly call Shutdown()
until ListenAndServe()
returns, like this: https://play.golang.org/p/LPfpz0LaBY
Note that due to the pre-emptive nature of Goroutines, since Shutdown()
can never be called on the same Goroutine as ListenAndServe()
, this theoretically affects all programs using Shutdown()
without a loop.