Fix RepeatTimer memory leak (#137)

fix RepeatTimer memory leak (Refs #137)

* test case

* drain channels on reset

Leaking memory:
```
leaktest.go:144: leaktest: leaked goroutine: goroutine 116 [chan send]:
        github.com/tendermint/tmlibs/common.(*RepeatTimer).fireRoutine(0xc42006a410, 0xc4203403c0, 0xc42031b2c0)
                /go/src/github.com/tendermint/tmlibs/common/repeat_timer.go:160 +0x6e
        created by github.com/tendermint/tmlibs/common.(*RepeatTimer).reset
                /go/src/github.com/tendermint/tmlibs/common/repeat_timer.go:196 +0xe9
```

The alternative solution could be draining channels on the client side.

* add one more select instead of draining

thanks to Jae
This commit is contained in:
Anton Kaliaev
2018-02-09 13:31:32 +04:00
committed by GitHub
parent 82ab92da9a
commit 52ce4c20f8
4 changed files with 41 additions and 2 deletions

View File

@ -155,7 +155,11 @@ func (t *RepeatTimer) fireRoutine(ch <-chan time.Time, quit <-chan struct{}) {
for {
select {
case t_ := <-ch:
t.ch <- t_
select {
case t.ch <- t_:
case <-quit:
return
}
case <-quit: // NOTE: `t.quit` races.
return
}
@ -210,7 +214,6 @@ func (t *RepeatTimer) stop() {
t.ticker.Stop()
t.ticker = nil
/*
XXX
From https://golang.org/pkg/time/#Ticker:
"Stop the ticker to release associated resources"
"After Stop, no more ticks will be sent"