News for hackers. Stuff that matters.
Re:Updating the Go Memory Model by judofyr on Monday July 12, 2021 @11:18AM (#27811130)
> In other words, Go encourages avoiding subtle bugs by avoiding subtle code.

I find this philosophy strange when they talk about the communication primitives. Channels are extremely subtle in my experience. A channel can be buffered or unbuffered, it can be closed, and there’s different behavior of read/write whether it’s in a select or not. And then you often end up with multiple channels as well. Reviewing code with channels is often more confusing than code that uses mutexes in my opinion.

Re:Updating the Go Memory Model by laumars on Monday July 12, 2021 @12:09PM (#27811738)
It took me a loooong time to fully wrap my head around channels. They’re definitely not explicit and full of subtle yet devastating bugs when used inappropriately (which is all to easy to do too). Some of the boilerplate code around using them is just ugly too.

That said, there are occasions when channels have proven invaluable. I think the real issue is they were branded as a killer feature but in reality their usefulness is a little more niche than a mutex.

Re:Updating the Go Memory Model by elithrar on Monday July 12, 2021 @12:16PM (#27811815)
> I think the real issue is they were branded as a killer feature but in reality their usefulness is a little more niche than a mutex.

Agree - and many people are surprised to learn both: a) how fast mutex ops are in practice, and b) how channels use mutexes under the covers.

Re:Updating the Go Memory Model by tapirl on Monday July 12, 2021 @07:00PM (#27816095)
Mutexes and channels really overlap in some functionalities, but not totally overlap, okay?
Re:Updating the Go Memory Model by HeyImAlex on Monday July 12, 2021 @12:33PM (#27812043)
They could have been a much more powerful primitive, but not having generics means tons of boilerplate; all basic channel patterns involve a lot of copy pasting.
Re:Updating the Go Memory Model by jasonwatkinspdx on Monday July 12, 2021 @03:59PM (#27814379)
Once generics have been out there for a while and the patterns of use in the community are clear/stable, I think there's a decent chance they'll revisit channels to help clean that up. It certainly is frustrating.
Re:Updating the Go Memory Model by fpoling on Monday July 12, 2021 @06:11PM (#27815715)
Channels in Go lack priority support which made them unusable to express some patterns.

Fortunately one can code a replacement for channels in Go using mutexes and semaphores. And that exercise in turn allows one to see that in many cases the channels are just a bad model. Things like having a single polymorphic priority message queue per go routine suits many cases better than dealing with multiple channels.

And this has been known since nineties if not eighties when Ada had to add mutexes after early designs based purely on channels. So it is puzzling why Go made channels such a central feature when the priority queue just works judging by Erlang or many successful C/C++ libraries.

Re:Updating the Go Memory Model by chakkepolja on Monday July 12, 2021 @12:24PM (#27811916)
Correct me if I am wrong, isn't a channel basically same as blocking queues in Java? I know go but not much advanced.
Re:Updating the Go Memory Model by eitland on Monday July 12, 2021 @12:52PM (#27812297)
Seems like there are a few differences but nothing I personally would leave normal indexing, kind of working generics, Maven and a choice of 3 world class IDEs behind for: https://stackoverflow.com/questions/10690403/go-channel-vs-j...

To each their own though. I hear a lot of people prefer it.

Re:Updating the Go Memory Model by boyter on Monday July 12, 2021 @08:51PM (#27816704)
More or less, but with the ability to "poison" them by closing which causes downstream go-routines to exit. You can do the same thing in java by checking for a poison object and then terminate your loop.

The close is built into the language itself which is useful, along with the other built in language functionality. I wrote about it here, with an example of how to do it https://boyter.org/posts/file-read-challange/

Re:Updating the Go Memory Model by chakkepolja on Monday July 12, 2021 @10:24PM (#27817105)
Thanks!
Re:Updating the Go Memory Model by clipradiowallet on Monday July 12, 2021 @04:50PM (#27814956)
I don't code Java, but I have the same sentiment about channels in golang to queue's in python.
Re:Updating the Go Memory Model by slaymaker1907 on Monday July 12, 2021 @04:21PM (#27814624)
The killer feature over a mutex, in my opinion, is that mutexes encourage sharing by default which is terrible for both program correctness and performance.
Re:Updating the Go Memory Model by laumars on Monday July 12, 2021 @04:54PM (#27814997)
That depends massively on the kind of problems you’re trying to solve. I’ve written some software where channels were the right tool, and others where mutexes were the correct choice.
Re:Updating the Go Memory Model by catlifeonmars on Tuesday July 13, 2021 @10:38AM (#27821505)
This is a shame, because if done correctly, “share memory by communicating” eliminates a whole class of bugs.

I agree that the ergonomics of Go channels leave a lot to be desired. Also, channels are still a primitive: you still need to build abstractions to do anything nontrivially useful with them. I think something like a good coroutine abstraction, or thread nursery abstraction would be ideal.

Re:Updating the Go Memory Model by tapirl on Monday July 12, 2021 @06:58PM (#27816070)
You should not blame Go channels for your inexperience of using them.
Re:Updating the Go Memory Model by tapirl on Monday July 12, 2021 @11:35AM (#27811331)
For some scenarios, the implementation code will be quite complicated if only mutexes are used, whereas the implementation will be quite simple by using channels.

I think your frustration comes from you wanted to use channels for any scenarios. This is not recommended too. https://github.com/golang/go/wiki/MutexOrChannel

Re:Updating the Go Memory Model by karmakaze on Monday July 12, 2021 @12:21PM (#27811875)
It fails to mention the performance penalty with channels.
Re:Updating the Go Memory Model by tapirl on Monday July 12, 2021 @12:30PM (#27811998)
For some scenarios channels and mutexes are both suitable for, mutextes are a bit more performant than channels. If you do care about the performance penalty, just choose mutexes. But the for scenarios mutextes are incapable of, still using mutexes is surely not a good idea.
Re:Updating the Go Memory Model by karmakaze on Monday July 12, 2021 @12:34PM (#27812061)
This would be a good thing to add to the reference.
Re:Updating the Go Memory Model by jerf on Monday July 12, 2021 @12:51PM (#27812291)
Dunno. Tell people "channels are about 3 times slower than mutexes" and watch them scramble the hell out of their program trying to avoid channels, when the reality is "in general, both of them are fast enough that they will not be the bottleneck on your program".

In general (not just Go!), don't send tiny amounts of work across any sort of internal boundary (goroutine, thread, spark, green thread, whatever they call it). The amount of work you send should significantly exceed the costs of sending it. Follow this advice and the perf differences between mutexes and channels will almost certainly not be relevant; fail to follow it and neither of them will be fast enough. I don't think there's actually a lot of programs in the wild where the performance difference between the two would be the make-or-break difference. Non-zero, but not many. In practice it's pretty clear developers will spend much more time worrying over it than is justified.

(A common benchmark I see new programmers apply to any language that claims to be good at multithreading is to try to "parallelize" the act of adding a few million integers together by sending individual integers or addition problems out to threads, and wondering why it's 10-100 times slower even though all my CPUs are at 100%, so why does multithreading suck so hard in this language? The problem is that no matter how cheap the send operation is, it's going to be dozens or hundreds of operations, whereas a single integer addition is generally a single cycle, or possibly, amortized to less than that depending on how good your compiler is. You just need to be sure to send units of work larger than the cost of sending them, which is generally not that hard. This isn't just Go, I've seen this charge leveled at Haskell, Erlang, and Python's multiprocessing too, and I'm sure others have seen it in their own communities. It's a very common mistake.)

Re:Updating the Go Memory Model by stcredzero on Monday July 12, 2021 @04:29PM (#27814696)
The amount of work you send should significantly exceed the costs of sending it.

Jerf, you did it again! This is gold! Succinct and to the point.

I'm going to think on this one.

Re:Updating the Go Memory Model by karmakaze on Monday July 12, 2021 @06:16PM (#27815764)
This implies that you know the "costs of sending it."
Re:Updating the Go Memory Model by jerf on Monday July 12, 2021 @09:29PM (#27816868)
For channels and other things in the local OS process, you should have a good idea. Give or take being in major swap problems, etc., but I think of that as an "already lost" situation; at that point the problem isn't the expense of moving things between threads or local processes anyhow.

When you get out into discussing network matters, it gets a lot fuzzier. You may "know" that you're only .025ms away from something in some specific system, but your TCP-based API doesn't necessarily "know" that and will happily use the exact same code to do something that takes 4 orders of magnitude larger. That's enough difference to be problematic.

Re:Updating the Go Memory Model by karmakaze on Tuesday July 13, 2021 @06:48PM (#27827192)
'Send'ing things via a Go channel is all in the same process, between goroutines (sometimes between threads).
Re:Updating the Go Memory Model by arghwhat on Monday July 12, 2021 @12:43PM (#27812176)
Because there is no generic penalty, and worrying ahead of having a problem to solve sounds like a terminal case of premature optimization.
Re:Updating the Go Memory Model by brobinson on Monday July 12, 2021 @01:09PM (#27812533)
Not sure if it's the case nowadays, but Go channels were historically around 4x slower than the equivalent mutex-based code. This is a Big Deal™ if you're writing performance sensitive code or in a hot loop.

https://www.jtolio.com/2016/03/go-channels-are-bad-and-you-s...

https://www.datadoghq.com/blog/go-performance-tales/

Re:Updating the Go Memory Model by arghwhat on Wednesday July 14, 2021 @02:47AM (#27830362)
Comparing a FIFO internally wrapped in mutexes and conds, designed to pass messages, to a single mutex makes very little sense. Of course a channel is slower than one of the bare primitives used to construct it!

The proper question to ask is whether your application (or your programmer) lends itself with the CSP paradigm or not, and whether this allows you to write simpler, more robust code.

Extremely performance sensitive code will always need to be written In lower level paradigms, using e.g. atomics directly - this doesn't matter for most code though. And to be frank, extremely performance sensitive code is past the capability of Go for quite a few reasons.

Re:Updating the Go Memory Model by karmakaze on Monday July 12, 2021 @02:20PM (#27813353)
I've had the opposite happen, where because of mindsets like this, that it doesn't get properly discussed and going in blind having to deal with it later in a case where it was clear that it would matter.
Re:Updating the Go Memory Model by arghwhat on Wednesday July 14, 2021 @02:51AM (#27830394)
The mindset is still correct - it depends too heavily on your usecase to make a generic statement.

For anything performance sensitive, the only thing that matters is to profile your code.

Re:Updating the Go Memory Model by throwaway894345 on Monday July 12, 2021 @11:20AM (#27811158)
I agree with this. Channels have not lived up to their promises, and I mostly avoid them in Go. They're handy sometimes when you want to just block some goroutine (read from a channel that is never written to) or for a few other use cases, but mutexes are my go-to primitive.
Re:Updating the Go Memory Model by naikrovek on Monday July 12, 2021 @12:29PM (#27811994)
it always shocks me when someone says that channels are not immediately intuitive. they make so much sense to me, and they did from the instant I read about them.

combined with goroutines, channels are absolutely one of the best features of this language, to me.

async & await, on the other hand, still confuse me and trip me up today, even after using them for years, because of very weird implementation minutia and edge cases that I always seem to stumble into.

I wish async and await would disappear from the face of the earth and I wish coroutines and channels were in every language I use.

Re:Updating the Go Memory Model by throwaway894345 on Monday July 12, 2021 @12:59PM (#27812397)
I've been using Go avidly since 2012. I still run into deadlocks and panics (writing to a closed channel) when I try to use channels beyond the simplest use cases. Error handling in a parallel context is also hairy with channels. I've learned to minimize my use of channels and prefer mutexes as my default concurrency primitive.

It really only means that CSP looks nice on paper but (at least as implemented in Go) doesn't work out well in practice. There's lots to like about the language even if the CSP theory didn't pan out.

Re:Updating the Go Memory Model by itake on Monday July 12, 2021 @01:23PM (#27812720)
My understanding is that if you follow the rule of using generator functions (creator of the channel is responsible for writing and the closing) it’s impossible to write to a closed channel.

When would that pattern not be useful?

Re:Updating the Go Memory Model by gilgad13 on Monday July 12, 2021 @05:12PM (#27815206)
The rule that the writer should be responsible for closing a channel is a good one to keep in mind, but it is often the case that you want to launch an indeterminate number of generators and collect the results from all of them. For example, you may want to make one connection to each server in the config, or to process each file in a directory in parallel. Because the arms of a `select{}` are determined at compile time, it cannot be used to select over the variable generator-owned channels, and you have four more difficult options:

* Use `reflect.Value.Select`[1]. Having to reach for reflect feels ugly for such a common case, and the performance of the reflect-select is much lower than the native select.

* Create a single channel owned by the reader, pass it to each writer, and arrange for this channel to be closed when the final writer exits, through a waitgroup. There is an example under "Parallel digestion" in the Go Concurrency Patterns blog post[2]. Note the little details to get right. We must launch a separate goroutine to monitor the waitgroup / channel closure. If we accidentally do it in-line at the wrong level, everything will work fine if the total number of items written to `c` is less than `c`'s capacity, but will hang once a worker becomes blocked on `c`. Additionally, the waitgroup is threaded directly into the writers, which may be more difficult if those are implemented in some other generic package.

* Wrap the above pattern up into a `merge` function, such as the one under "Fan-in, Fan-out" in the Go Concurrency Patterns post[2]. The lack of generics means we will have to copy-paste this function everywhere we want to use it. Additionally, this launches a goroutine for every channel being watched, which strikes people as "expensive" for such a simple operation.

* We can construct a function that takes two channels and launches a goroutine that selects between the two and writes to a merged output channel. By constructing a tree of these we can merge an arbitrary number of channels. This is really just an optimization of the above.

None of these options are particularly intuitive. Too often I've instead seen developers create a single channel owned by the reader and either:

* Assume it is never closed and the reader doesn't terminate until the application does

* Rely on some external mechanism to know when to stop reading. If the reader can stop reading without confirming that the writers have stopped writing, this can lead to the writers becoming blocked on sending into this channel, which may prevent them from performing necessary cleanup actions (signaling `.Done()` on a waitgroup, for instance) that cause hangs in other areas.

* Thread a cancellation ctx through every reader and writer. This ensures that nothing hangs, but can result in messages that are sitting in the the channels being dropped. If other areas of code have an assumption like, "every accepted request will receive a response", this can break that.

In addition, many developers have a gut instinct to add some amount of buffering to their channels, which usually results in these backpressure / channel issues being papered over during low-load unit tests, only to rear their head during higher load integration tests or in production, when the debugging story is much more difficult.

[1]: https://pkg.go.dev/reflect#Select

[2]: https://blog.golang.org/pipelines

Re:Updating the Go Memory Model by sagichmal on Monday July 12, 2021 @09:43PM (#27816923)
> Use `reflect.Value.Select`

Never really a good idea, and never necessary.

> create a single channel owned by the reader

Channels cannot be effectively owned by their reader(s), the contortions you have to bend the code into to make that work never really make sense. That's just a constraint of the type, but it's hardly a problem -- it makes the thing easier to model. So this isn't really an option on the table.

> a Merge function

Yes! The answer. And goroutine per channel is kind of the point of using them! Nothing inefficient about it.

> a function that takes two channels . . .

Now there's some inefficiency! ;) No reason to do this, given Merge.

--

> None of these options are particularly intuitive.

The merge option seems perfectly intuitive to me, assuming you understand channels have to be owned by a singular writer.

Re:Updating the Go Memory Model by tene on Monday July 12, 2021 @08:35PM (#27816616)
This kind of trouble is exactly why Rust's channels feel much more intuitive to me than Go's.

With channels in Rust, the channel is closed when either all senders or all receivers are dropped. This means that doing the default obvious thing is also correct, for a much larger set of tasks than made easy by Go's API choices, and it stays correct under refactoring.

Re:Updating the Go Memory Model by sagichmal on Monday July 12, 2021 @09:32PM (#27816875)
> I still run into deadlocks and panics (writing to a closed channel) when I try to use channels beyond the simplest use cases.

Wild!

The rule is that channels are owned by a single goroutine, who's uniquely responsible for sending on them, and closing them. That's basically it. Do that and everything works fine, in my experience.

Re:Updating the Go Memory Model by cp9 on Monday July 12, 2021 @01:00PM (#27812431)
same, I like go and I like channels in theory but they are too primitive in practice. I am much more likely to use waitgroups and errgroups than anything with raw channels
Re:Updating the Go Memory Model by tene on Monday July 12, 2021 @06:27PM (#27815861)
Channels as a high-level abstraction are pretty simple, but API and implementation details matter.

Go's implementation of channels are very simple to use for some very simple use-cases, but Go's API choices mean there are a lot of subtle, non-obvious details you need to learn and keep in mind to do anything nontrivial.

I really like https://medium.com/justforfunc/why-are-there-nil-channels-in... as an example. Reading from two channels safely should be a simple task, but just doing the intuitive thing will look like it works for many uses until it starts fabricating zero values, or blocks forever, or spins the CPU at 100% doing no work.

I really love channels, but I really hate working with Go's channels.

The channel API in that other language well-known for its good concurrency support is much simpler to learn and use for me, without as many subtle sharp edges, but that's possibly a bit off-topic, so I've removed a detailed comparison.

Re:Updating the Go Memory Model by tapirl on Monday July 12, 2021 @06:56PM (#27816055)
What you described are all well defined in the docs.
Re:Updating the Go Memory Model by tene on Monday July 12, 2021 @08:17PM (#27816534)
Sure, it's all out there, and it's possible to build useful software using Go's channels.

I was specifically trying to explain why I say that Go's channels are not intuitive, because they require studying and memorizing these other arbitrary complications.

I'm also curious about what docs you're referring to, exactly. Here's what I found when looking for golang channel docs:

https://golang.org/ref/spec#Channel_types

If the documentation's described behaviour, along with code patterns to accommodate that behaviour, are intuitive to you after reading this, then you have a very different perspective on the world than I do.

I also found these: https://tour.golang.org/concurrency/2 https://golang.org/doc/effective_go#channels

Unless I've missed it in my reading, I don't see any of these clearly stating that the single-return form of channel receive will fabricate zero values when misused, or describing how you need to replace a closed channel with a nil when selecting on multiple channels to avoid spinning the CPU when it's been closed.

I agree that this stuff is learnable. I have learned it, and so have you. I agree that there are learning resources out there that help with learning the nuances of using Go's channels well.

Hopefully this can help you feel less shocked the next time someone says that Go's channels are not intuitive. If you disagree, can you explain more about how Go's channel management choices are more intuitive than the alternatives to you?

[Edit: I found the documentation on producing a zero value when reading from a closed channel here: https://golang.org/ref/spec#Receive_operator]

Re:Updating the Go Memory Model by tapirl on Monday July 12, 2021 @09:12PM (#27816797)
> Unless I've missed it in my reading, I don't see any of these clearly stating that the single-return form of channel receive will fabricate zero values when misused, or describing how you need to replace a closed channel with a nil when selecting on multiple channels to avoid spinning the CPU when it's been closed.

You certainly missed this: https://golang.org/ref/spec#Close

You might also need read https://blog.golang.org/pipelines and https://blog.golang.org/concurrency-timeouts

If you need a good summary, you could read my articles: * https://go101.org/article/channel.html * https://go101.org/article/channel-use-cases.html * https://go101.org/article/channel-closing.html

Re:Updating the Go Memory Model by beltsazar on Monday July 12, 2021 @01:26PM (#27812748)
I too thought Go’s channels are intuitive.. until I found Channel Axioms: https://dave.cheney.net/2014/03/19/channel-axioms

I keep going back to that page whenever I need it, because I could never remember what the axioms are.

Re:Updating the Go Memory Model by naikrovek on Monday July 12, 2021 @09:54PM (#27816976)
these are 2nd nature to me now, and if I ever use another language that has channels, and does NOT have these axioms, them I'm basically screwed.
Re:Updating the Go Memory Model by parhamn on Monday July 12, 2021 @01:59PM (#27813142)
The main issue is channels are a very low level primitive (not far from mutexes and atomics). When generics come out we there will emerge a library that handles a lot of the common channel idioms that people are implementing (perhaps somewhat incorrectly).

Thanks like fanouts, multidispatch, sinks, chaining, simple concurrency, etc will likely emerge in a lib in the future.

Re:Updating the Go Memory Model by ra7 on Monday July 12, 2021 @02:38PM (#27813541)
This definitely the biggest thing I'm looking forward to in Go's post-generics world. It's frustrating to write the same concurrency abstractions like fan out, error handling etc for every type and reason about channel behavior carefully every single time.
Re:Updating the Go Memory Model by voidfunc on Monday July 12, 2021 @11:46AM (#27811452)
Hard agree about channels being a mess in Go. I avoid channels as much as possible in favor of mutexes but the problem is the community is so aligned towards using channels you get funny looks when you solve problems with mutexes.
Re:Updating the Go Memory Model by eknkc on Monday July 12, 2021 @11:25AM (#27811223)
Whever I tried to use channels I regretted it at the end. I believe the channels in Go are the worst part of the language.

I use mutexes.

Re:Updating the Go Memory Model by tapirl on Monday July 12, 2021 @11:31AM (#27811290)
How could it be? Programming with channels is not only intuitive but fun for many cases: https://go101.org/article/channel-use-cases.html

Yes, channels are not always best solutions for any case. There are cases mutexes are more suitable. Just choose the best solution for specified cases. Always sticking to one is not a good attitude.

Re:Updating the Go Memory Model by jeffreybolle on Monday July 12, 2021 @11:38AM (#27811373)
I agree with this.

We used to do a coding exercise at a previous job that was really well suited to mutexes and whenever someone tried to use channels they ended up with a much more complex solution.

Equally, there are lots of cases which lend themselves to channels really well. Waiting on multiple things at once is hard to without channels and select for example.

Re:Updating the Go Memory Model by nemo1618 on Monday July 12, 2021 @01:07PM (#27812517)
I agree (well, maybe not the worst, but certainly one of the worst), but in retrospect it's hard to say whether Go would have taken off the way it did, had it not pushed the concurrency angle so hard. I think Go's focus on software engineering (as well as its "stubbornness" in general) is far more valuable than its concurrency features, but there's no question the latter is more exciting and likely to drive growth.
Re:Updating the Go Memory Model by sagichmal on Monday July 12, 2021 @09:30PM (#27816871)
> A channel can be buffered or unbuffered

Yes, with predictable results.

> it can be closed

Yes, by the owner/sender, who then understands it as invalid. The effect of receiving on a closed channel is well-defined and predictable.

> and there's different behavior of [recv/send] whether it's in a select or not

I don't think that's true? What do you mean?

> And you often end up with multiple channels as well

Sure! That's part of their power.

> Reviewing code with channels is often more confusing than code that uses mutexes

I totally agree that goroutines and channels are often over-used by new Go programmers. If what you're trying to do is simply protect concurrent access to some shared state, a mutex is the far better choice.

Re:Updating the Go Memory Model by azth on Monday July 12, 2021 @12:53PM (#27812314)
Not to mention that there is no way to ensure immutability when passing messages in channels, and the language doesn't help you there. This is a recipe for race conditions.

Furthermore, you can easily have hierarchies of "goroutines", everything has to be painstakingly built from scratch and is very error prone.

> Another aspect of Go being a useful programming environment is having well-defined semantics for the most common programming mistakes, ... Quoting Tony Hoare

One of the most common programming mistakes of all, dubbed by Hoare himself as the "billion dollar mistake": null pointers, yet it was put into go without any consideration.

Re:Updating the Go Memory Model by throwaway894345 on Monday July 12, 2021 @01:37PM (#27812883)
> One of the most common programming mistakes of all, dubbed by Hoare himself as the "billion dollar mistake": null pointers, yet it was put into go without any consideration.

I get the distinct impression that people who invoke this boilerplate Hoare quote argument don't understand that null pointer bugs are "a billion dollar mistake" because nullable pointers have been idiomatic in every major programming language for the last 40+ years, not because they are individually particularly expensive.

Yeah, Rust-like enums would probably be strictly better than null pointers, but people talk about null pointers like they are going to doom your project when in reality they're mere papercuts--a small subset of all type errors--and there are whole projects that are written in fully dynamically typed languages! Indeed, there are worse problems than type errors in a language--you could have an impoverished ecosystem, poor tools (especially build tools), or abysmal performance, and many of the most popular programming languages have two of the three and null pointers. Go is fortunate to have only null pointers (papercuts) working against it.

Re:Updating the Go Memory Model by voidnullnil on Monday July 12, 2021 @03:39PM (#27814130)
I've never bothered to look up the hoare thing, but null as a member of all types increases the amount of time spent studying any given API (and of course, there are lots of null pointer bugs from people who didn't bother to study the API sufficiently).
Re:Updating the Go Memory Model by throwaway894345 on Monday July 12, 2021 @04:55PM (#27815019)
Yeah, as previously mentioned, I fully agree that nullable pointers kind of suck and exhaustive pattern matching on enums is strictly better. I'm taking issue narrowly with abusing the "billion dollar mistake" quote to exaggerate the severity of the problem.
Re:Updating the Go Memory Model by voidnullnil on Monday July 12, 2021 @05:12PM (#27815198)
It's a big problem in Java at least. In Go you have value types by default and they try to make zero a meaningful value.
Re:Updating the Go Memory Model by throwaway894345 on Monday July 12, 2021 @05:20PM (#27815274)
It's not a big problem when you look at the problems that plague even the most popular programming languages. At this point I can only refer you to my previous posts because I'm repeating myself. :)
Re:Updating the Go Memory Model by azth on Tuesday July 13, 2021 @09:40AM (#27820803)
Which problems do Java and C# have that golang doesn't? Incidentally, both of them have solutions to the billion dollar mistake, which golang doesn't.
Re:Updating the Go Memory Model by azth on Tuesday July 13, 2021 @09:39AM (#27820795)
Which is arguably even worse, because it goes on undetected and the code ends up producing incorrect results silently.
Re:Updating the Go Memory Model by saghm on Monday July 12, 2021 @03:46PM (#27814220)
The thing that always got me was that closing a channel on one end (I think the receiving end, but I can't remember exactly since I haven't programmed Go for a while) causes an error if the other side is open, but doing that on the other end (I think the front) doesn't give that issue. Sure, there's an explanation for that, but I feel like you could just as easily make a justification for acting consistently on both ends.
Re:Updating the Go Memory Model by tene on Monday July 12, 2021 @08:50PM (#27816695)
In Go, sending a value through a closed channel panics the program, and receiving a value from a closed channel immediately returns the zero value for the channel (and a false, if you use the two-return-values form of the receive operator).

There's no difference in what side closes the channel, I think, just in how a closed channel behaves when you try to send or receive on it.

Personally, I think it's terrifying that the "easy" option just lies to you instead of crashing the program, but I kind of see how it fits with some of Go's other design decisions that I'd also have made differently (Option/Result instead of pervasive nullability).

Re:Updating the Go Memory Model by owl57 on Monday July 12, 2021 @03:55PM (#27814319)
Don't know Go, but this way would be logical. "I've done sending" and "I refuse to handle more messages" aren't that symmetrical.
Re:Updating the Go Memory Model by silasdavis on Monday July 12, 2021 @03:41PM (#27814148)
Exactungly, Go's flavour of channels are full of subtle bugs. I could count on N hands the number of times I've had to make a channel to drain a channel.
Re:Updating the Go Memory Model by Thaxll on Monday July 12, 2021 @12:41PM (#27812147)
Channel are pipe if you want to use a mutex you have to re-invent a queue with a mutex which is very different, a mutext alone does not just replace a channel.
Re:Updating the Go Memory Model by ryanschneider on Monday July 12, 2021 @12:48PM (#27812253)
The thing that has bitten me the most is somewhat related: the lack of any uniform “deep copy” or immutability support. Sure I can make a `chan Foo` but if Foo is a struct with an embedded pointer in it you just aliased a pointer you probably didn’t mean to.

If there was supported for optional value types rather than using pointers that would help too.

Re:Updating the Go Memory Model by skywhopper on Monday July 12, 2021 @02:58PM (#27813729)
He’s stating the goal of the design, not an assertion that they got everything right. Obviously there are lots of places where the design fell short. But the philosophy behind the approach to future design is unchanged. Channels were an attempt to add something very new to the language and so it’s not a surprise that the design ended up being problematic. I’m sure Russ has thoughts on that, too.

But the vast majority of Go code doesn’t use channels. The fact that lots of people have trouble with them doesn’t change the fact that the vast majority of Go code does follow this principle.

Re:Updating the Go Memory Model by ferdowsi on Monday July 12, 2021 @04:12PM (#27814515)
I believe the guidance addresses this.

> Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access.

> To serialize access, protect the data with channel operations or other synchronization primitives such as those in the sync and sync/atomic packages.

Go gives us `sync.WaitGroup` and `errgroup.Group` as abstractions to manage concurrent operations. I always end up guiding developers towards using these unless they absolutely can't (like responding to OS signals requires interacting with channels).

Re:Updating the Go Memory Model by pkaye on Monday July 12, 2021 @05:47PM (#27815500)
For OS signals now they added NotifyContext() which uses context instead of channels.
Re:Updating the Go Memory Model by axaxs on Monday July 12, 2021 @04:18PM (#27814584)
I find channels massively overcomplicate many things. Cool in theory, not so useful in practice.

Anymore, I only use them as a semaphore to keep less than X routines running at once.

Re:Updating the Go Memory Model by tschellenbach on Monday July 12, 2021 @04:03PM (#27814415)
You can write Go for years without ever touching channels. I agree though, they are extremely easy to make mistakes with.
Re:Updating the Go Memory Model by voidnullnil on Monday July 12, 2021 @04:14PM (#27814539)
Channels and an alt operator are much more powerful than locks but eh I can't remember the edge cases in Go's channels so you might be right,
Re:Updating the Go Memory Model by convolvatron on Monday July 12, 2021 @11:22AM (#27811188)
to me the overriding problem here is that selects need to be carefully refactored when you add in other interactions. of course.

but this means there are as you say subtle non-local effects...and these kick in exactly when you don't want them - when you are trying to thread in a new feature in an existing codebase.

I prefer to just ignore channels. then that statement makes sense :)

Re:Updating the Go Memory Model by richardwhiuk on Monday July 12, 2021 @12:37PM (#27812087)
I agree - I find Go's error handling encourages subtle bugs.
Re:Updating the Go Memory Model by voidnullnil on Monday July 12, 2021 @04:22PM (#27814626)
Go's errors have basically all the problems of exception handling in previous generation languages (and a few novel ones).

This explains it (tl;dr nobody bothers to formally define what errors they return, and the blindly propagate such unspecified errors, and it leads to ambiguity and unintentional program behavior)

https://www.lainchan.org/%CE%BB/src/1612397614615.jpg

https://www.lainchan.org/%CE%BB/src/1612397701473.jpg

https://www.lainchan.org/%CE%BB/src/1612398029019.jpg

Re:Updating the Go Memory Model by astrange on Tuesday July 13, 2021 @01:22AM (#27817827)
Swift considered this and still decided to have untyped error returns. The issues are:

- wrapping every underlying error in your own error type is not helpful

- but defining every underlying error in your own type confuses your implementation (and especially dependencies) with your interface

- and most errors can't be handled in code anyway

There are a few errors (mostly in file operations) that are individually handled as normal parts of life, but otherwise they really are just there to send back up to the user. It's more important to know where an error can happen than what it is.

Re:Updating the Go Memory Model by Philip-J-Fry on Monday July 12, 2021 @01:46PM (#27812991)
The only place I would agree with you is when someone accidentally shadows an `err` variable. Although, I'd probably attribute this more to shadowing than the error handling itself.

Other than that, I can't think of any way the error handling subtly introduces bugs.

Re:Updating the Go Memory Model by richardwhiuk on Friday July 16, 2021 @05:24AM (#27854460)
Go maps are wonderful examples.

If you index into a map, then you get a tuple (val, err), where `err` is non nil if the value exists, and `val` is the value in the map or "the default construction of the value".

Go also silently discards tuple members - e.g. `v = map[4]` is valid, and silently discards the error.

This kind of means that all go maps can accidentally be defaultdicts (in Python terminology), but it's also an excellent way to introduce bugs without meaning to.

Re:Updating the Go Memory Model by randomdata on Monday July 12, 2021 @02:15PM (#27813314)
The onus is on the programmer to understand what errors might be returned, which isn't always easily gleaned, especially when calling a function that calls other functions that call other functions, each pushing the error up the stack. The error interface does not provide much information on its own.

The mistake of the programmer missing an error that should be handled in a particular situation could be considered a subtle bug, I suppose. This is different to languages that have compiler-enforced error handling, where if you forget to handle a certain case the code won't compile.

Re:Updating the Go Memory Model by jerf on Monday July 12, 2021 @02:44PM (#27813585)
None of that is unique to Go, and, indeed, the question of "what errors may be returned" in general is not solved in any language that I know of satisfactorily, especially once you include any form of polymorphism. Compiler-enforced error handling in general only works on the direct possible errors from the function you just called, not the transitive closure of everything that could have happened, again, especially once you include any sort of polymorphism, meaning that you don't even know at the time you're writing the error handler what the possibilities are, and depending on the language, it may not even be possible to statically know. Don't mistake "option" or "sum types" for a solution to this problem; they aren't.

(The only language I know where this is nominally "solved" is Java if you confine yourself to its checked exceptions, but nobody considers that solution "satisfactory", and indeed can be taken as a rough-and-ready proof that there may not even be a "static" solution to this problem that is usable. I'm beginning to think of it as "Errors don't compose, because they contain every possible pathology that would prevent values from composing." At this point, based on the consistent failures trying to create composable error solutions despite substantial effort poured into it across multiple languages, I am going to operate under the assumption there is no such solution until someone produces one.)

Re:Updating the Go Memory Model by AlexCoventry on Monday July 12, 2021 @01:23PM (#27812716)
Can you give an example?
Re:Updating the Go Memory Model by whateveracct on Monday July 12, 2021 @11:36AM (#27811341)
> it can be closed

..at most once!

Re:Updating the Go Memory Model by AtNightWeCode on Monday July 12, 2021 @04:27PM (#27814679)
Is it not just basically an implicit actor model.