Browse Source

Prevent panicking code triggered by matrix events from taking down the entire process

Specifically, this protects panicking `!commands` and expansions.
kegan/service-isolation
Kegan Dougal 8 years ago
parent
commit
ce414ea586
  1. 20
      src/github.com/matrix-org/go-neb/matrix/worker.go
  2. 2
      src/github.com/matrix-org/go-neb/server/server.go

20
src/github.com/matrix-org/go-neb/matrix/worker.go

@ -1,5 +1,10 @@
package matrix 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 // Worker processes incoming events and updates the Matrix client's data structures. It also informs
// any attached listeners of the new events. // any attached listeners of the new events.
type Worker struct { type Worker struct {
@ -38,6 +43,21 @@ func (worker *Worker) notifyListeners(event *Event) {
} }
func (worker *Worker) onSyncHTTPResponse(res syncHTTPResponse) { 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 { for roomID, roomData := range res.Rooms.Join {
room := worker.client.getOrCreateRoom(roomID) room := worker.client.getOrCreateRoom(roomID)
for _, event := range roomData.State.Events { for _, event := range roomData.State.Events {

2
src/github.com/matrix-org/go-neb/server/server.go

@ -45,7 +45,7 @@ func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc {
logger.Print("Incoming request") logger.Print("Incoming request")
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
logger.WithField("error", r).Errorf(
logger.WithField("panic", r).Errorf(
"Request panicked!\n%s", debug.Stack(), "Request panicked!\n%s", debug.Stack(),
) )
jsonErrorResponse( jsonErrorResponse(

Loading…
Cancel
Save