summaryrefslogtreecommitdiffstats
path: root/src/runtime/testdata/testprog/deadlock.go
blob: 781acbd7706ac68627f3962bfd9d11f6c8953f41 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2015 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"
	"runtime"
	"runtime/debug"
	"time"
)

func init() {
	registerInit("InitDeadlock", InitDeadlock)
	registerInit("NoHelperGoroutines", NoHelperGoroutines)

	register("SimpleDeadlock", SimpleDeadlock)
	register("LockedDeadlock", LockedDeadlock)
	register("LockedDeadlock2", LockedDeadlock2)
	register("GoexitDeadlock", GoexitDeadlock)
	register("StackOverflow", StackOverflow)
	register("ThreadExhaustion", ThreadExhaustion)
	register("RecursivePanic", RecursivePanic)
	register("RecursivePanic2", RecursivePanic2)
	register("RecursivePanic3", RecursivePanic3)
	register("RecursivePanic4", RecursivePanic4)
	register("RecursivePanic5", RecursivePanic5)
	register("GoexitExit", GoexitExit)
	register("GoNil", GoNil)
	register("MainGoroutineID", MainGoroutineID)
	register("Breakpoint", Breakpoint)
	register("GoexitInPanic", GoexitInPanic)
	register("PanicAfterGoexit", PanicAfterGoexit)
	register("RecoveredPanicAfterGoexit", RecoveredPanicAfterGoexit)
	register("RecoverBeforePanicAfterGoexit", RecoverBeforePanicAfterGoexit)
	register("RecoverBeforePanicAfterGoexit2", RecoverBeforePanicAfterGoexit2)
	register("PanicTraceback", PanicTraceback)
	register("GoschedInPanic", GoschedInPanic)
	register("SyscallInPanic", SyscallInPanic)
	register("PanicLoop", PanicLoop)
}

func SimpleDeadlock() {
	select {}
	panic("not reached")
}

func InitDeadlock() {
	select {}
	panic("not reached")
}

func LockedDeadlock() {
	runtime.LockOSThread()
	select {}
}

func LockedDeadlock2() {
	go func() {
		runtime.LockOSThread()
		select {}
	}()
	time.Sleep(time.Millisecond)
	select {}
}

func GoexitDeadlock() {
	F := func() {
		for i := 0; i < 10; i++ {
		}
	}

	go F()
	go F()
	runtime.Goexit()
}

func StackOverflow() {
	var f func() byte
	f = func() byte {
		var buf [64 << 10]byte
		return buf[0] + f()
	}
	debug.SetMaxStack(1474560)
	f()
}

func ThreadExhaustion() {
	debug.SetMaxThreads(10)
	c := make(chan int)
	for i := 0; i < 100; i++ {
		go func() {
			runtime.LockOSThread()
			c <- 0
			select {}
		}()
		<-c
	}
}

func RecursivePanic() {
	func() {
		defer func() {
			fmt.Println(recover())
		}()
		var x [8192]byte
		func(x [8192]byte) {
			defer func() {
				if err := recover(); err != nil {
					panic("wrap: " + err.(string))
				}
			}()
			panic("bad")
		}(x)
	}()
	panic("again")
}

// Same as RecursivePanic, but do the first recover and the second panic in
// separate defers, and make sure they are executed in the correct order.
func RecursivePanic2() {
	func() {
		defer func() {
			fmt.Println(recover())
		}()
		var x [8192]byte
		func(x [8192]byte) {
			defer func() {
				panic("second panic")
			}()
			defer func() {
				fmt.Println(recover())
			}()
			panic("first panic")
		}(x)
	}()
	panic("third panic")
}

// Make sure that the first panic finished as a panic, even though the second
// panic was recovered
func RecursivePanic3() {
	defer func() {
		defer func() {
			recover()
		}()
		panic("second panic")
	}()
	panic("first panic")
}

// Test case where a single defer recovers one panic but starts another panic. If
// the second panic is never recovered, then the recovered first panic will still
// appear on the panic stack (labeled '[recovered]') and the runtime stack.
func RecursivePanic4() {
	defer func() {
		recover()
		panic("second panic")
	}()
	panic("first panic")
}

// Test case where we have an open-coded defer higher up the stack (in two), and
// in the current function (three) we recover in a defer while we still have
// another defer to be processed.
func RecursivePanic5() {
	one()
	panic("third panic")
}

//go:noinline
func one() {
	two()
}

//go:noinline
func two() {
	defer func() {
	}()

	three()
}

//go:noinline
func three() {
	defer func() {
	}()

	defer func() {
		fmt.Println(recover())
	}()

	defer func() {
		fmt.Println(recover())
		panic("second panic")
	}()

	panic("first panic")
}

func GoexitExit() {
	println("t1")
	go func() {
		time.Sleep(time.Millisecond)
	}()
	i := 0
	println("t2")
	runtime.SetFinalizer(&i, func(p *int) {})
	println("t3")
	runtime.GC()
	println("t4")
	runtime.Goexit()
}

func GoNil() {
	defer func() {
		recover()
	}()
	var f func()
	go f()
	select {}
}

func MainGoroutineID() {
	panic("test")
}

func NoHelperGoroutines() {
	i := 0
	runtime.SetFinalizer(&i, func(p *int) {})
	time.AfterFunc(time.Hour, func() {})
	panic("oops")
}

func Breakpoint() {
	runtime.Breakpoint()
}

func GoexitInPanic() {
	go func() {
		defer func() {
			runtime.Goexit()
		}()
		panic("hello")
	}()
	runtime.Goexit()
}

type errorThatGosched struct{}

func (errorThatGosched) Error() string {
	runtime.Gosched()
	return "errorThatGosched"
}

func GoschedInPanic() {
	panic(errorThatGosched{})
}

type errorThatPrint struct{}

func (errorThatPrint) Error() string {
	fmt.Println("1")
	fmt.Println("2")
	return "3"
}

func SyscallInPanic() {
	panic(errorThatPrint{})
}

func PanicAfterGoexit() {
	defer func() {
		panic("hello")
	}()
	runtime.Goexit()
}

func RecoveredPanicAfterGoexit() {
	defer func() {
		defer func() {
			r := recover()
			if r == nil {
				panic("bad recover")
			}
		}()
		panic("hello")
	}()
	runtime.Goexit()
}

func RecoverBeforePanicAfterGoexit() {
	// 1. defer a function that recovers
	// 2. defer a function that panics
	// 3. call goexit
	// Goexit runs the #2 defer. Its panic
	// is caught by the #1 defer.  For Goexit, we explicitly
	// resume execution in the Goexit loop, instead of resuming
	// execution in the caller (which would make the Goexit disappear!)
	defer func() {
		r := recover()
		if r == nil {
			panic("bad recover")
		}
	}()
	defer func() {
		panic("hello")
	}()
	runtime.Goexit()
}

func RecoverBeforePanicAfterGoexit2() {
	for i := 0; i < 2; i++ {
		defer func() {
		}()
	}
	// 1. defer a function that recovers
	// 2. defer a function that panics
	// 3. call goexit
	// Goexit runs the #2 defer. Its panic
	// is caught by the #1 defer.  For Goexit, we explicitly
	// resume execution in the Goexit loop, instead of resuming
	// execution in the caller (which would make the Goexit disappear!)
	defer func() {
		r := recover()
		if r == nil {
			panic("bad recover")
		}
	}()
	defer func() {
		panic("hello")
	}()
	runtime.Goexit()
}

func PanicTraceback() {
	pt1()
}

func pt1() {
	defer func() {
		panic("panic pt1")
	}()
	pt2()
}

func pt2() {
	defer func() {
		panic("panic pt2")
	}()
	panic("hello")
}

type panicError struct{}

func (*panicError) Error() string {
	panic("double error")
}

func PanicLoop() {
	panic(&panicError{})
}