Skip to content

Use bufio.Reader instead of bufio.Scanner for logger.Copier - #13564

Merged
LK4D4 merged 1 commit into
moby:masterfrom
burke:fix-memory-leak
May 29, 2015
Merged

LK4D4 merged 1 commit into
moby:masterfrom
burke:fix-memory-leak

Conversation

@burke

@burke burke commented May 28, 2015

Copy link
Copy Markdown
Contributor

When using a scanner, log lines over 64K will crash the Copier with bufio.ErrTooLong. Subsequently, the ioutils.bufReader will grow without bound as the logs are no longer being flushed to disk.

For us, this manifested as some background workers occasionally stopping log writes, and slow memory growth between deploys.

@jessfraz

Copy link
Copy Markdown
Contributor

you are awesome :)

Comment thread daemon/logger/copier.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about ReadBytes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, good point, I forgot that one existed. Will update.

@burke
burke force-pushed the fix-memory-leak branch 3 times, most recently from 1ace45e to 4c515fe Compare May 28, 2015 22:15
Comment thread daemon/logger/copier.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can remove the double return by doing something like:

if err != io.EOF {
    logrus.Errorf("Error scanning log stream: %s", err)
}
return

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have this sense that comparing nil with io.EOF at runtime causes a panic but I can't remember why... I'll give it a shot.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, yeah, nevermind, I see what you're saying.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but you compared that err != nil in the line before.

@burke
burke force-pushed the fix-memory-leak branch from 4c515fe to c0b4fcb Compare May 28, 2015 22:34
@calavera

Copy link
Copy Markdown
Contributor

LGTM

@calavera calavera added this to the 1.7.0 milestone May 28, 2015
@calavera

Copy link
Copy Markdown
Contributor

I think this can go into 1.7, but feel free to remove it from the milestone.

@calavera calavera added the kind/bug Bugs are bugs. The cause may or may not be known at triage time so debugging may be needed. label May 28, 2015
@cpuguy83

Copy link
Copy Markdown
Member

ping @unclejack

@cpuguy83

Copy link
Copy Markdown
Member

This closes #13333

@burke
burke force-pushed the fix-memory-leak branch from c0b4fcb to e8b9a61 Compare May 29, 2015 01:20
Comment thread daemon/logger/copier.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I kinda liked your lightweight solution with slicing. I think might be:

line, err := reader.ReadBytes('\n')
line = line[:len(line)-1]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So the problem I had, and the reason I changed the commit, is that ReadBytes can return an error and a partial output. I haven't tested this, but I'm assuming that if it returns a partial input, it will end with a valuable byte and not a newline -- so it's probably prudent to only trim the trailing character if it's a newline.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay, anyway then you should reuse one variable :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Works for me. Changed.

When using a scanner, log lines over 64K will crash the Copier with
bufio.ErrTooLong. Subsequently, the ioutils.bufReader will grow without
bound as the logs are no longer being flushed to disk.

Signed-off-by: Burke Libbey <[email protected]>
@burke
burke force-pushed the fix-memory-leak branch from e8b9a61 to f779cfc Compare May 29, 2015 16:29
@LK4D4

LK4D4 commented May 29, 2015

Copy link
Copy Markdown
Contributor

LGTM

LK4D4 added a commit that referenced this pull request May 29, 2015
Use bufio.Reader instead of bufio.Scanner for logger.Copier
@LK4D4
LK4D4 merged commit c42810f into moby:master May 29, 2015
@burke burke mentioned this pull request Jun 1, 2015
@jessfraz

Copy link
Copy Markdown
Contributor

cherry-picked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Bugs are bugs. The cause may or may not be known at triage time so debugging may be needed. status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants