Created
December 12, 2018 14:08
-
-
Save kirugan/23a465e28fb7e30823928e7f5e6d21bd to your computer and use it in GitHub Desktop.
Panic goroutine test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"syscall" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
fmt.Println("CPU:", runtime.NumCPU()) | |
fmt.Printf("Root thread %d\n", syscall.Gettid()) | |
defer func() { | |
if rc := recover(); rc != nil { | |
fmt.Println("root defer", rc) | |
} | |
}() | |
go func() { | |
fmt.Printf("Child thread %d\n", syscall.Gettid()) | |
defer func() { | |
if rc := recover(); rc != nil { | |
fmt.Println("Parent worked", rc) | |
} | |
}() | |
time.Sleep(3 * time.Second) | |
go func() { | |
// advice to runtime for moving goroutine to another thread | |
time.Sleep(time.Microsecond) | |
fmt.Printf("Panic in thread %d\n", syscall.Gettid()) | |
panic("leaf panicked") | |
}() | |
time.Sleep(time.Second) | |
}() | |
time.Sleep(time.Second * 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to test:
docker run -it -v `pwd`:/gotest golang go run /gotest/test.go