Skip to content

Commit 4aeb9ba

Browse files
committed
runtime/pprof: return errors from writing profiles
Fixes #73107 Change-Id: I41f3e1bd1fdaca2f0e94151b2320bd569e258a51 Reviewed-on: https://go-review.googlesource.com/c/go/+/671576 Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent a632009 commit 4aeb9ba

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

src/runtime/pprof/pprof.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ func printCountCycleProfile(w io.Writer, countName, cycleName string, records []
446446
locs = b.appendLocsForStack(locs[:0], expandedStack[:n])
447447
b.pbSample(values, locs, nil)
448448
}
449-
b.build()
450-
return nil
449+
return b.build()
451450
}
452451

453452
// printCountProfile prints a countProfile at the specified debug level.
@@ -517,8 +516,7 @@ func printCountProfile(w io.Writer, debug int, name string, p countProfile) erro
517516
}
518517
b.pbSample(values, locs, labels)
519518
}
520-
b.build()
521-
return nil
519+
return b.build()
522520
}
523521

524522
// keysByCount sorts keys with higher counts first, breaking ties by key string order.

src/runtime/pprof/proto.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (b *profileBuilder) addCPUData(data []uint64, tags []unsafe.Pointer) error
345345
}
346346

347347
// build completes and returns the constructed profile.
348-
func (b *profileBuilder) build() {
348+
func (b *profileBuilder) build() error {
349349
b.end = time.Now()
350350

351351
b.pb.int64Opt(tagProfile_TimeNanos, b.start.UnixNano())
@@ -387,8 +387,11 @@ func (b *profileBuilder) build() {
387387
// TODO: Anything for tagProfile_KeepFrames?
388388

389389
b.pb.strings(tagProfile_StringTable, b.strings)
390-
b.zw.Write(b.pb.data)
391-
b.zw.Close()
390+
_, err := b.zw.Write(b.pb.data)
391+
if err != nil {
392+
return err
393+
}
394+
return b.zw.Close()
392395
}
393396

394397
// appendLocsForStack appends the location IDs for the given stack trace to the given

src/runtime/pprof/proto_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package pprof
77
import (
88
"bytes"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"internal/abi"
1213
"internal/profile"
@@ -33,7 +34,9 @@ func translateCPUProfile(data []uint64, count int) (*profile.Profile, error) {
3334
if err := b.addCPUData(data, tags); err != nil {
3435
return nil, err
3536
}
36-
b.build()
37+
if err := b.build(); err != nil {
38+
return nil, err
39+
}
3740
return profile.Parse(&buf)
3841
}
3942

@@ -473,3 +476,16 @@ func TestEmptyStack(t *testing.T) {
473476
t.Fatalf("translating profile: %v", err)
474477
}
475478
}
479+
480+
var errWrite = errors.New("error from writer")
481+
482+
type errWriter struct{}
483+
484+
func (errWriter) Write(p []byte) (int, error) { return 0, errWrite }
485+
486+
func TestWriteToErr(t *testing.T) {
487+
err := Lookup("heap").WriteTo(&errWriter{}, 0)
488+
if !errors.Is(err, errWrite) {
489+
t.Fatalf("want error from writer, got: %v", err)
490+
}
491+
}

src/runtime/pprof/protomem.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ func writeHeapProto(w io.Writer, p []profilerecord.MemProfileRecord, rate int64,
6363
}
6464
})
6565
}
66-
b.build()
67-
return nil
66+
return b.build()
6867
}
6968

7069
// scaleHeapSample adjusts the data from a heap Sample to

0 commit comments

Comments
 (0)