Skip to content

Commit

Permalink
semaphore: unblock waiters when the front waiter cancels
Browse files Browse the repository at this point in the history
When `Release`, if the remaining tokens are not enough for the front waiter, no waiters will be notified.

So if the canceled waiter is the front one, it should try to notify following waiters if there are remaining tokens.

I found this bug when implementing a cancelable rwmutex based on semaphore:

https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex.go

This bug can be verified by this test:

https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex_test.go#L43

Change-Id: Id8564976bd375a82c4fbc6cb08b0bb83118a346c
GitHub-Last-Rev: 29b6ff26bf779d23239cfe64a395378a9e41d1fc
GitHub-Pull-Request: golang/sync#10
Reviewed-on: https://go-review.googlesource.com/c/sync/+/223418
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
  • Loading branch information
anatoliinzrnk committed Mar 17, 2020
1 parent 0c41d52 commit 92bc329
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
11 changes: 10 additions & 1 deletion semaphore/semaphore.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func (s *Weighted) Acquire(ctx context.Context, n int64) error {
// fix up the queue, just pretend we didn't notice the cancelation.
err = nil
default:
isFront := s.waiters.Front() == elem
s.waiters.Remove(elem)
// If we're at the front and there're extra tokens left, notify other waiters.
if isFront && s.size > s.cur {
s.notifyWaiters()
}
}
s.mu.Unlock()
return err
Expand Down Expand Up @@ -97,6 +102,11 @@ func (s *Weighted) Release(n int64) {
s.mu.Unlock()
panic("semaphore: released more than held")
}
s.notifyWaiters()
s.mu.Unlock()
}

func (s *Weighted) notifyWaiters() {
for {
next := s.waiters.Front()
if next == nil {
Expand All @@ -123,5 +133,4 @@ func (s *Weighted) Release(n int64) {
s.waiters.Remove(next)
close(w.ready)
}
s.mu.Unlock()
}
31 changes: 31 additions & 0 deletions semaphore/semaphore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,34 @@ func TestLargeAcquireDoesntStarve(t *testing.T) {
sem.Release(n)
wg.Wait()
}

// translated from https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex_test.go#L43
func TestAllocCancelDoesntStarve(t *testing.T) {
sem := semaphore.NewWeighted(10)

// Block off a portion of the semaphore so that Acquire(_, 10) can eventually succeed.
sem.Acquire(context.Background(), 1)

// In the background, Acquire(_, 10).
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
sem.Acquire(ctx, 10)
}()

// Wait until the Acquire(_, 10) call blocks.
for sem.TryAcquire(1) {
sem.Release(1)
runtime.Gosched()
}

// Now try to grab a read lock, and simultaneously unblock the Acquire(_, 10) call.
// Both Acquire calls should unblock and return, in either order.
go cancel()

err := sem.Acquire(context.Background(), 1)
if err != nil {
t.Fatalf("Acquire(_, 1) failed unexpectedly: %v", err)
}
sem.Release(1)
}

0 comments on commit 92bc329

Please sign in to comment.