fix memory leak caused by time.Timer

This commit is contained in:
aler9 2020-10-31 17:03:03 +01:00
parent 0495c1ed4b
commit 08823ddc92
3 changed files with 57 additions and 47 deletions

View File

@ -46,35 +46,34 @@ func (e *ExternalCmd) run() {
defer close(e.done)
for {
if !e.runInner() {
ok := func() bool {
ok := e.runInner()
if !ok {
return false
}
if !e.restart {
<-e.terminate
return false
}
t := time.NewTimer(restartPause)
defer t.Stop()
select {
case <-t.C:
return true
case <-e.terminate:
return false
}
}()
if !ok {
break
}
}
}
func (e *ExternalCmd) runInner() bool {
ok := e.runInnerInner()
if !ok {
return false
}
if !e.restart {
<-e.terminate
return false
}
t := time.NewTimer(restartPause)
defer t.Stop()
select {
case <-t.C:
return true
case <-e.terminate:
return false
}
}
func (e *ExternalCmd) runInnerInner() bool {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
// on Windows the shell is not used and command is started directly

View File

@ -123,20 +123,25 @@ outer:
func (s *Source) runInner() {
defer close(s.innerDone)
outer:
for {
ok := s.runInnerInner()
ok := func() bool {
ok := s.runInnerInner()
if !ok {
return false
}
t := time.NewTimer(retryInterval)
defer t.Stop()
select {
case <-t.C:
return true
case <-s.innerTerminate:
return false
}
}()
if !ok {
break outer
}
t := time.NewTimer(retryInterval)
defer t.Stop()
select {
case <-s.innerTerminate:
break outer
case <-t.C:
break
}
}
}
@ -209,6 +214,7 @@ func (s *Source) runInnerInner() bool {
}()
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
select {
case <-confDone:

View File

@ -127,20 +127,25 @@ outer:
func (s *Source) runInner() {
defer close(s.innerDone)
outer:
for {
ok := s.runInnerInner()
ok := func() bool {
ok := s.runInnerInner()
if !ok {
return false
}
t := time.NewTimer(retryInterval)
defer t.Stop()
select {
case <-t.C:
return true
case <-s.innerTerminate:
return false
}
}()
if !ok {
break outer
}
t := time.NewTimer(retryInterval)
defer t.Stop()
select {
case <-s.innerTerminate:
break outer
case <-t.C:
break
}
}
}