github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/runtime/testdata/testprog/deadlock.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "runtime" 10 "runtime/debug" 11 "time" 12 ) 13 14 func init() { 15 registerInit("InitDeadlock", InitDeadlock) 16 registerInit("NoHelperGoroutines", NoHelperGoroutines) 17 18 register("SimpleDeadlock", SimpleDeadlock) 19 register("LockedDeadlock", LockedDeadlock) 20 register("LockedDeadlock2", LockedDeadlock2) 21 register("GoexitDeadlock", GoexitDeadlock) 22 register("StackOverflow", StackOverflow) 23 register("ThreadExhaustion", ThreadExhaustion) 24 register("RecursivePanic", RecursivePanic) 25 register("GoexitExit", GoexitExit) 26 register("GoNil", GoNil) 27 register("MainGoroutineID", MainGoroutineID) 28 register("Breakpoint", Breakpoint) 29 register("GoexitInPanic", GoexitInPanic) 30 register("PanicAfterGoexit", PanicAfterGoexit) 31 register("RecoveredPanicAfterGoexit", RecoveredPanicAfterGoexit) 32 register("PanicTraceback", PanicTraceback) 33 register("GoschedInPanic", GoschedInPanic) 34 register("SyscallInPanic", SyscallInPanic) 35 } 36 37 func SimpleDeadlock() { 38 select {} 39 panic("not reached") 40 } 41 42 func InitDeadlock() { 43 select {} 44 panic("not reached") 45 } 46 47 func LockedDeadlock() { 48 runtime.LockOSThread() 49 select {} 50 } 51 52 func LockedDeadlock2() { 53 go func() { 54 runtime.LockOSThread() 55 select {} 56 }() 57 time.Sleep(time.Millisecond) 58 select {} 59 } 60 61 func GoexitDeadlock() { 62 F := func() { 63 for i := 0; i < 10; i++ { 64 } 65 } 66 67 go F() 68 go F() 69 runtime.Goexit() 70 } 71 72 func StackOverflow() { 73 var f func() byte 74 f = func() byte { 75 var buf [64 << 10]byte 76 return buf[0] + f() 77 } 78 debug.SetMaxStack(1474560) 79 f() 80 } 81 82 func ThreadExhaustion() { 83 debug.SetMaxThreads(10) 84 c := make(chan int) 85 for i := 0; i < 100; i++ { 86 go func() { 87 runtime.LockOSThread() 88 c <- 0 89 select {} 90 }() 91 <-c 92 } 93 } 94 95 func RecursivePanic() { 96 func() { 97 defer func() { 98 fmt.Println(recover()) 99 }() 100 var x [8192]byte 101 func(x [8192]byte) { 102 defer func() { 103 if err := recover(); err != nil { 104 panic("wrap: " + err.(string)) 105 } 106 }() 107 panic("bad") 108 }(x) 109 }() 110 panic("again") 111 } 112 113 func GoexitExit() { 114 go func() { 115 time.Sleep(time.Millisecond) 116 }() 117 i := 0 118 runtime.SetFinalizer(&i, func(p *int) {}) 119 runtime.GC() 120 runtime.Goexit() 121 } 122 123 func GoNil() { 124 defer func() { 125 recover() 126 }() 127 var f func() 128 go f() 129 select {} 130 } 131 132 func MainGoroutineID() { 133 panic("test") 134 } 135 136 func NoHelperGoroutines() { 137 i := 0 138 runtime.SetFinalizer(&i, func(p *int) {}) 139 time.AfterFunc(time.Hour, func() {}) 140 panic("oops") 141 } 142 143 func Breakpoint() { 144 runtime.Breakpoint() 145 } 146 147 func GoexitInPanic() { 148 go func() { 149 defer func() { 150 runtime.Goexit() 151 }() 152 panic("hello") 153 }() 154 runtime.Goexit() 155 } 156 157 type errorThatGosched struct{} 158 159 func (errorThatGosched) Error() string { 160 runtime.Gosched() 161 return "errorThatGosched" 162 } 163 164 func GoschedInPanic() { 165 panic(errorThatGosched{}) 166 } 167 168 type errorThatPrint struct{} 169 170 func (errorThatPrint) Error() string { 171 fmt.Println("1") 172 fmt.Println("2") 173 return "3" 174 } 175 176 func SyscallInPanic() { 177 panic(errorThatPrint{}) 178 } 179 180 func PanicAfterGoexit() { 181 defer func() { 182 panic("hello") 183 }() 184 runtime.Goexit() 185 } 186 187 func RecoveredPanicAfterGoexit() { 188 defer func() { 189 defer func() { 190 r := recover() 191 if r == nil { 192 panic("bad recover") 193 } 194 }() 195 panic("hello") 196 }() 197 runtime.Goexit() 198 } 199 200 func PanicTraceback() { 201 pt1() 202 } 203 204 func pt1() { 205 defer func() { 206 panic("panic pt1") 207 }() 208 pt2() 209 } 210 211 func pt2() { 212 defer func() { 213 panic("panic pt2") 214 }() 215 panic("hello") 216 }