Skip to content

Commit

Permalink
fix: missing callback if subscribe after setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Aug 2, 2024
1 parent 4b35def commit fa67d0b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion future.go
Original file line number Diff line number Diff line change
@@ -115,7 +115,17 @@ func (s *state[T]) subscribe(cb func(T, error)) {

newCb.next = oldCb
if atomic.CompareAndSwapPointer(&s.stack, unsafe.Pointer(oldCb), unsafe.Pointer(newCb)) {
return
for {
// Double-check the state to ensure the callback is not missed
if ((atomic.LoadUint64(&s.state) & maskState) >> 32) == stateDone {
if atomic.CompareAndSwapPointer(&s.stack, unsafe.Pointer(newCb), unsafe.Pointer(newCb.next)) {
cb(s.val, s.err)
return
}
} else {
return
}
}
}
}
}

0 comments on commit fa67d0b

Please sign in to comment.