Skip to content

Instantly share code, notes, and snippets.

@kirugan
Created December 12, 2018 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirugan/23a465e28fb7e30823928e7f5e6d21bd to your computer and use it in GitHub Desktop.
Save kirugan/23a465e28fb7e30823928e7f5e6d21bd to your computer and use it in GitHub Desktop.
Panic goroutine test
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)
}
@kirugan
Copy link
Author

kirugan commented Dec 12, 2018

How to test:
docker run -it -v `pwd`:/gotest golang go run /gotest/test.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment