Skip to content

Commit 28bef6d

Browse files
authored
Merge pull request #7624 from TheThingsNetwork/bug/gs-race-condition-log
gs: Improve panic recover to detect gs race condition
2 parents 4589c86 + 1b06d44 commit 28bef6d

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

pkg/events/events.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,7 @@ func New(ctx context.Context, name, description string, opts ...Option) Event {
178178
return (&definition{name: name, description: description}).New(ctx, opts...)
179179
}
180180

181-
var errMarshalData = errors.Define("marshal_data", "marshal data")
182-
183181
func marshalData(data any) (anyPB *anypb.Any, err error) {
184-
// TODO: https://github.com/TheThingsIndustries/lorawan-stack-support/issues/1163.
185-
// Remove this after the issue is fixed.
186-
defer func() {
187-
if p := recover(); p != nil {
188-
if pErr, ok := p.(error); ok {
189-
err = errMarshalData.WithCause(pErr)
190-
} else {
191-
err = errMarshalData.WithAttributes("panic", p)
192-
}
193-
event := sentryerrors.NewEvent(err)
194-
sentry.CaptureEvent(event)
195-
}
196-
}()
197-
198-
return mustMarshalData(data)
199-
}
200-
201-
func mustMarshalData(data any) (anyPB *anypb.Any, err error) {
202182
if protoMessage, ok := data.(proto.Message); ok {
203183
anyPB, err = anypb.New(protoMessage)
204184
if err != nil {
@@ -242,14 +222,47 @@ func Proto(e Event) (*ttnpb.Event, error) {
242222
pb.Context = ctx
243223
if evt.data != nil {
244224
var err error
245-
pb.Data, err = marshalData(e.Data())
225+
// TODO: uncomment masrshalData and remove mustMarshalEventData after the issue mentioned
226+
// below (7632) is fixed.
227+
// pb.Data, err = marshalData(e.Data())
228+
pb.Data, err = mustMarshalEventData(e)
246229
if err != nil {
247230
return nil, err
248231
}
249232
}
250233
return pb, nil
251234
}
252235

236+
// TODO: remove after issue is resolved
237+
// https://github.com/TheThingsNetwork/lorawan-stack/issues/7623
238+
var errMarshalData = errors.Define("marshal_data", "marshal data")
239+
240+
func mustMarshalEventData(e Event) (*anypb.Any, error) {
241+
defer func() {
242+
if p := recover(); p != nil {
243+
var err error
244+
if pErr, ok := p.(error); ok {
245+
err = errMarshalData.WithCause(pErr).WithAttributes(
246+
"event_name", e.Name(),
247+
"event_correlation_ids", e.CorrelationIds(),
248+
"event_identifiers", e.Identifiers(),
249+
)
250+
} else {
251+
err = errMarshalData.WithAttributes(
252+
"panic", p,
253+
"event_name", e.Name(),
254+
"event_correlation_ids", e.CorrelationIds(),
255+
"event_identifiers", e.Identifiers(),
256+
)
257+
}
258+
event := sentryerrors.NewEvent(err)
259+
sentry.CaptureEvent(event)
260+
}
261+
}()
262+
263+
return marshalData(e.Data())
264+
}
265+
253266
// FromProto returns the event from its protobuf representation.
254267
func FromProto(pb *ttnpb.Event) (Event, error) {
255268
ctx, err := unmarshalContext(context.Background(), pb.Context)

0 commit comments

Comments
 (0)