]> Cypherpunks repositories - gostls13.git/commitdiff
net: avoid racing on port reuse in TestListenConfigControl
authorBryan C. Mills <bcmills@google.com>
Mon, 9 May 2022 16:51:28 +0000 (12:51 -0400)
committerGopher Robot <gobot@golang.org>
Mon, 9 May 2022 18:10:01 +0000 (18:10 +0000)
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>
src/net/listen_test.go
src/net/mockserver_test.go

index 59c0112122babe68d5e5af7d31d82f154429e43b..df3cadfa1a9ca6d5fb4882d9cabb1af2124c4f8e 100644 (file)
@@ -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.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 := newLocalPacketListener(t, network, &ListenConfig{Control: controlOnConnSetup})
                        c.Close()
-                       if network == "unixgram" {
-                               os.Remove(address)
-                       }
                }
        })
 }
index 186bd330b2c20933228fba0fb025c2a23b13de28..61c17530c27ef2958bde6b95549f4941cdf8fac6 100644 (file)
@@ -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)