Skip to content

Commit f08f9a1

Browse files
authored
Merge pull request #985 from nr-swilloughby/panic_nil
Remove special handling of panic(nil)
2 parents c1f3044 + f0da02c commit f08f9a1

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

v3/newrelic/internal_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ func TestPanicInt(t *testing.T) {
753753
app.ExpectMetrics(t, backgroundErrorMetrics)
754754
}
755755

756+
/* superseded now by TestPanicNilRecovery.
756757
func TestPanicNil(t *testing.T) {
757758
app := testApp(nil, func(cfg *Config) {
758759
enableRecordPanics(cfg)
@@ -769,6 +770,7 @@ func TestPanicNil(t *testing.T) {
769770
app.ExpectErrorEvents(t, []internal.WantEvent{})
770771
app.ExpectMetrics(t, backgroundMetrics)
771772
}
773+
*/
772774

773775
func TestResponseCodeError(t *testing.T) {
774776
app := testApp(nil, ConfigDistributedTracerEnabled(false), t)

v3/newrelic/internal_txn.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/http"
1111
"net/url"
1212
"reflect"
13-
"runtime"
1413
"runtime/debug"
1514
"sync"
1615
"time"
@@ -446,16 +445,11 @@ func (thd *thread) End(recovered interface{}) error {
446445

447446
txn.finished = true
448447

449-
// It used to be the case that panic(nil) would cause recover() to return nil,
450-
// which we test for here. However, that is no longer the case, hence the extra
451-
// check at this point to stop panic(nil) from propagating here. (as of Go 1.21)
452448
if recovered != nil {
453-
if _, isNilPanic := recovered.(*runtime.PanicNilError); !isNilPanic {
454-
e := txnErrorFromPanic(time.Now(), recovered)
455-
e.Stack = getStackTrace()
456-
thd.noticeErrorInternal(e, nil, false)
457-
log.Println(string(debug.Stack()))
458-
}
449+
e := txnErrorFromPanic(time.Now(), recovered)
450+
e.Stack = getStackTrace()
451+
thd.noticeErrorInternal(e, nil, false)
452+
log.Println(string(debug.Stack()))
459453
}
460454

461455
txn.markEnd(time.Now(), thd.thread)
@@ -547,13 +541,8 @@ func (thd *thread) End(recovered interface{}) error {
547541
}
548542
}
549543

550-
// Note that if a consumer uses `panic(nil)`, the panic will not
551-
// propagate. Update: well, not anymore. Go now returns an actual
552-
// non-nil value in this case.
553544
if recovered != nil {
554-
if _, isNilPanic := recovered.(*runtime.PanicNilError); !isNilPanic {
555-
panic(recovered)
556-
}
545+
panic(recovered)
557546
}
558547

559548
return nil

v3/newrelic/internal_txn_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"net/http"
99
"reflect"
10+
"runtime"
1011
"testing"
1112
"time"
1213

@@ -981,3 +982,39 @@ func TestErrAttrsAddedWhenPanic(t *testing.T) {
981982
},
982983
})
983984
}
985+
986+
func TestPanicNilRecovery(t *testing.T) {
987+
cfgFnRecordPanics := func(cfg *Config) {
988+
cfg.DistributedTracer.Enabled = true
989+
cfg.ErrorCollector.RecordPanics = true
990+
}
991+
app := testApp(replyFn, cfgFnRecordPanics, t)
992+
func() {
993+
defer func() {
994+
recovered := recover()
995+
if recovered == nil {
996+
t.Error("code did not panic as expected")
997+
} else if _, isNil := recovered.(*runtime.PanicNilError); !isNil {
998+
t.Errorf("code did not panic with nil as expected; got %v (type %T) instead", recovered, recovered)
999+
}
1000+
}()
1001+
txn := app.StartTransaction("hello")
1002+
defer txn.End()
1003+
panic(nil)
1004+
}()
1005+
app.ExpectSpanEvents(t, []internal.WantEvent{
1006+
{
1007+
AgentAttributes: map[string]interface{}{
1008+
SpanAttributeErrorClass: "panic",
1009+
SpanAttributeErrorMessage: "panic called with nil argument",
1010+
},
1011+
Intrinsics: map[string]interface{}{
1012+
"category": internal.MatchAnything,
1013+
"timestamp": internal.MatchAnything,
1014+
"name": "OtherTransaction/Go/hello",
1015+
"transaction.name": "OtherTransaction/Go/hello",
1016+
"nr.entryPoint": true,
1017+
},
1018+
},
1019+
})
1020+
}

0 commit comments

Comments
 (0)