File size: 1,915 Bytes
de452ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
// Copyright 2025 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 (
	"runtime"
)

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

// Stress test for pp.oldm pointing to an exited M.
//
// If pp.oldm points to an exited M it should be ignored and another M used
// instead. To stress:
//
// 1. Start and exit many threads (thus setting oldm on some P).
// 2. Meanwhile, frequently stop the world.
//
// If procresize incorrect attempts to assign a P to an exited M, likely
// failure modes are:
//
// 1. Crash in startTheWorldWithSema attempting to access the M, if it is nil.
//
// 2. Memory corruption elsewhere after startTheWorldWithSema writes to the M,
// if it is not nil, but is freed and reused for another allocation.
//
// 3. Hang on a subsequent stop the world waiting for the P to stop, if the M
// object is valid, but the M is exited, because startTheWorldWithSema didn't
// actually wake anything to run the P. The P is _Pidle, but not in the pidle
// list, thus startTheWorldWithSema will wake for it to actively stop.
//
// For this to go wrong, an exited M must fail to clear mp.self and must leave
// the M on the sched.midle list.
//
// Similar to TraceSTW.
func mexitSTW() {
	// Ensure we have multiple Ps, but not too many, as we want the
	// runnable goroutines likely to run on Ps with oldm set.
	runtime.GOMAXPROCS(4)

	// Background busy work so there is always something runnable.
	for i := range 2 {
		go traceSTWTarget(i)
	}

	// Wait for children to start running.
	ping.Store(1)
	for pong[0].Load() != 1 {}
	for pong[1].Load() != 1 {}

	for range 100 {
		// Exit a thread. The last P to run this will have it in oldm.
		go func() {
			runtime.LockOSThread()
		}()

		// STW
		var ms runtime.MemStats
		runtime.ReadMemStats(&ms)
	}

	stop.Store(true)

	println("OK")
}