Skip to content

Commit e201581

Browse files
authored
les: fix goroutine leak in execQueue (#14480)
execQueue used an atomic counter to track whether the queue had been closed, but the checking the counter didn't happen because the queue was blocked on its channel. Fix it by using a condition variable instead of sync/atomic. I tried an implementation based on channels first, but it was hard to make it reliable. quit now waits for the queue loop to exit.
1 parent ef7b9fb commit e201581

2 files changed

Lines changed: 118 additions & 30 deletions

File tree

‎les/execqueue.go‎

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,82 @@
1616

1717
package les
1818

19-
import (
20-
"sync/atomic"
21-
)
19+
import "sync"
2220

23-
// ExecQueue implements a queue that executes function calls in a single thread,
21+
// execQueue implements a queue that executes function calls in a single thread,
2422
// in the same order as they have been queued.
2523
type execQueue struct {
26-
chn chan func()
27-
cnt, stop, capacity int32
24+
mu sync.Mutex
25+
cond *sync.Cond
26+
funcs []func()
27+
closeWait chan struct{}
2828
}
2929

30-
// NewExecQueue creates a new execution queue.
31-
func newExecQueue(capacity int32) *execQueue {
32-
q := &execQueue{
33-
chn: make(chan func(), capacity),
34-
capacity: capacity,
35-
}
30+
// newExecQueue creates a new execution queue.
31+
func newExecQueue(capacity int) *execQueue {
32+
q := &execQueue{funcs: make([]func(), 0, capacity)}
33+
q.cond = sync.NewCond(&q.mu)
3634
go q.loop()
3735
return q
3836
}
3937

4038
func (q *execQueue) loop() {
41-
for f := range q.chn {
42-
atomic.AddInt32(&q.cnt, -1)
43-
if atomic.LoadInt32(&q.stop) != 0 {
44-
return
45-
}
39+
for f := q.waitNext(false); f != nil; f = q.waitNext(true) {
4640
f()
4741
}
42+
close(q.closeWait)
4843
}
4944

50-
// CanQueue returns true if more function calls can be added to the execution queue.
45+
func (q *execQueue) waitNext(drop bool) (f func()) {
46+
q.mu.Lock()
47+
if drop {
48+
// Remove the function that just executed. We do this here instead of when
49+
// dequeuing so len(q.funcs) includes the function that is running.
50+
q.funcs = append(q.funcs[:0], q.funcs[1:]...)
51+
}
52+
for !q.isClosed() {
53+
if len(q.funcs) > 0 {
54+
f = q.funcs[0]
55+
break
56+
}
57+
q.cond.Wait()
58+
}
59+
q.mu.Unlock()
60+
return f
61+
}
62+
63+
func (q *execQueue) isClosed() bool {
64+
return q.closeWait != nil
65+
}
66+
67+
// canQueue returns true if more function calls can be added to the execution queue.
5168
func (q *execQueue) canQueue() bool {
52-
return atomic.LoadInt32(&q.stop) == 0 && atomic.LoadInt32(&q.cnt) < q.capacity
69+
q.mu.Lock()
70+
ok := !q.isClosed() && len(q.funcs) < cap(q.funcs)
71+
q.mu.Unlock()
72+
return ok
5373
}
5474

55-
// Queue adds a function call to the execution queue. Returns true if successful.
75+
// queue adds a function call to the execution queue. Returns true if successful.
5676
func (q *execQueue) queue(f func()) bool {
57-
if atomic.LoadInt32(&q.stop) != 0 {
58-
return false
77+
q.mu.Lock()
78+
ok := !q.isClosed() && len(q.funcs) < cap(q.funcs)
79+
if ok {
80+
q.funcs = append(q.funcs, f)
81+
q.cond.Signal()
5982
}
60-
if atomic.AddInt32(&q.cnt, 1) > q.capacity {
61-
atomic.AddInt32(&q.cnt, -1)
62-
return false
63-
}
64-
q.chn <- f
65-
return true
83+
q.mu.Unlock()
84+
return ok
6685
}
6786

68-
// Stop stops the exec queue.
87+
// quit stops the exec queue.
88+
// quit waits for the current execution to finish before returning.
6989
func (q *execQueue) quit() {
70-
atomic.StoreInt32(&q.stop, 1)
90+
q.mu.Lock()
91+
if !q.isClosed() {
92+
q.closeWait = make(chan struct{})
93+
q.cond.Signal()
94+
}
95+
q.mu.Unlock()
96+
<-q.closeWait
7197
}

‎les/execqueue_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package les
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestExecQueue(t *testing.T) {
24+
var (
25+
N = 10000
26+
q = newExecQueue(N)
27+
counter int
28+
execd = make(chan int)
29+
testexit = make(chan struct{})
30+
)
31+
defer q.quit()
32+
defer close(testexit)
33+
34+
check := func(state string, wantOK bool) {
35+
c := counter
36+
counter++
37+
qf := func() {
38+
select {
39+
case execd <- c:
40+
case <-testexit:
41+
}
42+
}
43+
if q.canQueue() != wantOK {
44+
t.Fatalf("canQueue() == %t for %s", !wantOK, state)
45+
}
46+
if q.queue(qf) != wantOK {
47+
t.Fatalf("canQueue() == %t for %s", !wantOK, state)
48+
}
49+
}
50+
51+
for i := 0; i < N; i++ {
52+
check("queue below cap", true)
53+
}
54+
check("full queue", false)
55+
for i := 0; i < N; i++ {
56+
if c := <-execd; c != i {
57+
t.Fatal("execution out of order")
58+
}
59+
}
60+
q.quit()
61+
check("closed queue", false)
62+
}

0 commit comments

Comments
 (0)