summaryrefslogtreecommitdiffstats
path: root/src/runtime/testdata/testprognet/waiters.go
blob: a65c40bfbecc43ffca10f2ce634e6834d78a4510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"io"
	"log"
	"net"
	"runtime/internal/atomic"
	"sync"
	"time"
	_ "unsafe" // for go:linkname
)

// The bug is that netpollWaiters increases monotonically.
// This doesn't cause a problem until it overflows.
// Use linkname to see the value.
//
//go:linkname netpollWaiters runtime.netpollWaiters
var netpollWaiters atomic.Uint32

func init() {
	register("NetpollWaiters", NetpollWaiters)
}

func NetpollWaiters() {
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		log.Fatal(err)
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		conn, err := listener.Accept()
		if err != nil {
			log.Fatal(err)
		}
		defer conn.Close()
		if _, err := io.Copy(io.Discard, conn); err != nil {
			log.Fatal(err)
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		conn, err := net.Dial("tcp", listener.Addr().String())
		if err != nil {
			log.Fatal(err)
		}
		defer conn.Close()
		for i := 0; i < 10; i++ {
			fmt.Fprintf(conn, "%d\n", i)
			time.Sleep(time.Millisecond)
		}
	}()

	wg.Wait()
	if v := netpollWaiters.Load(); v != 0 {
		log.Fatalf("current waiters %v", v)
	}

	fmt.Println("OK")
}