internal/json: the nesting depth check costs more than the decode on large messages
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Checklist
- I searched the existing issues, and this has not been reported before.
- This is a real problem that I ran into myself, using the latest release of the SDK.
- I ran into it in a production system, not in a toy example, a benchmark, or an automated or AI-driven scan of the code.
- I (a human) examined the relevant SDK code, and wrote or reviewed every line of this report. It is not unreviewed AI output.
- This report is brief and concise, and sticks to the facts needed to understand the problem.
What problem are you trying to solve?
internal/json.unmarshal runs checkMaxDepth (added in #1200) before every decode. The check walks every byte of the input with a small state machine, string contents included. Tool arguments and results are mostly string contents, so on large messages the check costs more than segmentio's parse itself.
It also runs many times per request on the same bytes. A stateless tools/call with JSON responses calls it 11 times; for a 64 KiB text argument it scans 590 KB, 9x the body.
On main (0fa5e84), Apple M2 Pro:
| time | |
|---|---|
checkMaxDepth, 64 KiB text message |
77.8 µs |
Unmarshal of the same message, depth check included |
100.1 µs |
stateless tools/call, 64 KiB text argument, end to end |
1024 µs |
In a CPU profile of the last case, checkMaxDepth is 82% of the time in internal/json.unmarshal.
Where did you hit this?
No response
What would help?
Inside a string only an unescaped " matters. Past the first few bytes of a string, the check can jump to the next quote with bytes.IndexByte (vectorized) and treat it as escaped when an odd number of backslashes precedes it. Short strings should stay on the byte loop: jumping on every quote is slower than the current scan when quotes come every few bytes (keys, escaped JSON). The result stays exactly the same, and a fuzz test can compare the new scan with the current byte-at-a-time one.
I have this change locally. It steps through the first 32 bytes of each string, then jumps:
checkMaxDepth, ~64 KiB message |
before | after |
|---|---|---|
| plain text | 77.8 µs | 1.02 µs |
| escaped code | 78.9 µs | 35.2 µs |
JSON inside a string ({\"key\":\"value\"}) |
76.6 µs | 39.3 µs |
| array of small objects | 118.4 µs | 58.6 µs |
| array of empty strings | 90.4 µs | 65.4 µs |
nothing but \" |
48.4 µs | 35.0 µs |
Unmarshal of the 64 KiB text message goes from 100.1 µs to 23.3 µs, and a stateless tools/call with a 64 KiB text argument from 1.02 ms to 0.33 ms end to end. No input gets slower; allocations are unchanged.
End-to-end benchmark (in package mcp)
func BenchmarkStatelessToolCall(b *testing.B) {
for _, n := range []int{1, 64 << 10} {
b.Run(map[int]string{1: "small", 64 << 10: "64KiB"}[n], func(b *testing.B) {
s := NewServer(&Implementation{Name: "s", Version: "1"}, nil)
type in struct {
Text string `json:"text"`
}
AddTool(s, &Tool{Name: "echo"}, func(context.Context, *CallToolRequest, in) (*CallToolResult, any, error) {
return &CallToolResult{}, nil, nil
})
h := NewStreamableHTTPHandler(func(*http.Request) *Server { return s }, &StreamableHTTPOptions{Stateless: true, JSONResponse: true})
body := `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"echo","arguments":{"text":"` + strings.Repeat("a", n) + `"}}}`
b.ReportAllocs()
for b.Loop() {
req := httptest.NewRequest("POST", "/", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
req.Header.Set("Mcp-Protocol-Version", "2025-11-25")
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Code != 200 {
b.Fatalf("%d %s", w.Code, w.Body.String())
}
}
})
}
}
Not running the check 11 times per request would help further, but it changes the call sites in mcp and jsonrpc2, so I'd keep it separate.
I'd like to send the fix. It's about 40 lines in internal/json, plus a test and a fuzz target that compare it with the current scan, and benchmarks for the inputs in the table.
I used an AI assistant to profile and draft this; I reproduced the numbers and reviewed the report myself.
- Ngôn ngữ chính
- Go
- Star
- 5.4k
- Fork
- 623
- Merge trung bình
- 22 giờ 10 phút
- Pull request đã merge (30 ngày)
- 30
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/go-sdk
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/go-sdk#1557 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Streamable HTTP (2026-07-28): tools/call with an out-of-range number in arguments fails because extractName decodes Arguments into anyCó thể làm lại được @beiryu đã nhận 10 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
modelcontextprotocol/go-sdk#1534 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Expose generic `SendNotification` on `ServerSession` for custom protocol extensionsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởneeds investigation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
modelcontextprotocol/go-sdk#745 · 13 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 18/100
modelcontextprotocol/go-sdk#1555 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
mcp: 2026-07-28 client runs elicitation and sampling handlers for standalone server requestsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
modelcontextprotocol/go-sdk#1552 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của modelcontextprotocol/go-sdk
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
apache/dubbo-admin#1659 ·
Maintainer thường phản hồi trong vòng 8 ngày
-
taskmanager: a failed v1alpha1 backup leaves the partial backup behindCó thể đã có người làm @arshil42 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Packaging: support mise-en-placeĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100