diff --git a/.chloggen/fix-buffered-code-ub.yaml b/.chloggen/fix-buffered-code-ub.yaml new file mode 100644 index 00000000000..74ad8fa27ee --- /dev/null +++ b/.chloggen/fix-buffered-code-ub.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: pkg/otelcol + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Remove UB when taking internal logs and move them to the final zapcore.Core + +# One or more tracking issues or pull requests related to the change +issues: [14009] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: This can happen because of a race on accessing `logsTaken`. + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/otelcol/buffered_core.go b/otelcol/buffered_core.go index dd334a8454e..cba62fc390b 100644 --- a/otelcol/buffered_core.go +++ b/otelcol/buffered_core.go @@ -25,7 +25,7 @@ var _ zapcore.Core = (*bufferedCore)(nil) type bufferedCore struct { zapcore.LevelEnabler - mu sync.RWMutex + mu sync.Mutex logs []loggedEntry context []zapcore.Field logsTaken bool @@ -69,13 +69,13 @@ func (bc *bufferedCore) Sync() error { } func (bc *bufferedCore) TakeLogs() []loggedEntry { - if !bc.logsTaken { - bc.mu.Lock() - defer bc.mu.Unlock() - logs := bc.logs - bc.logs = nil - bc.logsTaken = true - return logs + bc.mu.Lock() + defer bc.mu.Unlock() + if bc.logsTaken { + return nil } - return nil + logs := bc.logs + bc.logs = nil + bc.logsTaken = true + return logs }