From ce414ea58609878753b939d0abc4949181c896a0 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 24 Oct 2016 15:37:09 +0100 Subject: [PATCH] Prevent panicking code triggered by matrix events from taking down the entire process Specifically, this protects panicking `!commands` and expansions. --- .../matrix-org/go-neb/matrix/worker.go | 20 +++++++++++++++++++ .../matrix-org/go-neb/server/server.go | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/matrix/worker.go b/src/github.com/matrix-org/go-neb/matrix/worker.go index c5770f5..3674601 100644 --- a/src/github.com/matrix-org/go-neb/matrix/worker.go +++ b/src/github.com/matrix-org/go-neb/matrix/worker.go @@ -1,5 +1,10 @@ package matrix +import ( + log "github.com/Sirupsen/logrus" + "runtime/debug" +) + // Worker processes incoming events and updates the Matrix client's data structures. It also informs // any attached listeners of the new events. type Worker struct { @@ -38,6 +43,21 @@ func (worker *Worker) notifyListeners(event *Event) { } func (worker *Worker) onSyncHTTPResponse(res syncHTTPResponse) { + defer func() { + if r := recover(); r != nil { + userID := "" + if worker.client != nil { + userID = worker.client.UserID + } + log.WithFields(log.Fields{ + "panic": r, + "user_id": userID, + }).Errorf( + "onSyncHTTPResponse panicked!\n%s", debug.Stack(), + ) + } + }() + for roomID, roomData := range res.Rooms.Join { room := worker.client.getOrCreateRoom(roomID) for _, event := range roomData.State.Events { diff --git a/src/github.com/matrix-org/go-neb/server/server.go b/src/github.com/matrix-org/go-neb/server/server.go index 98d74d1..91e64e8 100644 --- a/src/github.com/matrix-org/go-neb/server/server.go +++ b/src/github.com/matrix-org/go-neb/server/server.go @@ -45,7 +45,7 @@ func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc { logger.Print("Incoming request") defer func() { if r := recover(); r != nil { - logger.WithField("error", r).Errorf( + logger.WithField("panic", r).Errorf( "Request panicked!\n%s", debug.Stack(), ) jsonErrorResponse(