diff --git a/src/runtime/metrics/description_test.go b/src/runtime/metrics/description_test.go index 0ee9ea16d0b..71c7b00b02e 100644 --- a/src/runtime/metrics/description_test.go +++ b/src/runtime/metrics/description_test.go @@ -156,3 +156,10 @@ func TestDocs(t *testing.T) { fmt.Fprintf(os.Stderr, "go test -generate: doc.go already up-to-date\n") } } + +func TestReadEmptySlice(t *testing.T) { + // Test that Read does not panic when given an empty slice. + // This should be a no-op. + metrics.Read(nil) + metrics.Read([]metrics.Sample{}) +} diff --git a/src/runtime/metrics/sample.go b/src/runtime/metrics/sample.go index 9efc5c5f06a..df1341d48c5 100644 --- a/src/runtime/metrics/sample.go +++ b/src/runtime/metrics/sample.go @@ -43,5 +43,8 @@ func runtime_readMetrics(unsafe.Pointer, int, int) // Sample values with names not appearing in [All] will have their Value populated // as KindBad to indicate that the name is unknown. func Read(m []Sample) { + if len(m) == 0 { + return + } runtime_readMetrics(unsafe.Pointer(&m[0]), len(m), cap(m)) }