fix(handler): opsCaptureWriter 释放后 nil pointer panic#3994
Merged
Wei-Shaw merged 2 commits intoJul 10, 2026
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
Fixes #3993,关联 #3961
v0.1.150 compact 请求失败时 logger middleware 访问已释放的 opsCaptureWriter 导致 panic。多人复现,gpt-5.6/5.5 都中。
根因
releaseOpsCaptureWriter 把 w.ResponseWriter 设成 nil 放回 sync.Pool,但 openAICompactKeepaliveWriter 还持有对它的引用。logger middleware 在 handler 之后执行 c.Writer.Status(),调用链穿透到 nil 接口就 panic 了。
修改
给 opsCaptureWriter 加了 Status()、Size()、Written() 三个 nil 守卫方法,Write/WriteString 也加了守卫。释放后再调用这些方法返回零值而不是 panic。
测试
5 个断言覆盖了所有 5 个可能 panic 的方法。