forked from natefinch/lumberjack
-
Notifications
You must be signed in to change notification settings - Fork 0
Fixed goroutine leak, made tests deterministic #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,8 +111,15 @@ type Logger struct { | |
| file *os.File | ||
| mu sync.Mutex | ||
|
|
||
| millCh chan bool | ||
| startMill sync.Once | ||
| // millCh is signalled to tell mill to compress/remove old log files. | ||
| millCh chan struct{} | ||
| // millShutdownCh is signalled when mill has shutdown completely. | ||
| millShutdownCh chan struct{} | ||
|
|
||
| // notifyRemoved is signalled when one or more files are removed. Used only for testing. | ||
| notifyRemoved chan struct{} | ||
| // notifyCompressed is signalled when a file is compressed. Used only for testing. | ||
| notifyCompressed chan struct{} | ||
| } | ||
|
|
||
| var ( | ||
|
|
@@ -165,6 +172,14 @@ func (l *Logger) Write(p []byte) (n int, err error) { | |
| func (l *Logger) Close() error { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
|
|
||
| if l.millCh != nil { | ||
| close(l.millCh) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the actual fix for the leak, right? (just making sure I grok it)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
| <-l.millShutdownCh | ||
| l.millCh = nil | ||
| l.millShutdownCh = nil | ||
| } | ||
|
|
||
| return l.close() | ||
| } | ||
|
|
||
|
|
@@ -356,18 +371,29 @@ func (l *Logger) millRunOnce() error { | |
| } | ||
| } | ||
|
|
||
| filesRemoved := false | ||
| for _, f := range remove { | ||
| errRemove := os.Remove(filepath.Join(l.dir(), f.Name())) | ||
| if err == nil && errRemove != nil { | ||
| err = errRemove | ||
| } | ||
| filesRemoved = true | ||
| } | ||
| if filesRemoved && l.notifyRemoved != nil { | ||
| l.notifyRemoved <- struct{}{} | ||
| } | ||
|
|
||
| filesCompressed := false | ||
| for _, f := range compress { | ||
| fn := filepath.Join(l.dir(), f.Name()) | ||
| errCompress := compressLogFile(fn, fn+compressSuffix) | ||
| if err == nil && errCompress != nil { | ||
| err = errCompress | ||
| } | ||
| filesCompressed = true | ||
| } | ||
| if filesCompressed && l.notifyCompressed != nil { | ||
| l.notifyCompressed <- struct{}{} | ||
| } | ||
|
|
||
| return err | ||
|
|
@@ -376,21 +402,24 @@ func (l *Logger) millRunOnce() error { | |
| // millRun runs in a goroutine to manage post-rotation compression and removal | ||
| // of old log files. | ||
| func (l *Logger) millRun() { | ||
| for _ = range l.millCh { | ||
| for range l.millCh { | ||
| // what am I going to do, log this? | ||
| _ = l.millRunOnce() | ||
| } | ||
| l.millShutdownCh <- struct{}{} | ||
| } | ||
|
|
||
| // mill performs post-rotation compression and removal of stale log files, | ||
| // starting the mill goroutine if necessary. | ||
| func (l *Logger) mill() { | ||
| l.startMill.Do(func() { | ||
| l.millCh = make(chan bool, 1) | ||
| // We're inside the mutex, so it's okay to access millCh. | ||
| if l.millCh == nil { | ||
| l.millCh = make(chan struct{}, 1) | ||
| l.millShutdownCh = make(chan struct{}, 1) | ||
| go l.millRun() | ||
| }) | ||
| } | ||
| select { | ||
| case l.millCh <- true: | ||
| case l.millCh <- struct{}{}: | ||
| default: | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the easiest way to test the linux version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a build tag for only running on linux (if necessary), then run on a linux machine (e.g. Jenkins)