From 81b0da8156e9c8fed78c34b9661f1fe9c5295514 Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Thu, 18 Aug 2016 09:01:12 -0600 Subject: [PATCH] recover from panic on startup and retry --- bench.txt | 9 --------- cmd/avi/avi.go | 10 ++++++++-- head/head.go | 21 ++++++++++++++++++++- 3 files changed, 28 insertions(+), 12 deletions(-) delete mode 100644 bench.txt diff --git a/bench.txt b/bench.txt deleted file mode 100644 index 4c38c42..0000000 --- a/bench.txt +++ /dev/null @@ -1,9 +0,0 @@ -PASS -BenchmarkLoop 2000000 913 ns/op -ok github.com/nathanielc/avi 2.759s -? github.com/nathanielc/avi/avi [no test files] -? github.com/nathanielc/avi/head [no test files] -? github.com/nathanielc/avi/jac [no test files] -? github.com/nathanielc/avi/nathanielc [no test files] -? github.com/nathanielc/avi/nav [no test files] -? github.com/nathanielc/avi/ships [no test files] diff --git a/cmd/avi/avi.go b/cmd/avi/avi.go index 4101ced..b2f6a34 100644 --- a/cmd/avi/avi.go +++ b/cmd/avi/avi.go @@ -58,7 +58,9 @@ func main() { s := snappy.NewReader(f) updates := head.ProtoStreamUpdates(s) - head.Run(updates) + if err := head.Run(updates); err != nil { + log.Fatal(err) + } return } @@ -115,7 +117,11 @@ func main() { if *live { ls := head.NewLiveStream() drawer = head.NewProxyStream(drawer, ls) - go head.Run(ls.Updates()) + go func() { + if err := head.Run(ls.Updates()); err != nil { + log.Fatal(err) + } + }() } sim, err := avi.NewSimulation( diff --git a/head/head.go b/head/head.go index d948f57..7dd5a1a 100644 --- a/head/head.go +++ b/head/head.go @@ -24,7 +24,25 @@ func init() { flatShader, _ = gfxutil.OpenShader("head/models/flat") } -func Run(updates <-chan FrameUpdate) { +func Run(updates <-chan FrameUpdate) (err error) { + maxStartupRetries := 5 + for maxStartupRetries > 0 { + if err = run(updates); err == nil { + // Technically run only returns if it errors, but this is for completeness + return + } + maxStartupRetries-- + } + return +} + +func run(updates <-chan FrameUpdate) (err error) { + defer func() { + r := recover() + if r != nil { + err = fmt.Errorf("starup error: %v", r) + } + }() go func() { // Create our window. props := window.NewProps() @@ -38,6 +56,7 @@ func Run(updates <-chan FrameUpdate) { go gfxLoopWindow(w, r, updates) }() window.MainLoop() + return } func gfxLoopWindow(w window.Window, d gfx.Device, updates <-chan FrameUpdate) {