diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index ef3030fe980a..3e530eaba911 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -40,6 +40,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d *Affecting all Beats* - Fix a race condition with the Kafka pipeline client, it is possible that `Close()` get called before `Connect()` . {issue}11945[11945] +- Recover from panics in the javascript process and log details about the failure to aid in future debugging. {pull}13690[13690] - Make the script processor concurrency-safe. {issue}13690[13690] {pull}13857[13857] *Auditbeat* diff --git a/libbeat/processors/script/javascript/session.go b/libbeat/processors/script/javascript/session.go index 46aea59bc9e6..6570d2398201 100644 --- a/libbeat/processors/script/javascript/session.go +++ b/libbeat/processors/script/javascript/session.go @@ -24,6 +24,7 @@ import ( "github.com/dop251/goja" "github.com/pkg/errors" + "go.uber.org/zap" "github.com/elastic/beats/libbeat/beat" "github.com/elastic/beats/libbeat/common" @@ -197,8 +198,25 @@ func (s *session) setEvent(b *beat.Event) error { } // runProcessFunc executes process() from the JS script. -func (s *session) runProcessFunc(b *beat.Event) (*beat.Event, error) { - var err error +func (s *session) runProcessFunc(b *beat.Event) (out *beat.Event, err error) { + defer func() { + if r := recover(); r != nil { + s.log.Errorw("The javascript processor caused an unexpected panic "+ + "while processing an event. Recovering, but please report this.", + "event", common.MapStr{"original": b.Fields.String()}, + "panic", r, + zap.Stack("stack")) + if !s.evt.IsCancelled() { + out = b + } + err = errors.Errorf("unexpected panic in javascript processor: %v", r) + if s.tagOnException != "" { + common.AddTags(b.Fields, []string{s.tagOnException}) + } + appendString(b.Fields, "error.message", err.Error(), false) + } + }() + if err = s.setEvent(b); err != nil { // Always return the event even if there was an error. return b, err