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

fix(logger): make MockLogger lock the buffer implicitly #14428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thp-canonical
Copy link

This has been ported from a similar change in Pebble:

The explicit logging feature was implemented in snapd in commit 16c3680b78d, but this requires a developer to know when another goroutine might be using the logger, and using WithLoggerLock() explicitly. In addition, as WithLoggerLock() acquires the global logger lock, trying to log from the callback of WithLoggerLock() might result in a deadlock (but the testing code right now isn't doing this, of course).

Instead of requiring explicit locking, this change makes it so that Write(), String() and Reset() access to the bytes.Buffer is mutex-protected.

As logger.MockLogger() is only used in unit test code, locking an otherwise uncontested mutex for Write() should hopefully not cause any noticeable slowdown, while making it impossible to accidentally use the mock logger in a thread-unsafe way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant