Skip to content

Commit

Permalink
ipv4,ipv6: retry ENOBUFS and shut down the PacketConn on failure in T…
Browse files Browse the repository at this point in the history
…estPacketConnConcurrentReadWriteUnicastUDP

This ports CL 376094 and CL 376095 to the UDP variants of the test.

Fixes golang/go#52549 (hopefully).

Change-Id: I2537af1bc14a42b2e51882e5d646912e1239758c
Reviewed-on: https://go-review.googlesource.com/c/net/+/402059
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>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Apr 25, 2022
1 parent 1d1ef93 commit 2871e0c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 26 deletions.
50 changes: 37 additions & 13 deletions ipv4/readwrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,31 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
t.Fatal(err)
}

var firstError sync.Once
fatalf := func(format string, args ...interface{}) {
// On the first error, close the PacketConn to unblock the remaining
// goroutines. Suppress any further errors, which may occur simply due to
// closing the PacketConn.
first := false
firstError.Do(func() {
first = true
p.Close()
})
if first {
t.Helper()
t.Errorf(format, args...)
}
runtime.Goexit()
}

var wg sync.WaitGroup
reader := func() {
defer wg.Done()
rb := make([]byte, 128)
if n, cm, _, err := p.ReadFrom(rb); err != nil {
t.Error(err)
return
fatalf("%v", err)
} else if !bytes.Equal(rb[:n], wb) {
t.Errorf("got %v; want %v", rb[:n], wb)
return
fatalf("got %v; want %v", rb[:n], wb)
} else {
s := cm.String()
if strings.Contains(s, ",") {
Expand All @@ -270,15 +285,24 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
cm.IfIndex = ifi.Index
}
if err := p.SetControlMessage(cf, toggle); err != nil {
t.Error(err)
return
}
if n, err := p.WriteTo(wb, &cm, dst); err != nil {
t.Error(err)
return
} else if n != len(wb) {
t.Errorf("got %d; want %d", n, len(wb))
return
fatalf("%v", err)
}

backoff := time.Millisecond
for {
n, err := p.WriteTo(wb, &cm, dst)
if err != nil {
if n == 0 && isENOBUFS(err) {
time.Sleep(backoff)
backoff *= 2
continue
}
fatalf("%v", err)
}
if n != len(wb) {
fatalf("got %d; want %d", n, len(wb))
}
break
}
}

Expand Down
50 changes: 37 additions & 13 deletions ipv6/readwrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,31 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
t.Fatal(err)
}

var firstError sync.Once
fatalf := func(format string, args ...interface{}) {
// On the first error, close the PacketConn to unblock the remaining
// goroutines. Suppress any further errors, which may occur simply due to
// closing the PacketConn.
first := false
firstError.Do(func() {
first = true
p.Close()
})
if first {
t.Helper()
t.Errorf(format, args...)
}
runtime.Goexit()
}

var wg sync.WaitGroup
reader := func() {
defer wg.Done()
rb := make([]byte, 128)
if n, cm, _, err := p.ReadFrom(rb); err != nil {
t.Error(err)
return
fatalf("%v", err)
} else if !bytes.Equal(rb[:n], wb) {
t.Errorf("got %v; want %v", rb[:n], wb)
return
fatalf("got %v; want %v", rb[:n], wb)
} else {
s := cm.String()
if strings.Contains(s, ",") {
Expand All @@ -274,15 +289,24 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
cm.IfIndex = ifi.Index
}
if err := p.SetControlMessage(cf, toggle); err != nil {
t.Error(err)
return
}
if n, err := p.WriteTo(wb, &cm, dst); err != nil {
t.Error(err)
return
} else if n != len(wb) {
t.Errorf("got %d; want %d", n, len(wb))
return
fatalf("%v", err)
}

backoff := time.Millisecond
for {
n, err := p.WriteTo(wb, &cm, dst)
if err != nil {
if n == 0 && isENOBUFS(err) {
time.Sleep(backoff)
backoff *= 2
continue
}
fatalf("%v", err)
}
if n != len(wb) {
fatalf("got %d; want %d", n, len(wb))
}
break
}
}

Expand Down

0 comments on commit 2871e0c

Please sign in to comment.