Potential issue with Counter internal slice management
bernata opened this issue · 0 comments
bernata commented
What did you do?
I created a PR to demonstrate the problem:
#1295
To summarize, there are 4 counters:
c1 := (&Counter{}).With("a", "1", "b", "2", "c", "3", "d", "4")
c2 := c1.With("e", "5").(*Counter)
c3 := c2.With("f", "6", "g", "7").(*Counter)
c4 := c2.With("h", "8").(*Counter)
The counters c3
and c4
share the same backing memory for the counter lvs slice. As a result, c4
will overwrite the contents of c3
lvs slice. So in the example above, at the end of the execution, c3 lvs internal slice has values: a 1 b 2 c 3 d 4 e 5 h 8 g 7
What did you expect?
The expectation I have is c3 lvs internal slice has values: a 1 b 2 c 3 d 4 e 5 f 6 g 7
I assumed it behaved a bit like context.WithValue which I think would have similar behavior in keeping the parent-child contexts free from overwrite.
What happened instead?
The c3 lvs internal slice has values: a 1 b 2 c 3 d 4 e 5 h 8 g 7
because c4
overwrote part of the c3
slice.