Skip to content

Commit

Permalink
runtime: don't call traceReadCPU on the system stack
Browse files Browse the repository at this point in the history
traceReadCPU calls profBuf.read, which does a raceacquire. g0 does not
have a race context, so this crashes when running on the system stack.

We could borrow a race context, but it is simpler to just move
traceReadCPU off of the system stack.

Fixes golang#65607.

Change-Id: I335155b96d683aebb92b2f4e1eea063dd139f2d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/562996
Auto-Submit: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
prattmic authored and gopherbot committed Feb 9, 2024
1 parent 532c6f1 commit 9fa153b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/runtime/trace2.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ func traceAdvance(stopTrace bool) {
}
statusWriter.flush().end()

// Read everything out of the last gen's CPU profile buffer.
traceReadCPU(gen)

systemstack(func() {
// Flush CPU samples, stacks, and strings for the last generation. This is safe,
// because we're now certain no M is writing to the last generation.
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/trace2cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func traceStopReadCPU() {
//
// No more than one goroutine may be in traceReadCPU for the same
// profBuf at a time.
//
// Must not run on the system stack because profBuf.read performs race
// operations.
func traceReadCPU(gen uintptr) bool {
var pcBuf [traceStackSize]uintptr

Expand Down Expand Up @@ -198,9 +201,6 @@ func traceReadCPU(gen uintptr) bool {
//
//go:systemstack
func traceCPUFlush(gen uintptr) {
// Read everything out of the last gen's CPU profile buffer.
traceReadCPU(gen)

// Flush any remaining trace buffers containing CPU samples.
if buf := trace.cpuBuf[gen%2]; buf != nil {
lock(&trace.lock)
Expand Down

0 comments on commit 9fa153b

Please sign in to comment.