bug(upstream): 并发关闭响应体导致后续响应 EOF 阻塞(Go 1.27)
问题描述
sub2api 公共 HTTP 上游层没有为每次请求建立由响应体持有的取消生命周期。后台读取与提前 Body.Close() 重叠时,在 Go 1.27.0 的 HTTP/1 连接复用场景下,旧读取协程可能继续等待 EOF,继而阻塞后续响应的收尾。
可见症状是:上游已经结束 HTTP 响应,sub2api 仍未结束当前请求,总耗时异常增大,响应体关闭和 inFlight 释放延迟。这是 sub2api 的响应体生命周期处理缺陷,可用本地合成 SSE 服务复现。
影响版本与触发条件
- 截至 2026-09-18 核对:最新 release v0.2.5(
86f93c28)及最新 mainefe9aab1e4ec89a42ba45e8dac20e882c5409a6a均可复现,工具链为 Go 1.27.0。以下源码前后量化对照统一以该最新 main 为基线。 - 触发组合:HTTP/1 keep-alive、后台 reader 仍在读取、消费方提前关闭响应体。
- 正常完整消费响应不必然触发;本 issue 不把所有长耗时请求归因于这一问题。
预期行为
关闭当前响应时,sub2api 应先取消该次上游请求,使活跃 reader 退出,再关闭响应体及解压器。该请求的收尾不应阻塞后续响应,也不应取消调用方用于重试的 context。完整消费后的连接复用、HTTP/2 其他流以及客户端断开后收集 usage 的行为应保持有效。
修复前后对照
| 同一套仓库回归测试 | 修复前 efe9aab1 |
修复后 3fdd54ca |
|---|---|---|
| HTTP,重复 3 次 | 3/3 超时失败 | 3/3 通过 |
| HTTPS,重复 3 次 | 3/3 超时失败 | 3/3 通过 |
| 完成全部 8 个后续响应的测试轮次 | 0/6 | 6/6 |
环境为 Go 1.27.0、darwin/arm64,使用真实回环 HTTP transport、单连接池和相同的 5 秒 EOF 等待上限。基线仅通过 Go overlay 将 http_upstream.go 替换为 efe9aab1 的原文件,测试代码及其他源码完全相同。6 次基线均报 response EOF waits for a subsequent request;修复后的每轮测试(含 8 个后续响应)耗时不超过 0.02 秒,以 go test -json 的记录精度为准。该结果是本地回归测试结果,不是生产延迟 SLA。
对应源码、测试、微基准和修复边界见 PR #7303。
最小复现
以下标准库程序用于单独验证触发机制:启动本地 SSE 服务,每笔响应约 20ms;制造一次并发读取/提前关闭,然后在单连接池上发送 8 笔请求。保存为 main.go,执行 GOTOOLCHAIN=go1.27.0 go run main.go。
| 标准库对照条件 | 主动清理连接前完成数 | 仍等待的后续请求 |
|---|---|---|
| keep-alive,直接 Close | 7/8 | 1 |
| keep-alive,先 cancel 再 Close | 8/8 | 0 |
| 禁用 keep-alive,直接 Close | 8/8 | 0 |
这组标准库对照支持 cancel-before-close 的修复方向;上面的仓库测试验证 sub2api 实际源码的前后差异。Go 上游是否正式确认对应标准库缺陷,不作为本 issue 的前提。
标准库复现代码
package main
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"runtime"
"sync"
"time"
)
type result struct {
n int
elapsed time.Duration
err error
}
func run(disableKeepAlive bool, cancelBeforeClose bool) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
fmt.Fprintln(w, "data: begin\n")
w.(http.Flusher).Flush()
time.Sleep(20 * time.Millisecond)
fmt.Fprintln(w, "data: complete\n")
}))
defer srv.Close()
tr := &http.Transport{MaxIdleConns: 1, MaxIdleConnsPerHost: 1, MaxConnsPerHost: 1, IdleConnTimeout: 10 * time.Second, DisableKeepAlives: disableKeepAlive}
defer tr.CloseIdleConnections()
cli := &http.Client{Transport: tr}
fmt.Printf("scenario keepalive=%v cancel_before_close=%v\n", !disableKeepAlive, cancelBeforeClose)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
resp, err := cli.Do(req)
if err != nil {
panic(err)
}
first := make(chan struct{})
go func() { io.Copy(io.Discard, resp.Body); close(first) }()
time.Sleep(5 * time.Millisecond)
if cancelBeforeClose {
cancel()
}
resp.Body.Close()
time.Sleep(60 * time.Millisecond)
results := make(chan result, 8)
var wg sync.WaitGroup
for n := 0; n < 8; n++ {
r, e := cli.Get(srv.URL)
if e != nil {
panic(e)
}
wg.Add(1)
go func(n int, r *http.Response) {
defer wg.Done()
start := time.Now()
_, e := io.Copy(io.Discard, r.Body)
r.Body.Close()
results <- result{n, time.Since(start), e}
}(n, r)
time.Sleep(45 * time.Millisecond)
}
time.Sleep(500 * time.Millisecond)
completed := len(results)
fmt.Printf("before_cleanup completed=%d/8 stuck=%d\n", completed, 8-completed)
tr.CloseIdleConnections()
end := make(chan struct{})
go func() { wg.Wait(); close(end) }()
select {
case <-end:
case <-time.After(2 * time.Second):
fmt.Println("cleanup_timeout")
os.Exit(1)
}
close(results)
for r := range results {
fmt.Printf("request=%d body_read_ms=%.3f error=%v\n", r.n, float64(r.elapsed.Microseconds())/1000, r.err)
}
select {
case <-first:
fmt.Println("initial_reader=done")
default:
fmt.Println("initial_reader=stuck")
}
}
func main() {
fmt.Println("toolchain", runtime.Version(), "platform", runtime.GOOS, runtime.GOARCH)
run(false, false)
run(true, false)
run(false, true)
}
关联工作
- #7303:本 issue 的修复和回归测试。
- #6754:已在一条 streaming Chat Completions 路径采用 cancel-before-close;公共
Do/DoWithTLS仍缺少统一的请求收尾。 - #6473:处理 SSE terminal event 的业务结束时机;本 issue 聚焦并发关闭后,连接复用导致的后续响应 EOF 等待。
- Related to #5910:该 issue 的阶段诊断需求范围更广。