Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logger): Make MockLogger threadsafe (#441)
When using a `logger.MockLogger()`, a background goroutine calling `logger.Noticef()` might race the underlying `bytes.Buffer` for read/write access. To avoid that, limit the returned buffer type for `MockLogger()` to just `fmt.Stringer` (and mutex-protect its reading from the buffer) as well as mutex-protect `Write()` calls to the underlying buffer. To reproduce the race condition, run `go test -race ./internals/logger` with `TestMockLoggerReadWriteThreadsafe` added, but with the unmodified `MockLogger` implementation. If more functions from `bytes.Buffer` are needed from the `MockLogger` return value, the interface can be expanded with additional pass-through lock-protected calls, but in all of Pebble, it seems only the `.String()` function is used, so only that is exposed for now. While `logger.Noticef()` calls are serialized by `logger.loggerLock`, the same cannot be said for parallel access to the underlying `bytes.Buffer` that is returned from `MockLogger` when calling `logger.Noticef()` in one goroutine, and accessing the `bytes.Buffer` from another goroutine.
- Loading branch information