$ the-wire · showcase
Go's log/slog MultiHandler stops sharing attrs slices
By RepoJournal · Filed · About Go · Composed from the cited sources · methodology
Go's standard library log/slog package changed MultiHandler.WithAttrs to clone the attrs slice for every contained Handler except the last, closing a path where one Handler's mutation could corrupt what later Handlers see.
Handler.WithAttrs transfers ownership of its attrs slice to the receiver. MultiHandler was passing that same slice to every contained Handler, so a Handler that modified the slice could affect subsequent Handlers downstream [1].
With this commit, MultiHandler clones the attrs slice for every Handler except the last. The last Handler receives the original slice, which avoids an unnecessary allocation while still giving every Handler independent backing storage [1].
A regression test was added alongside the change [1]. If you wrap Handlers with a MultiHandler that dispatches to more than one sink, and any of those sinks alter the attrs it receives, your behavior changes: previously those mutations leaked across Handlers, and now they do not.
Action items
- → Audit MultiHandler setups where a contained Handler mutates attrs; behavior now differs golang/go [plan]
- → Rebuild against the updated log/slog and run the new regression test golang/go [monitor]
References