Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 896e347

Browse files
authored
Merge pull request #701 from Random-Liu/fix-event-monitor-panic
Fix event monitor panic.
2 parents 796cae7 + 277edb2 commit 896e347

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pkg/server/events.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package server
1818

1919
import (
20+
"sync"
2021
"time"
2122

2223
eventtypes "github.com/containerd/containerd/api/events"
@@ -56,7 +57,9 @@ type eventMonitor struct {
5657
}
5758

5859
type backOff struct {
59-
queuePool map[string]*backOffQueue
60+
queuePool map[string]*backOffQueue
61+
// tickerMu is mutex used to protect the ticker.
62+
tickerMu sync.Mutex
6063
ticker *time.Ticker
6164
minDuration time.Duration
6265
maxDuration time.Duration
@@ -120,8 +123,8 @@ func (em *eventMonitor) start() (<-chan struct{}, error) {
120123
return nil, errors.New("event channel is nil")
121124
}
122125
closeCh := make(chan struct{})
126+
backOffCheckCh := em.backOff.start()
123127
go func() {
124-
backOffCheckCh := em.backOff.start()
125128
for {
126129
select {
127130
case e := <-em.ch:
@@ -366,12 +369,18 @@ func (b *backOff) reBackOff(key string, events []interface{}, oldDuration time.D
366369
}
367370

368371
func (b *backOff) start() <-chan time.Time {
372+
b.tickerMu.Lock()
373+
defer b.tickerMu.Unlock()
369374
b.ticker = time.NewTicker(b.checkDuration)
370375
return b.ticker.C
371376
}
372377

373378
func (b *backOff) stop() {
374-
b.ticker.Stop()
379+
b.tickerMu.Lock()
380+
defer b.tickerMu.Unlock()
381+
if b.ticker != nil {
382+
b.ticker.Stop()
383+
}
375384
}
376385

377386
func newBackOffQueue(events []interface{}, init time.Duration, c clock.Clock) *backOffQueue {

0 commit comments

Comments
 (0)