server: try avoid the "address already in use" error (#8842)

Replace defer l.Close() with l.Close() to shutdown the listener
as fast as possible to avoid the "bind: address already in use"
error that so often occurs in CI build.

From: https://github.com/phayes/freeport/pull/8
Reference: #6671

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
This commit is contained in:
Alessio Treglia 2021-03-15 10:15:48 +00:00 committed by GitHub
parent a4b7f76110
commit d02a39739b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,16 +13,10 @@ func FreeTCPAddr() (addr, port string, err error) {
return "", "", err
}
closer := func() {
err := l.Close()
if err != nil {
// TODO: Handle with #870
panic(err)
}
if err := l.Close(); err != nil {
return "", "", fmt.Errorf("couldn't close the listener: %w", err)
}
defer closer()
portI := l.Addr().(*net.TCPAddr).Port
port = fmt.Sprintf("%d", portI)
addr = fmt.Sprintf("tcp://0.0.0.0:%s", port)