Repository navigation
Conversation
WithGroup appended to the cloned handler's groups slice without clipping it, so handlers derived from the same parent could share a backing array. Once a handler was three groups deep, sibling handlers overwrote each other's last group, so ReplaceAttr got the wrong groups, and concurrent Handle calls with group attributes wrote the same slot. Clip the slice after appending, as slog's own handlers do, so that every later append makes a new array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #113
WithGroupappended to the cloned handler'sgroupsslice without clipping it, so a derived handler could share its backing array with its parent. From three groups deep the slice has spare capacity, and then:parent.WithGroup("x"),parent.WithGroup("y")) overwrite each other's last group, soReplaceAttrgets[a b c y]for both;appendAttrappends a group attribute's key into the same spare slot duringHandle, so concurrentInfo(..., slog.Group(...))calls on that logger race.The change clips the slice after appending in
WithGroup.h.groupsthen never has spare capacity, so every laterappend(in a derived handler or inappendAttr) makes a new array. This is the same thingslog'scommonHandler.clonedoes withslices.Clip(h.groups). The key prefix was never affected, because it is built from the separategroupPrefixstring.How I found it
I was reading
handler.gonext tolog/slog/handler.goand noticed thatclonecopiesgroupsas is while the stdlib clips it. A recordingReplaceAttron two sibling handlers showed the wrong groups, and a small concurrent test under-racereported the write at thegroups = append(groups, attr.Key)line.Where this can show up in practice
A service that builds per-component loggers from a shared parent with three or more groups and uses
ReplaceAttrto redact or drop attributes by group would apply the rule to the wrong component. Services that logslog.Groupattributes from many goroutines through such a logger get a race report fromgo test -race. This is my estimate; I have not seen a field report.Testing
TestReplaceAttrGroupsOfSiblingHandlers: records thegroupspassed toReplaceAttrfor two sibling handlers and for a group attribute on the parent. It fails before the change ([a b c y]forx).TestConcurrentGroupAttrs: four goroutines log group attributes through a three-group logger. Under-raceit reports a data race before the change.TZ="" go test ./... -race -tags=faketimepasses on macOS with Go 1.21.13 and Go 1.27.1;go vetand staticcheck are clean. Not run: Linux and Windows.