I think,  this is what is happening.

I don't think this is a bug.

When server shuts down,  it asks the kernel to release the resources it was 
holding.  One of the resources is port. 
Though the program closed the port (by the shutdown()),  but kernel still 
keeps it in TIME_WAIT state.  Now if we try to use the same
port again (ListenAndServe()),  we'll get address already in use error.  
So,  this should be an expected error.

> If it's the latter, how can I safely make sure that Shutdown() completely 
frees up the port used
I don't think this is possible other than some retry hack.  Though,  i 
would like to call in some expert :)

On Wednesday, 29 November 2017 10:23:48 UTC+5:30, Albert Tedja wrote:
>
> net/http's Shutdown() does not free up the port upon return, or rather it 
> seems a bit undefined how long it takes for the port to be reusable again.
>
>     server := &http.Server{
>         Addr: fmt.Sprintf(":9000"),
>     }
>     go func() {
>         fmt.Println("hosting...")
>         err := server.ListenAndServe()
>     }()
>     time.Sleep(1 * time.Second)
>
>     fmt.Println("closing")
>     err := server.Shutdown(nil)
>     fmt.Println("shutdown error", err)
>
>     fmt.Println("hosting again...")
>     err = server.ListenAndServe()
>     fmt.Println("host again err", err)
>
>
>
> The code above, for example, sometimes successfully hosting the http 
> server twice, but sometimes, the second one fails with "address already in 
> use" error
>
> $ go run main.go 
> hosting...
> closing
> shutdown error <nil>
> hosting again...
> // This is okay
>
> $ go run main.go 
> hosting...
> closing
> shutdown error <nil>
> hosting again...
> host again err listen tcp :9000: bind: address already in use
>
> My question is, is this a bug, or an expected undetermined behavior? If 
> it's the latter, how can I safely make sure that Shutdown() completely 
> frees up the port used?
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to