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