From 7ab9794623bf6e4d5f1f8503eba1a1fddc40e728 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 24 Oct 2016 15:44:04 +0100 Subject: [PATCH] Protect panicking poll loops from taking down the entire process --- src/github.com/matrix-org/go-neb/polling/polling.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/github.com/matrix-org/go-neb/polling/polling.go b/src/github.com/matrix-org/go-neb/polling/polling.go index 2500d30..d4b6bbc 100644 --- a/src/github.com/matrix-org/go-neb/polling/polling.go +++ b/src/github.com/matrix-org/go-neb/polling/polling.go @@ -5,6 +5,7 @@ import ( "github.com/matrix-org/go-neb/clients" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/types" + "runtime/debug" "sync" "time" ) @@ -71,6 +72,15 @@ func pollLoop(service types.Service, ts int64) { "service_id": service.ServiceID(), "service_type": service.ServiceType(), }) + + defer func() { + if r := recover(); r != nil { + logger.WithField("panic", r).Errorf( + "pollLoop panicked!\n%s", debug.Stack(), + ) + } + }() + poller, ok := service.(types.Poller) if !ok { logger.Error("Service is not a Poller.")