cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

pkg/util/log: make use of OrigStderr thread safe

Open abarganier opened this issue 1 year ago • 1 comments

Fixes: https://github.com/cockroachdb/cockroach/issues/122791

The log package exports a variable called OrigStderr which is meant to represent the original stderr os.File for the process at startup.

Various operations in the log package may "hijack" the stderr, for example, to redirect the process's stderr logs to a file instead. The package maintains a reference to the original stderr in order to maintain the ability to still make use of it under certain circumstances while still redirecting stderr to a file.

A race condition was appearing in tests that was reassigning this variable while it was in use by the process.

This patch fixes the race condition by wrapping the os.File in a mutex, and then implementing the io.Writer interface on the wrapper type. Swapping out the underlying file can now be an atomic operation, and we also expose an API to perform atomic operations on the underlying file. A RWMutex is used to reduce contention.

Release note: none

Epic: none

abarganier avatar May 15 '24 15:05 abarganier

This change is Reviewable

cockroach-teamcity avatar May 15 '24 15:05 cockroach-teamcity

bors r=xinhaoz

dhartunian avatar May 22 '24 03:05 dhartunian

blathers backport 24.1.0-rc

dhartunian avatar May 22 '24 17:05 dhartunian