fix edge cases refCounter close channel twice

This commit is contained in:
yusing
2025-01-05 09:15:03 +08:00
parent b7802f4e3e
commit 70c8c4b4aa
2 changed files with 39 additions and 16 deletions

View File

@@ -24,11 +24,31 @@ func (rc *RefCount) Zero() <-chan struct{} {
}
func (rc *RefCount) Add() {
atomic.AddUint32(&rc.refCount, 1)
// We add before checking to ensure proper ordering
newV := atomic.AddUint32(&rc.refCount, 1)
if newV == 1 {
// If it was 0 before we added, that means we're incrementing after a close
// This is a programming error
panic("RefCount.Add() called after count reached zero")
}
}
func (rc *RefCount) Sub() {
if atomic.AddUint32(&rc.refCount, ^uint32(0)) == 0 {
close(rc.zeroCh)
// First read the current value
for {
current := atomic.LoadUint32(&rc.refCount)
if current == 0 {
// Already at zero, channel should be closed
return
}
// Try to decrement, but only if the value hasn't changed
if atomic.CompareAndSwapUint32(&rc.refCount, current, current-1) {
if current == 1 { // Was this the last reference?
close(rc.zeroCh)
}
return
}
// If CAS failed, someone else modified the count, try again
}
}