Skip to content

Commit

Permalink
Bugfix: handle non-string panic values.
Browse files Browse the repository at this point in the history
Fixes: #59
  • Loading branch information
lthibault committed Feb 15, 2022
1 parent 783dc58 commit 067b286
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions v4/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type (
EventType int

EventHook func(Event)

// SprintFunc formats an arbitrary Go value into a string.
// It is used by the supervisor to format the value of a call
// to recover().
SprintFunc func(interface{}) string
)

const (
Expand Down
6 changes: 3 additions & 3 deletions v4/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ type syncSupervisor struct {

func (ss syncSupervisor) isSupervisorMessage() {}

func (s *Supervisor) fail(id serviceID, panicMsg string, stacktrace []byte) {
s.control <- serviceFailed{id, panicMsg, stacktrace}
func (s *Supervisor) fail(id serviceID, panicVal interface{}, stacktrace []byte) {
s.control <- serviceFailed{id, panicVal, stacktrace}
}

type serviceFailed struct {
id serviceID
panicMsg string
panicVal interface{}
stacktrace []byte
}

Expand Down
13 changes: 10 additions & 3 deletions v4/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (s *Supervisor) Serve(ctx context.Context) error {
case m := <-s.control:
switch msg := m.(type) {
case serviceFailed:
s.handleFailedService(ctx, msg.id, msg.panicMsg, msg.stacktrace, true)
s.handleFailedService(ctx, msg.id, msg.panicVal, msg.stacktrace, true)
case serviceEnded:
_, monitored := s.services[msg.id]
if monitored {
Expand Down Expand Up @@ -507,7 +507,7 @@ func (s *Supervisor) handleFailedService(ctx context.Context, id serviceID, err
CurrentFailures: s.failures,
FailureThreshold: s.spec.FailureThreshold,
Restarting: curState == normal,
PanicMsg: err.(string),
PanicMsg: s.spec.Sprint(err),
Stacktrace: string(stacktrace),
})
} else {
Expand Down Expand Up @@ -543,7 +543,7 @@ func (s *Supervisor) runService(ctx context.Context, service Service, id service
buf := make([]byte, 65535)
written := runtime.Stack(buf, false)
buf = buf[:written]
s.fail(id, r.(string), buf)
s.fail(id, r, buf)
}
}()
}
Expand Down Expand Up @@ -848,6 +848,7 @@ var ErrSupervisorNotStarted = errors.New("supervisor not started yet")
// supervisor. See the New function for full documentation.
type Spec struct {
EventHook EventHook
Sprint SprintFunc
FailureDecay float64
FailureThreshold float64
FailureBackoff time.Duration
Expand Down Expand Up @@ -880,4 +881,10 @@ func (s *Spec) configureDefaults(supervisorName string) {
log.Print(e)
}
}

if s.Sprint == nil {
s.Sprint = func(v interface{}) string {
return fmt.Sprintf("%v", v)
}
}
}

0 comments on commit 067b286

Please sign in to comment.