Skip to content
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

fixed goroutine leak #57

Open
wants to merge 2 commits into
base: v2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Logger struct {
mu sync.Mutex

millCh chan bool
wg sync.WaitGroup
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a pointer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? it would not be passed by method, just one property of this struct.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@natefinch could you reply this?

Copy link

@gz-c gz-c Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaczhao
https://golang.org/pkg/sync/#WaitGroup

A WaitGroup must not be copied after first use.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so where to init the : wg sync.WaitGroup

temporary I put it in file : lumberjack.go

// mill performs post-rotation compression and removal of stale log files,
// starting the mill goroutine if necessary.
func (l *Logger) mill() {
	if l.wg == nil {
		l.wg = new(sync.WaitGroup)
	}

	l.startMill.Do(func() {
		l.wg.Add(1)
		l.millCh = make(chan bool, 1)
		go l.millRun()
	})
	select {
	case l.millCh <- true:
	default:
	}
}

startMill sync.Once
}

Expand Down Expand Up @@ -165,7 +166,15 @@ func (l *Logger) Write(p []byte) (n int, err error) {
func (l *Logger) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
return l.close()

err := l.close()

if l.millCh != nil {
close(l.millCh)
l.wg.Wait()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after waiting, you need to set millCh to nil, so if someone calls close again, it won't panic, trying to close the same channel.

Copy link
Author

@jaczhao jaczhao Nov 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you are righ

}
return err

}

// close closes the file if it is open.
Expand Down Expand Up @@ -376,6 +385,7 @@ 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() {
defer l.wg.Done()
for _ = range l.millCh {
// what am I going to do, log this?
_ = l.millRunOnce()
Expand All @@ -386,6 +396,7 @@ func (l *Logger) millRun() {
// starting the mill goroutine if necessary.
func (l *Logger) mill() {
l.startMill.Do(func() {
l.wg.Add(1)
l.millCh = make(chan bool, 1)
go l.millRun()
})
Expand Down
4 changes: 2 additions & 2 deletions lumberjack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func TestCompressOnRotate(t *testing.T) {

// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)
<-time.After(20 * time.Millisecond)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you need to up these to make the tests pass? Just curious

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I would got random failed if only 5 seconds


// a compressed version of the log file should now exist and the original
// should have been removed.
Expand Down Expand Up @@ -672,7 +672,7 @@ func TestCompressOnResume(t *testing.T) {

// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)
<-time.After(20 * time.Millisecond)

// The write should have started the compression - a compressed version of
// the log file should now exist and the original should have been removed.
Expand Down