Skip to content

Commit 49e1dc9

Browse files
committed
runtime: limit total frames traversed during stack walking
Stack walking in tracebackPCs now limits the total number of frames traversed, including wrapper frames, to prevent excessive CPU time when unwinding stacks with very deep context chains. Previously, tracebackPCs only limited the number of output frames (64) but would traverse all wrapper frames without limit. With extremely deep context chains (e.g., 10 million layers), this could cause stack walking to take seconds, as wrapper frames for methods like context.(*valueCtx).Deadline are traversed but not counted toward the output limit. This change adds a maxTotalFrames constant (1024) to cap the total number of physical frames walked. This is high enough for normal stack traces while preventing multi-second delays in CPU profiling and other stack walking scenarios. Fixes #75583
1 parent 2a71af1 commit 49e1dc9

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"runtime"
11+
"time"
12+
)
13+
14+
func init() {
15+
register("DeepContextChain", DeepContextChain)
16+
}
17+
18+
// DeepContextChain tests that traceback completes in reasonable time
19+
// even with very deep context chains (issue #75583).
20+
func DeepContextChain() {
21+
// Create a context chain deep enough to trigger the frame limit.
22+
// We use 2000 layers to ensure we exceed the limit (1024) while
23+
// keeping the test fast.
24+
const depth = 2000
25+
ctx := context.Background()
26+
for i := 0; i < depth; i++ {
27+
ctx = context.WithValue(ctx, i, i)
28+
}
29+
30+
// Start profiling to trigger stack walking with deep context
31+
start := time.Now()
32+
33+
// Get a stack trace multiple times
34+
// This simulates what happens during CPU profiling
35+
for i := 0; i < 10; i++ {
36+
var pcs [64]uintptr
37+
n := runtime.Callers(0, pcs[:])
38+
if n == 0 {
39+
fmt.Println("FAIL: got 0 callers")
40+
return
41+
}
42+
43+
// Call Deadline to ensure the deep context chain is traversed
44+
// during any potential stack walking
45+
_, _ = ctx.Deadline()
46+
}
47+
48+
elapsed := time.Since(start)
49+
50+
// The test should complete quickly. If it takes more than 1 second,
51+
// something is wrong (likely walking too many frames).
52+
if elapsed > time.Second {
53+
fmt.Printf("FAIL: test took %v, expected < 1s\n", elapsed)
54+
return
55+
}
56+
57+
fmt.Println("OK")
58+
}

src/runtime/traceback.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,18 @@ func (u *unwinder) cgoCallers(pcBuf []uintptr) int {
620620
func tracebackPCs(u *unwinder, skip int, pcBuf []uintptr) int {
621621
var cgoBuf [32]uintptr
622622
n := 0
623+
// maxTotalFrames limits the total number of frames we'll walk through,
624+
// including wrapper frames. This prevents excessive CPU time when
625+
// unwinding stacks with very deep context chains (issue #75583).
626+
// The limit of 1024 is high enough for normal stacks while preventing
627+
// multi-second delays from walking millions of wrapper frames.
628+
const maxTotalFrames = 1024
629+
totalFrames := 0
623630
for ; n < len(pcBuf) && u.valid(); u.next() {
631+
totalFrames++
632+
if totalFrames >= maxTotalFrames {
633+
break
634+
}
624635
f := u.frame.fn
625636
cgoN := u.cgoCallers(cgoBuf[:])
626637

src/runtime/traceback_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,3 +882,16 @@ func TestSetCgoTracebackNoCgo(t *testing.T) {
882882
t.Fatalf("want %s, got %s\n", want, output)
883883
}
884884
}
885+
886+
// TestDeepContextChainTraceback tests that tracebackPCs completes in reasonable
887+
// time even with very deep context chains (issue #75583).
888+
// This test creates a context chain deep enough to trigger the frame limit
889+
// and verifies that tracing completes quickly.
890+
func TestDeepContextChainTraceback(t *testing.T) {
891+
output := runTestProg(t, "testprog", "DeepContextChain")
892+
if !strings.Contains(output, "OK") {
893+
t.Fatalf("expected OK, got:\n%s", output)
894+
}
895+
// The test should complete in reasonable time. If it hangs or takes
896+
// multiple seconds, the frame limit is not working.
897+
}

0 commit comments

Comments
 (0)