Skip to content

Commit 157cd11

Browse files
authored
Bugfix: conditional variable broadcast channel size (#3906)
* Conditional variable broadcast function should create channel with size 1, not 0
1 parent a070fb5 commit 157cd11

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

common/locks/condition_variable_impl.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewConditionVariable(
4646
lock: lock,
4747

4848
chanLock: sync.Mutex{},
49-
channel: make(chan struct{}, 1),
49+
channel: newCVChannel(),
5050
}
5151
}
5252

@@ -64,7 +64,7 @@ func (c *ConditionVariableImpl) Signal() {
6464

6565
// Broadcast wakes all goroutines waiting on this condition variable.
6666
func (c *ConditionVariableImpl) Broadcast() {
67-
newChannel := make(chan struct{})
67+
newChannel := newCVChannel()
6868

6969
c.chanLock.Lock()
7070
defer c.chanLock.Unlock()
@@ -104,3 +104,7 @@ func (c *ConditionVariableImpl) Wait(
104104
// interrupted
105105
}
106106
}
107+
108+
func newCVChannel() chan struct{} {
109+
return make(chan struct{}, 1)
110+
}

common/locks/condition_variable_test.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type (
4040
suite.Suite
4141

4242
lock sync.Locker
43-
cv ConditionVariable
43+
cv *ConditionVariableImpl
4444
}
4545
)
4646

@@ -67,6 +67,34 @@ func (s *conditionVariableSuite) TearDownTest() {
6767

6868
}
6969

70+
func (s *conditionVariableSuite) TestChannelSize_New() {
71+
s.testChannelSize(s.cv.channel)
72+
}
73+
74+
func (s *conditionVariableSuite) TestChannelSize_Broadcast() {
75+
s.cv.Broadcast()
76+
s.testChannelSize(s.cv.channel)
77+
}
78+
79+
func (s *conditionVariableSuite) testChannelSize(
80+
channel chan struct{},
81+
) {
82+
// assert channel size == 1
83+
select {
84+
case channel <- struct{}{}:
85+
// noop
86+
default:
87+
s.Fail("conditional variable size should be 1")
88+
}
89+
90+
select {
91+
case channel <- struct{}{}:
92+
s.Fail("conditional variable size should be 1")
93+
default:
94+
// noop
95+
}
96+
}
97+
7098
func (s *conditionVariableSuite) TestSignal() {
7199
signalWaitGroup := sync.WaitGroup{}
72100
signalWaitGroup.Add(1)

0 commit comments

Comments
 (0)