mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
fix: orDone 使用 sync.Once 修复 close(done) 竞态条件
修复 Gemini 审查意见:多 goroutine 同时 close(done) 可能导致 panic。 恢复 sync.Once 保证 channel 只被关闭一次。 Alina Agent生成
This commit is contained in:
parent
2d693e3b13
commit
9dcab4b1ae
1 changed files with 3 additions and 5 deletions
|
|
@ -6,6 +6,7 @@ package touka
|
|||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -120,15 +121,12 @@ func (mc *mergedContext) Value(key any) any {
|
|||
// orDone 返回一个 channel, 当任意一个输入 context 的 Done() channel 关闭时关闭.
|
||||
func orDone(contexts ...context.Context) <-chan struct{} {
|
||||
done := make(chan struct{})
|
||||
var once sync.Once
|
||||
for _, ctx := range contexts {
|
||||
go func(c context.Context) {
|
||||
select {
|
||||
case <-c.Done():
|
||||
select {
|
||||
case <-done:
|
||||
default:
|
||||
close(done)
|
||||
}
|
||||
once.Do(func() { close(done) })
|
||||
case <-done:
|
||||
}
|
||||
}(ctx)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue