mirror of
https://github.com/golang/go.git
synced 2025-05-26 01:41:25 +00:00
net: avoid racing on port reuse in TestListenConfigControl
Fixes #52798. Fixes #51441 (until proven otherwise 😅). Change-Id: Ic1eadebd0d41c5cbe37340190f8b2bde4b6c5673 Reviewed-on: https://go-review.googlesource.com/c/go/+/405214 Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
0d410d676d
commit
4861475c1a
@ -7,7 +7,6 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"internal/testenv"
|
||||
"os"
|
||||
@ -735,17 +734,7 @@ func TestListenConfigControl(t *testing.T) {
|
||||
if !testableNetwork(network) {
|
||||
continue
|
||||
}
|
||||
ln := newLocalListener(t, network)
|
||||
address := ln.Addr().String()
|
||||
// TODO: This is racy. The selected address could be reused in between
|
||||
// this Close and the subsequent Listen.
|
||||
ln.Close()
|
||||
lc := ListenConfig{Control: controlOnConnSetup}
|
||||
ln, err := lc.Listen(context.Background(), network, address)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
continue
|
||||
}
|
||||
ln := newLocalListener(t, network, &ListenConfig{Control: controlOnConnSetup})
|
||||
ln.Close()
|
||||
}
|
||||
})
|
||||
@ -754,24 +743,8 @@ func TestListenConfigControl(t *testing.T) {
|
||||
if !testableNetwork(network) {
|
||||
continue
|
||||
}
|
||||
c := newLocalPacketListener(t, network)
|
||||
address := c.LocalAddr().String()
|
||||
// TODO: This is racy. The selected address could be reused in between
|
||||
// this Close and the subsequent ListenPacket.
|
||||
c := newLocalPacketListener(t, network, &ListenConfig{Control: controlOnConnSetup})
|
||||
c.Close()
|
||||
if network == "unixgram" {
|
||||
os.Remove(address)
|
||||
}
|
||||
lc := ListenConfig{Control: controlOnConnSetup}
|
||||
c, err := lc.ListenPacket(context.Background(), network, address)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
continue
|
||||
}
|
||||
c.Close()
|
||||
if network == "unixgram" {
|
||||
os.Remove(address)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -33,9 +34,20 @@ func testUnixAddr(t testing.TB) string {
|
||||
return filepath.Join(d, "sock")
|
||||
}
|
||||
|
||||
func newLocalListener(t testing.TB, network string) Listener {
|
||||
func newLocalListener(t testing.TB, network string, lcOpt ...*ListenConfig) Listener {
|
||||
var lc *ListenConfig
|
||||
switch len(lcOpt) {
|
||||
case 0:
|
||||
lc = new(ListenConfig)
|
||||
case 1:
|
||||
lc = lcOpt[0]
|
||||
default:
|
||||
t.Helper()
|
||||
t.Fatal("too many ListenConfigs passed to newLocalListener: want 0 or 1")
|
||||
}
|
||||
|
||||
listen := func(net, addr string) Listener {
|
||||
ln, err := Listen(net, addr)
|
||||
ln, err := lc.Listen(context.Background(), net, addr)
|
||||
if err != nil {
|
||||
t.Helper()
|
||||
t.Fatal(err)
|
||||
@ -306,9 +318,20 @@ func transceiver(c Conn, wb []byte, ch chan<- error) {
|
||||
}
|
||||
}
|
||||
|
||||
func newLocalPacketListener(t testing.TB, network string) PacketConn {
|
||||
func newLocalPacketListener(t testing.TB, network string, lcOpt ...*ListenConfig) PacketConn {
|
||||
var lc *ListenConfig
|
||||
switch len(lcOpt) {
|
||||
case 0:
|
||||
lc = new(ListenConfig)
|
||||
case 1:
|
||||
lc = lcOpt[0]
|
||||
default:
|
||||
t.Helper()
|
||||
t.Fatal("too many ListenConfigs passed to newLocalListener: want 0 or 1")
|
||||
}
|
||||
|
||||
listenPacket := func(net, addr string) PacketConn {
|
||||
c, err := ListenPacket(net, addr)
|
||||
c, err := lc.ListenPacket(context.Background(), net, addr)
|
||||
if err != nil {
|
||||
t.Helper()
|
||||
t.Fatal(err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user