Skip to content

Clip groups slice in WithGroup - #114

Open
0z5a wants to merge 1 commit into
lmittmann:mainfrom
0z5a:clip-group-slice
Open

0z5a wants to merge 1 commit into
lmittmann:mainfrom
0z5a:clip-group-slice

Conversation

@0z5a

@0z5a 0z5a commented Nov 18, 2026

Copy link
Copy Markdown

Fixes #113

WithGroup appended to the cloned handler's groups slice 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:

  • sibling handlers (parent.WithGroup("x"), parent.WithGroup("y")) overwrite each other's last group, so ReplaceAttr gets [a b c y] for both;
  • appendAttr appends a group attribute's key into the same spare slot during Handle, so concurrent Info(..., slog.Group(...)) calls on that logger race.

The change clips the slice after appending in WithGroup. h.groups then never has spare capacity, so every later append (in a derived handler or in appendAttr) makes a new array. This is the same thing slog's commonHandler.clone does with slices.Clip(h.groups). The key prefix was never affected, because it is built from the separate groupPrefix string.

How I found it

I was reading handler.go next to log/slog/handler.go and noticed that clone copies groups as is while the stdlib clips it. A recording ReplaceAttr on two sibling handlers showed the wrong groups, and a small concurrent test under -race reported the write at the groups = 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 ReplaceAttr to redact or drop attributes by group would apply the rule to the wrong component. Services that log slog.Group attributes from many goroutines through such a logger get a race report from go test -race. This is my estimate; I have not seen a field report.

Testing

  • TestReplaceAttrGroupsOfSiblingHandlers: records the groups passed to ReplaceAttr for two sibling handlers and for a group attribute on the parent. It fails before the change ([a b c y] for x).
  • TestConcurrentGroupAttrs: four goroutines log group attributes through a three-group logger. Under -race it reports a data race before the change.
  • TZ="" go test ./... -race -tags=faketime passes on macOS with Go 1.21.13 and Go 1.27.1; go vet and staticcheck are clean. Not run: Linux and Windows.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WithGroup: sibling handlers share the groups slice, so ReplaceAttr gets the wrong groups

1 participant