Skip to content

Commit

Permalink
Merge pull request #11261 from hashicorp/b-logmon-leak
Browse files Browse the repository at this point in the history
Fix a logmon goroutine and memory leak
  • Loading branch information
Mahmood Ali authored Oct 5, 2021
2 parents 88ff7f4 + 40c7720 commit 1cb2049
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 193 deletions.
3 changes: 3 additions & 0 deletions .changelog/11261.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: Fixed a memory leak in log collector when tasks restart
```
16 changes: 11 additions & 5 deletions client/logmon/logging/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewFileRotator(path string, baseFile string, maxFiles int,
flushTicker: time.NewTicker(bufferFlushDuration),
logger: logger,
purgeCh: make(chan struct{}, 1),
doneCh: make(chan struct{}, 1),
doneCh: make(chan struct{}),
}

if err := rotator.lastFile(); err != nil {
Expand Down Expand Up @@ -237,8 +237,14 @@ func (f *FileRotator) createFile() error {
// flushPeriodically flushes the buffered writer every 100ms to the underlying
// file
func (f *FileRotator) flushPeriodically() {
for range f.flushTicker.C {
f.flushBuffer()
for {
select {
case <-f.flushTicker.C:
f.flushBuffer()
case <-f.doneCh:
return
}

}
}

Expand All @@ -251,9 +257,9 @@ func (f *FileRotator) Close() error {
f.flushTicker.Stop()
f.flushBuffer()

// Stop the purge go routine
// Stop the go routines
if !f.closed {
f.doneCh <- struct{}{}
close(f.doneCh)
close(f.purgeCh)
f.closed = true
f.currentFile.Close()
Expand Down
Loading

0 comments on commit 1cb2049

Please sign in to comment.