News for hackers. Stuff that matters.
Re:Data Race Patterns in Go by kosherhurricane on Friday June 10, 2022 @07:29PM (#31700481)
>>> The reference to the slice was resized in the middle of an append operation from another async routine.

> What exactly happens in these cases?

Go's append looks like this:

mySlice = append(mySlice, newItem)

To me, this makes it very clear that 1) mySlice pointer can now point to someplace entirely different in memory, and 2) there maybe new allocation.

I write both Java and Go. For personal projects, I always choose go.

Re:Data Race Patterns in Go by Groxx on Friday June 10, 2022 @07:52PM (#31700623)
The append pattern also implies the opposite of reality, in that it also (usually!) mutates mySlice. Which is the source of one of the two(?) possible races in that piece of code.
Re:Data Race Patterns in Go by sidlls on Friday June 10, 2022 @08:00PM (#31700666)
I'm no fan of go, although I think it's better than many other languages for services, but the argument here is against the label "append", not the operation. It's a poor name for the operation, but the documentation is quite clear about what's going on. I'd argue that understanding the keywords and builtins of a language is the bare minimum an engineer should do before he starts writing anything in it.
Re:Data Race Patterns in Go by erik_seaberg on Friday June 10, 2022 @11:03PM (#31701573)

  s2 := append(s1, x)
  s3 := append(s1, y)
shouldn’t be allowed, because what it’s likely to do is not what anyone meant. In a pass-by-value language, passing a slice or map by value should copy it, append should be a method that returns void, and passing a pointer should be the way to share state and avoid copies.
Re:Data Race Patterns in Go by sethammons on Saturday June 11, 2022 @08:31AM (#31703988)
What would you expect that code to do?

I'd expect to have two different slices, s2 and s3, to contain all the same elements aside from the last.

[a, b, c, x] and [a, b, c, y] and s1 remains [a, b, c]

Re:Data Race Patterns in Go by Groxx on Saturday June 11, 2022 @02:03PM (#31706593)
When printing s1, s2, then s3:

It does exactly that, yes: https://go.dev/play/p/rs2FeK_QUjs

    [a b c]
    [a b c x]
    [a b c y]
But maybe it doesn't: https://go.dev/play/p/Na-eL0sOV9e

    [a b c]
    [a b c y]  <- this is now "y"
    [a b c y]
So... maybe they share the same backing array? Lets try setting s2[0] to "z" after appending with the original code: https://go.dev/play/p/mAB-gUb0shB

    [a b c]
    [z b c x]
    [a b c y]
Apparently not. But also apparently yes? https://go.dev/play/p/k1ciGzyS2gc

    [z b c]    <- this changed too
    [z b c y]
    [z b c y]
Let's try appending just one more item before redoing ^ that example, where they all shared the same data: https://go.dev/play/p/5JneXHMeUjx

    [a b c]
    [z b c x x2]
    [a b c y y2]
Notice that in all of these examples, I haven't explicitly declared a length or capacity. There's nothing "funny looking" or clearly intentionally allowing these different behaviors, it's just simple, very-common slice use.

.... so yeah. This is a source of a number of hard-to-track-down bugs.

Re:Data Race Patterns in Go by Jcowell on Sunday June 12, 2022 @04:22PM (#31717929)
Is there a bug or issue on this in their GitHub cause wtf is this ?
Re:Data Race Patterns in Go by Groxx on Sunday June 12, 2022 @04:31PM (#31717987)
This is expected behavior. It's just an easy thing to screw up by accident.
Re:Data Race Patterns in Go by sethammons on Saturday June 11, 2022 @03:24PM (#31707417)
Amazing, very interesting
Re:Data Race Patterns in Go by meowface on Friday June 10, 2022 @08:09PM (#31700713)
Yes, this majorly tripped me up when working on my first big Go project. Spent days hunting for a non-deterministic data corruption issue which was caused by this. It's definitely my fault for not fully reading the documentation and not realizing that append may (and often does) mutate the slice, but I was indeed misled by the `x = append(x, ...)` syntax into assuming it only works off of a copy without modifying the original.
Re:Data Race Patterns in Go by yencabulator on Saturday June 11, 2022 @03:22PM (#31707398)
Go's append is pretty much C's realloc, and behaves very much the same; the pointer you get back may or may not be the passed-in pointer.

Also,

> If the capacity of s is not large enough to fit the additional values, append allocates a new, sufficiently large underlying array that fits both the existing slice elements and the additional values. Otherwise, append re-uses the underlying array.

https://go.dev/ref/spec#Appending_and_copying_slices