v0.1.150 nil pointer dereference panic in openAICompactKeepaliveWriter when compact fails
版本: v0.1.150
触发条件: gpt-5.6-sol / gpt-5.6-terra 的 remote compact 请求失败时(upstream 返回 502)
错误日志:
http: panic serving: runtime error: invalid memory address or nil pointer dereference
goroutine [running]:
github.com/Wei-Shaw/sub2api/internal/handler.(*opsCaptureWriter).Written(0x330c600?)
panic({0x32cda00?, 0x69d9690?})
github.com/Wei-Shaw/sub2api/internal/handler.(*opsCaptureWriter).Status(0x5?)
关键位置:
- openai_compact_sse_keepalive.go:220 — Status() 方法
- openai_compact_sse_keepalive.go:232 — Written() 方法
- opsCaptureWriter 内部指针为 nil
复现步骤:
- 使用 gpt-5.6-sol 模型
- 触发 remote compact(上下文较长时自动触发)
- compact 请求上游返回 502 时,logger middleware 尝试访问 response writer 的 Status()/Written() 导致 panic
影响: 每次 compact 失败都会产生 panic,被 gin recovery 捕获后继续运行,但日志中会有大量 panic stacktrace。compact 失败本身返回 502 给客户端行为正常,但进程稳定性受影响。
关键位置:
- openai_compact_sse_keepalive.go:220 — Status() 方法
- openai_compact_sse_keepalive.go:232 — Written() 方法
- opsCaptureWriter 内部指针为 nil
复现步骤:
- 使用 gpt-5.6-sol 模型
- 触发 remote compact(上下文较长时自动触发)
- compact 请求上游返回 502 时(compact请求gpt-5.6失败本身已经0.1.150被修复,失败可能是网络波动问题),logger middleware 尝试访问 response writer 的 Status()/Written() 导致 panic
影响: compact 失败会产生 panic,被 gin recovery 捕获后继续运行,但日志中会有大量 panic stacktrace。compact 失败本身返回 502 给客户端行为正常,但进程稳定性受影响。
我使用 gpt5.5 也同样遇到这个问题
同样遇到这个问题。几乎每次compact都会失败,无论是gpt5.6 还是 5.5 5.4。 客户端将会显示 Stream disconnected before completion: Transport error: network error: error decoding response body. 服务端log是
http: panic serving 127.0.0.1:44942: runtime error: invalid memory address or nil pointer dereference goroutine 35014 [running]: net/http.(*conn).serve.func1() /opt/hostedtoolcache/go/1.26.5/x64/src/net/http/server.go:1907 +0xbd panic({0x32cda00?, 0x69d9690?}) /opt/hostedtoolcache/go/1.26.5/x64/src/runtime/panic.go:860 +0x13a github.com/Wei-Shaw/sub2api/internal/handler.(*opsCaptureWriter).Written(0x330c600?) <autogenerated>:1 +0x15 github.com/Wei-Shaw/sub2api/internal/service.(*openAICompactKeepaliveWriter).Written(0x4082bc0?) /home/runner/work/sub2api/sub2api/backend/internal/service/openai_compact_sse_keepalive.go:232 +0x82 github.com/Wei-Shaw/sub2api/internal/server.ProvideRouter.Recovery.func2(0x3cff3588400, {0x32cda00?, 0x69d9690}) /home/runner/work/sub2api/sub2api/backend/internal/server/middleware/recovery.go:31 +0x88 github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1.1() /home/runner/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:98 +0x6ff panic({0x32cda00?, 0x69d9690?}) /opt/hostedtoolcache/go/1.26.5/x64/src/runtime/panic.go:860 +0x13a github.com/Wei-Shaw/sub2api/internal/handler.(*opsCaptureWriter).Status(0x5?) <autogenerated>:1 +0x15 github.com/Wei-Shaw/sub2api/internal/service.(*openAICompactKeepaliveWriter).Status(0xc28c49532f8c2fc7?) /home/runner/work/sub2api/sub2api/backend/internal/service/openai_compact_sse_keepalive.go:220 +0x89 github.com/Wei-Shaw/sub2api/internal/server.SetupRouter.Logger.func5(0x3cff3588400) /home/runner/work/sub2api/sub2api/backend/internal/server/middleware/logger.go:34 +0x13a github.com/gin-gonic/gin.(*Context).Next(0x3cff3588400) /home/runner/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 +0x2b github.com/Wei-Shaw/sub2api/internal/server.SetupRouter.RequestLogger.func4(0x3cff3588400) /home/runner/work/sub2api/sub2api/backend/internal/server/middleware/request_logger.go:43 +0x65c github.com/gin-gonic/gin.(*Context).Next(...) /home/runner/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1(0x3cff3588400) /home/runner/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:102 +0x6f github.com/gin-gonic/gin.(*Context).Next(...) /home/runner/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0x3cff05bc680, 0x3cff3588400) /home/runner/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:620 +0x65b github.com/gin-gonic/gin.(*Engine).ServeHTTP(0x3cff05bc680, {0x40a0910, 0x3cff37bdd10}, 0x3cff04a7b80) /home/runner/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:576 +0x1ad github.com/Wei-Shaw/sub2api/internal/server.ProvideHTTPServer.MaxBytesHandler.func1({0x40a0910, 0x3cff37bdd10}, 0x3cff04a7900) /opt/hostedtoolcache/go/1.26.5/x64/src/net/http/server.go:4071 +0x12a net/http.HandlerFunc.ServeHTTP(0x425de5?, {0x40a0910?, 0x3cff37bdd10?}, 0x3cff37bdd01?) /opt/hostedtoolcache/go/1.26.5/x64/src/net/http/server.go:2286 +0x29 net/http.serverHandler.ServeHTTP({0x4093778?}, {0x40a0910?, 0x3cff37bdd10?}, 0x6?) /opt/hostedtoolcache/go/1.26.5/x64/src/net/http/server.go:3311 +0x8e net/http.(*conn).serve(0x3cff01758c0, {0x40a44a8, 0x3cff01ec720}) /opt/hostedtoolcache/go/1.26.5/x64/src/net/http/server.go:2073 +0x650 created by net/http.(*Server).Serve in goroutine 1150 /opt/hostedtoolcache/go/1.26.5/x64/src/net/http/server.go:3464 +0x485