Skip to content

Commit 0627444

Browse files
authored
Remove UB when taking internal logs and move them to the final zapcore.Core (#14009)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 1169338 commit 0627444

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pkg/otelcol
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Remove UB when taking internal logs and move them to the final zapcore.Core
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14009]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: This can happen because of a race on accessing `logsTaken`.
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [user]

otelcol/buffered_core.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var _ zapcore.Core = (*bufferedCore)(nil)
2525

2626
type bufferedCore struct {
2727
zapcore.LevelEnabler
28-
mu sync.RWMutex
28+
mu sync.Mutex
2929
logs []loggedEntry
3030
context []zapcore.Field
3131
logsTaken bool
@@ -69,13 +69,13 @@ func (bc *bufferedCore) Sync() error {
6969
}
7070

7171
func (bc *bufferedCore) TakeLogs() []loggedEntry {
72-
if !bc.logsTaken {
73-
bc.mu.Lock()
74-
defer bc.mu.Unlock()
75-
logs := bc.logs
76-
bc.logs = nil
77-
bc.logsTaken = true
78-
return logs
72+
bc.mu.Lock()
73+
defer bc.mu.Unlock()
74+
if bc.logsTaken {
75+
return nil
7976
}
80-
return nil
77+
logs := bc.logs
78+
bc.logs = nil
79+
bc.logsTaken = true
80+
return logs
8181
}

0 commit comments

Comments
 (0)