Skip to content

Commit

Permalink
fix(api): better panic recovery in SSE (#3517)
Browse files Browse the repository at this point in the history
to handle:
```
api       | 2018-10-29 10:30:15 | err  | [PANIC_RECOVERY] Panic occurred on GET:/events?lastEventId=&r=5076636469124916, recover index > windowEnd
api       | 2018-10-29 10:30:15 | err  | [PANIC_RECOVERY] Stacktrace of 4096 bytes
```
  • Loading branch information
fsamin authored and yesnault committed Oct 31, 2018
1 parent 87d14b5 commit 506e86f
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions engine/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,19 @@ func (b *eventsBroker) Start(ctx context.Context) {
}

func (b *eventsBroker) ServeHTTP() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error) {
// Make sure that the writer supports flushing.
f, ok := w.(http.Flusher)
if !ok {
return sdk.WrapError(fmt.Errorf("streaming unsupported"), "")
}

defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("eventsBroker.ServeHTTP recovered %v", r)
}
}()

user := getUser(ctx)
if err := loadUserPermissions(b.dbFunc(), b.cache, user); err != nil {
return sdk.WrapError(err, "eventsBroker.Serve Cannot load user permission")
Expand Down Expand Up @@ -270,6 +276,12 @@ func (client *eventsBrokerSubscribe) Send(event sdk.Event) (err error) {
client.mutex.Lock()
defer client.mutex.Unlock()

defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("eventsBrokerSubscribe.Send recovered %v", r)
}
}()

if client == nil || client.w == nil {
return nil
}
Expand Down Expand Up @@ -298,12 +310,6 @@ func (client *eventsBrokerSubscribe) Send(event sdk.Event) (err error) {
return nil
}

defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()

if _, err := client.w.Write(buffer.Bytes()); err != nil {
return sdk.WrapError(err, "unable to write to client")
}
Expand Down

0 comments on commit 506e86f

Please sign in to comment.