Skip to content

修复claudeResponse流式请求空指针Panic - #2510

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/0e7050dc89c1b761069f5e528d8ecf786e7008ae
Dec 24, 2025
Merged

修复claudeResponse流式请求空指针Panic#2510
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/0e7050dc89c1b761069f5e528d8ecf786e7008ae

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Dec 24, 2025

Copy link
Copy Markdown
Member

claude在流式返回某些情况下StopReason为nil,
该错误由于没有recover, 所以会导致panic后整个程序完全退出
如果使用docker守护则表现为docker重启,
需要尽快修复!

panic日志:
new-api-1 | panic: runtime error: invalid memory address or nil pointer dereference
new-api-1 | [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x11411f0]
new-api-1 |
new-api-1 | goroutine 1616088 [running]:
new-api-1 | github.com/QuantumNous/new-api/relay/channel/claude.StreamResponseClaude2OpenAI(0x2, 0xc0009020b0)
new-api-1 | /build/relay/channel/claude/relay-claude.go:486 +0x750
new-api-1 | github.com/QuantumNous/new-api/relay/channel/claude.HandleStreamResponseData(0xc00029ab00, 0xc001fd6608, 0xc001d335c0, {0xc0002960d6, 0xc0}, 0x2)
new-api-1 | /build/relay/channel/claude/relay-claude.go:659 +0x26e
new-api-1 | github.com/QuantumNous/new-api/relay/channel/claude.ClaudeStreamHandler.func1({0xc0002960d6?, 0xc001a14080?})
new-api-1 | /build/relay/channel/claude/relay-claude.go:713 +0x3d
new-api-1 | github.com/QuantumNous/new-api/relay/helper.StreamScannerHandler.func4.2()
new-api-1 | /build/relay/helper/stream_scanner.go:228 +0xaa
new-api-1 | created by github.com/QuantumNous/new-api/relay/helper.StreamScannerHandler.func4 in goroutine 1583058
new-api-1 | /build/relay/helper/stream_scanner.go:225 +0x426

Summary by CodeRabbit

Bug Fixes

  • Improved system reliability by adding proper validation checks in response handling to prevent potential crashes when processing certain data edge cases.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Dec 24, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The pull request adds nil-safety checks in the "message_delta" path of StreamResponseClaude2OpenAI to guard against potential nil pointer dereferences when accessing claudeResponse.Delta.StopReason, only conditionally setting FinishReason when both the Delta and StopReason fields exist and have valid values.

Changes

Cohort / File(s) Summary
Claude relay nil-safety guards
relay/channel/claude/relay-claude.go
Added nil-checks for claudeResponse.Delta.StopReason in the message_delta handling path; only dereferences and assigns FinishReason when Delta is non-nil, StopReason is non-nil, and the value is not "null"

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • #2035: Adds nil-safe handling for Thinking in the thinking_delta path of the same StreamResponseClaude2OpenAI function, applying similar defensive patterns.
  • #1761: Modifies StreamResponseClaude2OpenAI to remap Claude stop reasons to OpenAI finish_reason values, affecting the same stop reason handling logic.

Suggested reviewers

  • seefs001
  • Calcium-Ion

Poem

🐰 A hop through the code, guards standing tall,
Nil checks prevent the dreaded crash and fall,
StopReason whispers, but only if it's there,
No more dereferencing into thin air! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title in Chinese clearly describes the fix: addressing nil pointer panic in Claude's streaming requests for claudeResponse. It directly relates to the changeset which adds nil-checks for StopReason to prevent dereference panics.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 42109c5 and 3652dfd.

📒 Files selected for processing (1)
  • relay/channel/claude/relay-claude.go
🔇 Additional comments (1)
relay/channel/claude/relay-claude.go (1)

485-491: LGTM! Critical panic fix correctly implemented.

The nil checks for claudeResponse.Delta and claudeResponse.Delta.StopReason properly prevent the nil pointer dereference that was causing panics in streaming responses. The pattern is consistent with the existing nil check for claudeResponse.Delta in the content_block_delta case (line 466).


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Calcium-Ion
Calcium-Ion merged commit 12555a3 into QuantumNous:main Dec 24, 2025
1 check passed
@coderabbitai coderabbitai Bot mentioned this pull request Feb 2, 2026
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…761069f5e528d8ecf786e7008ae

修复claudeResponse流式请求空指针Panic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants