Skip to content

Commit

Permalink
fix(chore): Wrong panic in singleflight
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Jordan committed Jan 9, 2024
1 parent 7fb48f5 commit 9299416
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,19 @@ func (s *SouinBaseHandler) Upstream(
prometheus.Increment(prometheus.RequestCounter)
shared := true

var recoveredFromErr error = nil
defer func() {
// In case of "http.ErrAbortHandler" panic,
// prevent singleflight from wrapping it into "singleflight.panicError".
if r := recover(); r != nil {
err := r.(error)
if errors.Is(err, http.ErrAbortHandler) {
recoveredFromErr = errors.Unwrap(err)
} else {
panic(err)
}
}
}()
sfValue, err, _ := s.singleflightPool.Do(cachedKey, func() (interface{}, error) {
shared = false
if e := next(customWriter, rq); e != nil {
Expand Down Expand Up @@ -330,7 +343,9 @@ func (s *SouinBaseHandler) Upstream(
code: customWriter.statusCode,
}, err
})

if recoveredFromErr != nil {
panic(recoveredFromErr)
}
if err != nil {
return err
}
Expand Down

0 comments on commit 9299416

Please sign in to comment.