Skip to content

Commit

Permalink
stomp: fix Unsubscribe race
Browse files Browse the repository at this point in the history
I think this is OK because the only point that wakes the `sync.Cond` is
_after_ closing the channel and updating the `status` member. There
should be no way to close the channel _before_ updating the status. If
there were, this code would be racy again.

Signed-off-by: Hank Donnay <[email protected]>
  • Loading branch information
hdonnay committed Aug 26, 2024
1 parent 9a13a1a commit 9165d8a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ func (s *Subscription) Unsubscribe(opts ...func(*frame.Frame) error) error {
for atomic.LoadInt32(&s.state) != subStateClosed {
err = waitWithTimeout(s.closeCond, s.unsubscribeReceiptTimeout)
if err != nil && errors.Is(err, &ErrUnsubscribeReceiptTimeout) {
msg := s.subscriptionErrorMessage("channel unsubscribe receipt timeout")
s.C <- msg
// The [closeCond.Broadcast] can race with the timeout, so make sure
// the channel is still available.
if atomic.LoadInt32(&s.state) != subStateClosed {
msg := s.subscriptionErrorMessage("channel unsubscribe receipt timeout")
s.C <- msg
}
return err
}
}
Expand Down

0 comments on commit 9165d8a

Please sign in to comment.