TTStream mux池淘汰连接列表时未关闭 transport,导致连接和 goroutine 累积引发内存泄漏

Aperta
#2,014 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Start with pkg/remote/trans/ttstream/client_trans_pool_muxconn.go, especially muxConnTransPool.Get(), Put(), and the background cleanup loop. Trace how lists track active streams and how concurrent Get() calls interact with eviction and transport closure; the issue notes that closing on deletion alone can interrupt active long streams. Done means safely closing evicted transports and releasing their goroutines and SharedTicker registrations without disrupting active streams; use the reproduction configuration and repeated stream batches to verify cleanup.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

问题位于 pkg/remote/trans/ttstream/client_trans_pool_muxconn.go,涉及 muxConnTransPool.Get() 和 Put() 中的后台清理逻辑。

Get() 按下游地址获取连接列表;不存在时创建:

func (p *muxConnTransPool) Get(
    network, addr string,
) (trans *clientTransport, err error) {
    v, ok := p.pool.Load(addr)
    if !ok {
        v, _ = p.pool.LoadOrStore(
            addr,
            newMuxConnTransList(p.config.PoolSize, p),
        )
    }
    return v.(*muxConnTransList).Get(network, addr)
}

stream 结束后调用 Put(),记录该地址最近一次归还时间。以下摘录省略了后台任务启动等代码:

func (p *muxConnTransPool) Put(trans *clientTransport) {
    p.activity.Store(trans.conn.RemoteAddr().String(), time.Now())

    // 后续代码启动后台清理循环。
}

后台循环判断超时后,仅执行:

p.pool.Delete(addr)
p.activity.Delete(addr)

这里移除了连接列表,但没有关闭其中的 transport。 后续 Get() 无法再复用这些旧连接,而会创建新的列表和连接:

Get 创建第一代 list 和连接
  → 请求完成,Put 更新 activity
  → 超时后删除 list 映射,但未关闭 transport
  → 旧 transport 被收发 goroutine、SharedTicker 引用,继续存活
  → 新请求 Get 创建第二代 list 和连接
  → 重复淘汰、重建,资源逐轮累积

list 本身可以被 GC;无法回收的是仍保持可达的 transport 及其关联资源。PoolSize 只限制每代列表,不能限制残留旧连接的总数。

To Reproduce

TTStream mux 客户端的关键配置如下,完整独立复现工程待补充:

ttstream.MuxConnConfig{
    PoolSize:       4,
    MaxIdleTimeout: 250 * time.Millisecond,
}
  1. 使用同一个客户端,分批向同一下游地址发送 stream 请求。
  2. 每批完整接收至 EOF,等待所有 handler 结束。
  3. 等待列表被空闲淘汰,再发送下一批,重复多轮。
  4. 停止请求,观察 TCP、goroutine profile,并执行强制 GC。

250ms 用于加速复现,不需要取消、业务错误或对端断连。

Expected behavior

连接列表被淘汰后,应安全完成 transport 关闭,释放连接、ticker 注册及相关 goroutine。

Actual behavior

每组完成 5,184 次正常 stream RPC。客户端、服务端位于同一测试进程:

配置 最终 TCP 连接 客户端循环 服务端循环,含 OnRead
原版 mux,开启淘汰 24 48 72
原版 mux,禁用淘汰 4,可复用 8 12
原版 stream 短连接 0 0 0

开启淘汰时,TCP 连接数逐轮增长:

4 → 8 → 12 → 16 → 20 → 24

停止请求、空闲 75 秒,再新增 11 次强制 GC 后:

  • 活跃 stream、handler 均为 0。
  • 六个已淘汰 list 的弱引用全部清空,两端各 24 个 transport 仍存活。
  • 同一批 120 个相关 goroutine ID 仍存在,与生命周期日志一致。
  • 24 个客户端 transport 仍注册在 SharedTicker 中。

诊断探针仅保存弱引用,不强引用这些对象。

Environment:

  • Kitex:v0.15.2
  • Netpoll:v0.7.2
  • Go:1.24.9
  • OS / Architecture:Linux arm64
  • Protocol:Thrift streaming
  • Transport:TTHeader Streaming / TTStream mux
  • 开启 race detector,本轮无 race 报告

Additional context

评估后认为不能简单在删除位置增加 Close():activity 记录的是最近一次 Put(),不代表列表中的所有流已经结束,仍可能存在活跃长流。修复需要同时协调使用状态、并发 Get() 和关闭过程。

截至 2026-10-20,v0.16.4 和 main 仍保留上述淘汰逻辑。实测结果来自 v0.15.2;最新版仅核对源码,尚未重新压测。

Lingua principale
Go
Stelle
8.1k
Fork
925
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudwego/kitex

Tutte le issue di cloudwego/kitex

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.