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,14 +46,8 @@ func (e *ExternalCmd) run() {
defer close(e.done)
for {
if !e.runInner() {
break
}
}
}
func (e *ExternalCmd) runInner() bool {
ok := e.runInnerInner()
ok := func() bool {
ok := e.runInner()
if !ok {
return false
}
@ -72,9 +66,14 @@ func (e *ExternalCmd) runInner() bool {
case <-e.terminate:
return false
}
}()
if !ok {
break
}
}
}
func (e *ExternalCmd) runInnerInner() bool {
func (e *ExternalCmd) runInner() 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 := func() bool {
ok := s.runInnerInner()
if !ok {
break outer
return false
}
t := time.NewTimer(retryInterval)
defer t.Stop()
select {
case <-s.innerTerminate:
break outer
case <-t.C:
return true
case <-s.innerTerminate:
return false
}
}()
if !ok {
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 := func() bool {
ok := s.runInnerInner()
if !ok {
break outer
return false
}
t := time.NewTimer(retryInterval)
defer t.Stop()
select {
case <-s.innerTerminate:
break outer
case <-t.C:
return true
case <-s.innerTerminate:
return false
}
}()
if !ok {
break
}
}
}