fix(stream): OnClose 可在返回前注册,消除 Inject 循环等待死锁

旧实现 OnClose 先等待 ready,但 Inject 只有业务方法返回后才发生:业务同步 OnClose → 等 Inject;框架等业务返回 → 永久死锁,流首帧永不输出。改为仅持锁注册回调,不等待 channel 注入;已关闭场景锁外立即回调,避免重入自锁。新增两条回归测试。
This commit is contained in:
2026-09-16 13:54:00 +08:00
parent 94fded09d5
commit 81d36f827e
2 changed files with 54 additions and 3 deletions
+8 -3
View File
@@ -67,14 +67,19 @@ func (s *Stream) Close() {
close(s.ch)
}
// OnClose 注册关闭回调;流已关闭时立即执行
// OnClose 注册关闭回调;可在业务方法 return *Stream 之前同步调用。
// 不得等待 Inject:Inject 只有业务方法返回后才发生,等待会形成循环依赖死锁。
// 流已关闭时在锁外立即执行回调,避免回调重入 Stream 时自锁。
func (s *Stream) OnClose(cb func()) {
<-s.getReady()
if cb == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
s.mu.Unlock()
cb()
return
}
s.onClose = cb
s.mu.Unlock()
}