Skip to content

Commit

Permalink
server: try avoid the "address already in use" error (#8842)
Browse files Browse the repository at this point in the history
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: phayes/freeport#8
Reference: #6671

Co-authored-by: Amaury <[email protected]>
  • Loading branch information
2 people authored and daeMOn63 committed Aug 18, 2021
1 parent 63852a9 commit 8f74ae2
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions server/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8f74ae2

Please sign in to comment.