Skip to content

Commit 2adcbb3

Browse files
WeiminShangBryan Mills
authored and
Bryan Mills
committed
ipv6: shut down the PacketConn on failure in TestPacketConnConcurrentReadWriteUnicast
This avoids a deadlock (observed in golang/go#50455) that may otherwise swallow the error logs from a failure, which may help us better diagnose the underlying problem. For golang/go#50455. Change-Id: Id73bd9589ae23385a433da0b24840ef945601f63 Reviewed-on: https://go-review.googlesource.com/c/net/+/376094 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 594f291 commit 2adcbb3

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

ipv6/readwrite_test.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,6 @@ func TestPacketConnConcurrentReadWriteUnicast(t *testing.T) {
356356
}
357357

358358
func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn, data []byte, dst net.Addr, batch bool) {
359-
t.Helper()
360-
361359
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
362360
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
363361

@@ -368,23 +366,37 @@ func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn,
368366
t.Fatal(err)
369367
}
370368

369+
var firstError sync.Once
370+
fatalf := func(format string, args ...interface{}) {
371+
// On the first error, close the PacketConn to unblock the remaining
372+
// goroutines. Suppress any further errors, which may occur simply due to
373+
// closing the PacketConn.
374+
first := false
375+
firstError.Do(func() {
376+
first = true
377+
p.Close()
378+
})
379+
if first {
380+
t.Helper()
381+
t.Errorf(format, args...)
382+
}
383+
runtime.Goexit()
384+
}
385+
371386
var wg sync.WaitGroup
372387
reader := func() {
373388
defer wg.Done()
374389
b := make([]byte, 128)
375390
n, cm, _, err := p.ReadFrom(b)
376391
if err != nil {
377-
t.Error(err)
378-
return
392+
fatalf("%v", err)
379393
}
380394
if !bytes.Equal(b[:n], data) {
381-
t.Errorf("got %#v; want %#v", b[:n], data)
382-
return
395+
fatalf("got %#v; want %#v", b[:n], data)
383396
}
384397
s := cm.String()
385398
if strings.Contains(s, ",") {
386-
t.Errorf("should be space-separated values: %s", s)
387-
return
399+
fatalf("should be space-separated values: %s", s)
388400
}
389401
}
390402
batchReader := func() {
@@ -397,27 +409,22 @@ func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn,
397409
}
398410
n, err := p.ReadBatch(ms, 0)
399411
if err != nil {
400-
t.Error(err)
401-
return
412+
fatalf("%v", err)
402413
}
403414
if n != len(ms) {
404-
t.Errorf("got %d; want %d", n, len(ms))
405-
return
415+
fatalf("got %d; want %d", n, len(ms))
406416
}
407417
var cm ipv6.ControlMessage
408418
if err := cm.Parse(ms[0].OOB[:ms[0].NN]); err != nil {
409-
t.Error(err)
410-
return
419+
fatalf("%v", err)
411420
}
412421
b := ms[0].Buffers[0][:ms[0].N]
413422
if !bytes.Equal(b, data) {
414-
t.Errorf("got %#v; want %#v", b, data)
415-
return
423+
fatalf("got %#v; want %#v", b, data)
416424
}
417425
s := cm.String()
418426
if strings.Contains(s, ",") {
419-
t.Errorf("should be space-separated values: %s", s)
420-
return
427+
fatalf("should be space-separated values: %s", s)
421428
}
422429
}
423430
writer := func(toggle bool) {
@@ -431,17 +438,14 @@ func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn,
431438
cm.IfIndex = ifi.Index
432439
}
433440
if err := p.SetControlMessage(cf, toggle); err != nil {
434-
t.Error(err)
435-
return
441+
fatalf("%v", err)
436442
}
437443
n, err := p.WriteTo(data, &cm, dst)
438444
if err != nil {
439-
t.Error(err)
440-
return
445+
fatalf("%v", err)
441446
}
442447
if n != len(data) {
443-
t.Errorf("got %d; want %d", n, len(data))
444-
return
448+
fatalf("got %d; want %d", n, len(data))
445449
}
446450
}
447451
batchWriter := func(toggle bool) {
@@ -455,8 +459,7 @@ func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn,
455459
cm.IfIndex = ifi.Index
456460
}
457461
if err := p.SetControlMessage(cf, toggle); err != nil {
458-
t.Error(err)
459-
return
462+
fatalf("%v", err)
460463
}
461464
ms := []ipv6.Message{
462465
{
@@ -467,16 +470,13 @@ func testPacketConnConcurrentReadWriteUnicast(t *testing.T, p *ipv6.PacketConn,
467470
}
468471
n, err := p.WriteBatch(ms, 0)
469472
if err != nil {
470-
t.Error(err)
471-
return
473+
fatalf("%v", err)
472474
}
473475
if n != len(ms) {
474-
t.Errorf("got %d; want %d", n, len(ms))
475-
return
476+
fatalf("got %d; want %d", n, len(ms))
476477
}
477478
if ms[0].N != len(data) {
478-
t.Errorf("got %d; want %d", ms[0].N, len(data))
479-
return
479+
fatalf("got %d; want %d", ms[0].N, len(data))
480480
}
481481
}
482482

0 commit comments

Comments
 (0)