News for hackers. Stuff that matters.
Re:Even in Go, concurrency is still not easy by naikrovek on Thursday September 03, 2020 @12:19AM (#24360765)
Hmm, I don't know. I had this limited concurrency issue on the first Go program I ever wrote, and I'm very new to concurrent stuff, certainly if you only count the past ten years or so.

I figured this out in Go about 30 minutes after I realized I needed to limit concurrency, and I'm a dumbass, basically.

So, I'm not sure that "still not easy" is accurate, especially given that I have a lot of trouble conceptualizing async and await, as used in C#. In fact, I don't think I've ever successfully used that paradigm in C#. I've been told that C# async and await are "easy" and this article says Go concurrency is "still not easy" and I've had the exact opposite experience in both cases.

In fact I really don't understand why async and await is even a thing anymore, given the paradigm that Go uses and the example it sets. It is almost supernatural in its ease, for me.

Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @01:19AM (#24360987)
The paradigm that Go uses only fits a very narrow case of concurrency, and actually it is quite old.

Modula-2, Concurrent Pascal, Active Oberon already had similar constructs.

Async/await are configurable, not only can you replace the task scheduler algorithms, if the type being applied on does provide some magic methods, the runtime will use them instead of the default ones.

This allows for very powerful concurrency optimizations and workflows, that still look like plain async/await calls.

Re:Even in Go, concurrency is still not easy by YetAnotherNick on Thursday September 03, 2020 @03:22AM (#24361578)
Async/await can easily be implemented through channels. I view channel as providing superset of functionality then async/await at least. Granted there are other paradigms like lock free synchronization which can not be directly be translated to channel.
Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @03:26AM (#24361597)
Something that looks like async/await and has a rather bad performance can be implemented through channels.

Nestable stackless coroutines cannot. By definition they require compiler support.

Your proposed solution will not have any of the advantages of stackless coroutines (effect typing of async functions and perfectly optimal memory use) while suffering all the performance penalty of running on top of a channel.

Re:Even in Go, concurrency is still not easy by YetAnotherNick on Thursday September 03, 2020 @03:35AM (#24361655)
Channels have close to zero performance penalty. If you are talking about goroutines, you need to come up with data or code. I found goroutines to be really cheap to create.
Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @05:26AM (#24362275)
Not true: https://www.jtolio.com/2016/03/go-channels-are-bad-and-you-s...

I'm pretty sure the implementation has seen some improvements since then. But using a channel for async await is the same as using a global mutex every time you await. That is bound to be contentious on high load.

Re:Even in Go, concurrency is still not easy by Taek on Thursday September 03, 2020 @11:30AM (#24364988)
I think today channels cost a few hundred nanoseconds. For most applications, this is 'close to no overhead'. If you are doing a really tight loop of course or have threads that bump into eachother a million times per second, you're going to want a different primitive.

But most programmers aren't going to see any overhead using channels instead of something else.

Re:Even in Go, concurrency is still not easy by unscaled on Friday September 04, 2020 @11:28PM (#24381201)
Please remember the approach is "every context switch should go through a centralized channel". This including waiting on all other channels.

I need to see some benchmark to this level to be proven the overhead is negligible.

And of course, there is still the question of WHY would you ever want to do that. What can be gained from simulating an async-await model on top of a channel.

Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @03:25AM (#24361592)
So how do you change the scheduling algorithm though channels?
Re:Even in Go, concurrency is still not easy by YetAnotherNick on Thursday September 03, 2020 @03:33AM (#24361639)
async/await and channel are independent of scheduling. Nodejs doesn't have multithread but has async/await. I find goroutines lightweight enough in my personal testing and didn't tried tweaking it.
Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @03:51AM (#24361735)
On .NET and C++ they surely are not, given that you can fine tune them either by providing new schedulers, magic methods on awaitable objects that can be either value or reference types.

So not only you can control the scheduling algorithm, you can control how the async/await state gets managed in memory and also introduce additional parameters that influence the decision when to switch tasks across owner threads.

Try to do this in Go via channels, while offering just the channel syntax to the caller.

EDIT: The only thing in common between JavaScript, Python, .NET, C++ and Kotlin async/await is the name of the primitives.

Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @05:46AM (#24362367)
Custom stackless coroutine schedulers are also available in Kotlin.

Python also also allows you to customize the event loop, I think, but overriding AbstractEventLoop. A M:N implementation would probably be inefficient though because of the GIL.

Basically almost all stackless implementations I know allow for custom scheduling. The fact that some of them can't do M:N and are restricted to 1:N is just a limitation of dynamic programming languages. There is a global interpreter state which prevents you from using multiple OS threads efficiently. You either rely on GIL (CPython and Ruby MRI) or rely on just allow only one thread (Node, Lua).

So GP is right in that not _all_ async/await implementations will allow you to fully customize your threads but I think the main point here is that stackless coroutines are practically _a prerequisite_ for enabling custom scheduling (like C# et. al allow). You cannot have this custom scheduling with stackful coroutines, because that would require the scheduler to have access to low level memory management for controlling the stack.

I've never seen a language which allows that. I guess this could be doable on C++, but I really don't see the point, since it will require a lot more effort than the current stackless C++ coroutines.

Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @06:09AM (#24362502)
That is my whole point, this is not doable with Go channels alone as primitive.
Re:Even in Go, concurrency is still not easy by gpderetta on Thursday September 03, 2020 @06:56AM (#24362744)
Yes, that's not a good primitive. You need some delimited form of call/cc.
Re:Even in Go, concurrency is still not easy by gpderetta on Thursday September 03, 2020 @06:55AM (#24362741)
> I've never seen a language which allows that.

It is not really hard. You can use the exact same scheduler you would use for stackless coroutines.

For example in C++ land you can use boost.asio to schedule both stackless coroutines and boost.fibers (or whatever have you). Boost.asio predates both and doesn't care.

Re:Even in Go, concurrency is still not easy by pansa2 on Thursday September 03, 2020 @01:42AM (#24361077)
> I really don't understand why async and await is even a thing anymore

Programming with threads is hard because they can context-switch at arbitrary points. Goroutines are nothing more than (lightweight) threads.

Stackful coroutines (as in Lua) aren’t much better, because they can context-switch at any function call.

Code that uses async-await is different - it can only context-switch at points explicitly marked with `await`.

Re:Even in Go, concurrency is still not easy by diek on Thursday September 03, 2020 @02:35AM (#24361348)
async/await in the general sense doesn't have anything to do with the preemption policy of a particular language implementation. For instance, C# implement async/await as keywords on top of its existing concurrency model.

That's really what it comes down to: cooperative multi-tasking vs preemptive multi-tasking.

Windows 3.1 had cooperative multi-tasking. Everything went great until one program didn't yield control, and then the whole system ground to a halt. That should sound familiar to any Node.js developers with their event loop.

Cooperative multi-tasking also extremely complicates code that actually uses the CPU. If you look inside libuv, it even has to jump through hoops with its encryption functions by effectively calling yield() in between rounds of calculation.

Re:Even in Go, concurrency is still not easy by csharptwdec19 on Thursday September 03, 2020 @01:44PM (#24366560)
>If you look inside libuv, it even has to jump through hoops with its encryption functions by effectively calling yield() in between rounds of calculation.

I would have assumed the Yield was there to help with protect against timing pokes...

Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @03:31AM (#24361630)
Since stackless coroutine schedulers can often be customized, you could (and are encouraged to) launch coroutine code doing heavy performance on a thread based scheduler.
Re:Even in Go, concurrency is still not easy by jayflux on Thursday September 03, 2020 @04:29AM (#24361936)
> Programming with threads is hard because they can context-switch at arbitrary points. Goroutines are nothing more than (lightweight) threads.

Goroutines don’t switch at arbitrary points, they get swapped out during IO, networking or sleeping (they’re cooperative not pre-emtive). So they’re not like threads in that fashion they’re more closer to JavaScripts event loop (I think both node and go use libuv?). The only difference is the await syntax isn’t needed because Go “awaits” by default, that is the coroutine will block and yield the thread while it waits.

Re:Even in Go, concurrency is still not easy by pansa2 on Thursday September 03, 2020 @04:54AM (#24362073)
> Goroutines don’t switch at arbitrary points

That was only ever an implementation detail, though - you always had to program as though goroutines were preemptive. And sure enough, from Go 1.14, “Goroutines are now asynchronously preemptible” [0].

> Go “awaits” by default

Yes, the main selling point of async-await isn’t cooperative vs preemptive, it’s the explicit context switches compared to the implicit context switches of threads, goroutines and stackful coroutines.

[0] https://golang.org/doc/go1.14

Re:Even in Go, concurrency is still not easy by jshen on Thursday September 03, 2020 @01:25AM (#24361006)
I’ll bet your Go code, if it isn’t trivial, has race conditions all over and you just don’t realize it.
Re:Even in Go, concurrency is still not easy by naikrovek on Thursday September 03, 2020 @11:11AM (#24364787)
It's not trivial and I found a lot of races, yes, but I refactored over a couple days to completely isolate work within a goroutine so that data is only passed in and out of them via channels, and with that all races went away. The tool has been used in production extensively for a couple of years now, converting 3D model files from one format to another, and it's running great, and very fast as well.

So, you're right, but it has been addressed.

Re:Even in Go, concurrency is still not easy by xmprt on Thursday September 03, 2020 @02:02AM (#24361154)
Can't that be said for most languages? Is there anything special that other languages do to make race conditions less likely than they are in Go?
Re:Even in Go, concurrency is still not easy by pizza234 on Thursday September 03, 2020 @02:13AM (#24361204)
I believe it's correct that that can be "said for most languages".

Since you're asking, Rust's ownership/type system, for example, has checks in place that prevent at least some classes of concurrency errors.

For example, you can't directly modify a variable from multiple threads, due to the so-called "move" rules:

    let mut myvar = vec![];

    thread::spawn(move || {
        myvar.push(1);
    });

    myvar.push(1); // can't do this; variable has been moved
Re:Even in Go, concurrency is still not easy by ben0x539 on Thursday September 03, 2020 @02:37AM (#24361365)
I think Go invites mistakes like this being made over and over, because (in my subjective opinion as someone who writes a lot of Go at work but isn't extremely happy about that) you have to write out the basic low-level concurrency patterns over and over, because the language tries to resist abstraction.

In other langs I'd expect to see this code use a generic map-reduce/parallel map helper function or something like that, but in Go people seem to be happier writing for-loops and spawning goroutines by hand every time.

Re:Even in Go, concurrency is still not easy by iainmerrick on Thursday September 03, 2020 @03:44AM (#24361702)
A lot of people are making tangential points in this discussion (“concurrency is always hard!”) but you’ve put your finger on the key point.

Concurrency is hard, and one fairly successful approach is to provide battle-tested libraries for these high-level use cases (“run M functions with max parallelism N”). Go gives you low-level concurrency tools, but actively resists the kind of abstraction that would let you make reusable high-level tools, so that robust library doesn’t exist.

It’s a strange lack, as it’s meant to be a concurrency-focused language, and the standard library is absolutely terrific in other areas (eg networking and encryption).

Re:Even in Go, concurrency is still not easy by apta on Thursday September 03, 2020 @02:28PM (#24367113)
It just shows golang designers lack of experience in language design (it's stuck in the 70's). They may have written a lot of code, but it doesn't automatically make them good language designers.
Re:Even in Go, concurrency is still not easy by ben0x539 on Friday September 04, 2020 @01:53AM (#24372107)
I'm not really sure about this. I think the language they designed has the properties they desired, in that they consciously forewent a lot of potential benefits because some notion of simplicity is more important to them. So in that sense I think they were successful at designing a language.

Like some other comment here says, you run into bugs like this early on and they're not necessarily show-stoppers. Maybe keeping the language simple and ensuring that most code is readable if you only know the language and the stdlib (and not some fancy concurrency library) is worth the occasional bug?

Of course that doesn't necessarily mean that Go is the language I want to use for every use case under the sun, but that probably wasn't a goal of Go to begin with.

Re:Even in Go, concurrency is still not easy by apta on Friday September 04, 2020 @11:50AM (#24376040)
> I think the language they designed has the properties they desired

That's a tautology though :) Of course any language designer will design a language according to the properties he/she desires.

> you run into bugs like this early on

Not necessarily. Race conditions are hard to find. It's established that "simplicity" is not a straight forward metric to measure. Having a simplistic language just means that complexity is being pushed on the programmer, since most non-trivial programs are complex by definition.

Regardless of what the goals of golang were (they kept changing their definition of "systems language"), the fact that it's being pushed for almost everything, due to hype and fad driven development, and the inexperience of many programmers, is an issue. It's almost ironic that golang is used more outside of google than in.

Re:Even in Go, concurrency is still not easy by ben0x539 on Friday September 04, 2020 @06:05PM (#24379357)
> That's a tautology though :) Of course any language designer will design a language according to the properties he/she desires.

Eh, you can just fail, too, no? Like I could sit down and set myself the goal of designing a language that achieves goal X while staying under complexity limit Y (by whatever measurement), and then end up having to compromise on either of them because I can't quite figure out a good way to achieve a particularly tricky part of X without adding a lot more complexity than I initially expected. Or I could decide I want to design a language that's portable across many CPU architectures but inadvertently bake in a lot of x86isms that make it awkward/inefficient to implement elsewhere.

> Not necessarily. Race conditions are hard to find. It's established that "simplicity" is not a straight forward metric to measure. Having a simplistic language just means that complexity is being pushed on the programmer, since most non-trivial programs are complex by definition.

Ah, I didn't mean that concurrency bugs are quickly found or solved. I definitely agree with the just-moving-the-complexity-around part. I just meant that soon after getting started with Go, you'll probably have run into a bunch of concurrency bugs already and developed the kind of mental scarring and automatic averse reactions to certain concurrency situations that'll let you cope and still get things done despite it being clearly suboptimal.

> the fact that it's being pushed for almost everything, due to hype and fad driven development, and the inexperience of many programmers, is an issue.

But this is a kind of success too, isn't it? Presenting your solution to a problem in a way that appeals to enough "inexperienced" people to create the hype/fad isn't trivial, so they must have been doing something right. I don't think it's just a case of "$bigcorp does it, so it's cool", plenty of $bigcorp languages or technologies don't really catch on, let alone on Go's scale.

I think there really is a niche where Go is the best solution (or at least a significant local maximum), even if that is only "'inexperienced' programmers can get started with, say, highly concurrent http services", it's still _something_.

(Again, disclaimer: I use Go at work and I like to give my (non-google) employer and my coworkers enough credit to think that it's not just because of hype/fad, so I may be biased.)

Re:Even in Go, concurrency is still not easy by IshKebab on Thursday September 03, 2020 @07:07AM (#24362800)
> Is there anything special that other languages do to make race conditions less likely than they are in Go?

Yes, other languages provide you with more tools than the basic nuts and bolts. For example a language might provide you with a "parallel for" primitive that is well tested.

The issue is not exactly that Go's concurrency stuff is bad, it's just that it doesn't give you much help so you have to implement it all yourself. Also the lack of generics means it is difficult to write libraries for this stuff.

Re:Even in Go, concurrency is still not easy by ed25519FUUU on Thursday September 03, 2020 @11:03AM (#24364710)
The frustrating part is that the standard lib is just good enough that you also don’t tend to get good adoption for third party libraries, which could easily provide better abstractions.
Re:Even in Go, concurrency is still not easy by continuational on Thursday September 03, 2020 @02:10AM (#24361190)
Some languages emphasize persistent data structures, or even enforce immutability, which means you can only introduce race conditions via explicit concurrency. That's a lot less surface area for bugs.
Re:Even in Go, concurrency is still not easy by johnisgood on Friday September 04, 2020 @11:19AM (#24375694)
Yep, Ada/SPARK: https://docs.adacore.com/spark2014-docs/html/ug/en/source/co...

Search for "race condition", or just "race".

This section in particular is about preventing data races: https://docs.adacore.com/spark2014-docs/html/ug/en/source/co...

This one is about preventing race conditions: https://docs.adacore.com/spark2014-docs/html/ug/en/source/co...

Re:Even in Go, concurrency is still not easy by thesuperbigfrog on Thursday September 03, 2020 @03:25AM (#24361593)
Clojure uses immutable data structures and software transactional memory:

https://clojure.org/about/concurrent_programming

Re:Even in Go, concurrency is still not easy by jshen on Friday September 04, 2020 @03:15AM (#24372520)
I love clojure, and I’ve written some high concurrency production apps in it. Concurrency is still hard in clojure, and that’s assuming all of your state and logic can live in a single process. That assumption usually doesn’t hold, and then concurrency gets way harder.
Re:Even in Go, concurrency is still not easy by papaf on Thursday September 03, 2020 @01:33AM (#24361036)
Go has a built in race detector that most teams run during CI.

[1] https://golang.org/doc/articles/race_detector.html

Re:Even in Go, concurrency is still not easy by ben0x539 on Thursday September 03, 2020 @02:30AM (#24361314)
This roughly detects data races, not all race conditions.
Re:Even in Go, concurrency is still not easy by gpderetta on Thursday September 03, 2020 @03:49AM (#24361729)
And not all possible data races.
Re:Even in Go, concurrency is still not easy by andonisus on Thursday September 03, 2020 @11:22AM (#24364898)
This does not run if you have more than 10K go routines :(
Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @03:24AM (#24361590)
Calling the stackful coroutine model as offered by Go superior to stackless coroutines ("async/await") is highly controversial.

Stackful coroutines are not new - it might surprise you to know that they are older than async/await, existing in full form probably introduced at least in 1967, with the first documented implementations starting around 1958. Stackful coroutines were implemented in a wide array of popular languages in the 1980s as pjmlp noted.

Stackless coroutines need better clarification, since the paper that defined this term [2] refers to a limited subset of stackless coroutines that we would nowadays just call "generators". To the best of my knowledge, when the paper came out generators were the only type of stackless coroutine in existence, and thus came the perception that stackless coroutines cannot be nested (they can) and that stackful coroutines are strictly superior to stackless coroutines (they aren't). You can see an example discussion here: https://news.ycombinator.com/item?id=16318535

The real difference is not nesting, but the fact that stackful coroutines manage a growable stack dynamically at runtime, while stackless coroutines pass the job on to the compiler to evaluate all possible nesting permutations, and create an optimally memory-efficient stack machine that represents the coroutine.

To summarize the pros and cons of each, I think you can say:

Stackful pros:

* [No colored functions](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...) - summed up it says: I can write asynchronous I/O code the same way I write multi-threaded code or non-blocking code. It's definitely more ergonomic, but also means your code can hold more surprises for you. Not everybody thinks 'Colored Functions' are an absolute advantage, otherwise we wouldn't have Effect Typing. * Less GC allocations: stack is [re-]allocated in bulk every time time a lightweight starts (go statement) or whenever it needs to grow. Stackless coroutines typically allocate a smaller object on every call to an async function.

Stackless pros: * Clear yield points: you always know the points where your coroutine could suspend and control would be moved to another coroutine: whenever you see an await statement[3]. This is why function colors are needed - to be explicit about the control flow. * Less memory waste and less overall allocation size: the "compile-time stacks" generated by stackless coroutines are perfectly efficient. There is no unnecessary memory allocated.

I don't think it is clear that one model is strictly superior to the other, but I do believe that if you're aiming for ease of correct use (rather than ease of use), the stackless model is clearly better. It is definitely somewhat harder to wrap your mind around, but it is also somewhat harder to get a deadlock or have a hard-to-detect data race in this model.

[1] Simula 67 (and probably Simula I too), and first implemented in Assembly as early as 1958 by Melvin Conway of Conway's Law fame.

[2] http://www.inf.puc-rio.br/~roberto/docs/MCC15-04.pdf by Roberto Ierusalimschy, the author of Lua.

[3] It's slightly different in an awaitless language like Kotlin - here you actually have to check whether the function you're calling is suspendable or not.

Re:Even in Go, concurrency is still not easy by pansa2 on Thursday September 03, 2020 @05:08AM (#24362161)
> Calling the stackful coroutine model as offered by Go superior to stackless coroutines ("async/await") is highly controversial.

Yes. Preference among programmers for one model or the other appears close to 50/50.

More languages seem to support stackless coroutines than stackful ones, although that could just be because they’re easier to retro-fit to an existing compiler/VM. It’ll be interesting to see which model new languages choose.

Re:Even in Go, concurrency is still not easy by naikrovek on Thursday September 03, 2020 @11:40AM (#24365093)
The coroutine and communication by channels paradigm which Go introduced to me (I recognize that it is a very old paradigm, but it was new to me at the time) is just so very easy for me to reason about.
Re:Even in Go, concurrency is still not easy by unscaled on Friday September 04, 2020 @11:24PM (#24381186)
Communicating through channels is completely orthogonal to the type of coroutines (stackless or stackful). I would argue that Kotlin does channels strictly better than Go, since Kotlin channels do not have any of the problem of Go channels[1]:

1. No blocking forever on nil channels (WTF?) 2. Close is idempotent: No panic when closing a channel twice. Now you don't require 1 or 2 extra channels to measure proper closing of every channel :) 3. Send and receive behavior on closed channel is consistent - a easy-to-handle exception on both cases. Go panics on one case and returns a nil value in the other. 4. You can check the closed status of channel. 5. There are non-blocking versions of send and receive.

I haven't tried Rust channels yet, but I assume they also deal with these issues by having a sane API.

Go was not a pioneer at all here - it just had a Google-level PR to push its "revolutionary" ideas. The concepts of channels is much older than Go. It's not a secret that channels are based on the CSP model by Tony Hoare, and Rob Pike has a long history of researching CSP and integrating CSP constructs into his languages while he was at Bell Labs[2].

Channels are not the possible construct here. I'd argue that earlier languages implemented channel-like constructs in a much safer and saner way: e.g. Erlang's actors or Ada's Rendezvous (which dates all the way bac to 1983!).

[1] https://www.jtolio.com/2016/03/go-channels-are-bad-and-you-s... [2] https://swtch.com/~rsc/thread/

Re:Even in Go, concurrency is still not easy by mathw on Thursday September 03, 2020 @03:23AM (#24361583)
async/await isn't really concurrency. It's syntactic sugar over callback hell, which can be used to write concurrent software in the "spawn something, wait for it to finish later" model. It has absolutely nothing to help you passing data between ongoing threads, accidental unlocked access to shared data etc. etc.
Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @05:16AM (#24362213)
This used to be my assumption long ago, but this is incorrect for almost every implementation async await.

The compiler does not syntactically converts your awaits to callbacks - it converts everything into state machines.

Re:Even in Go, concurrency is still not easy by thelazydogsback on Thursday September 03, 2020 @12:58PM (#24366060)
C# converts to state-machines, F# converts to nested callbacks. (Although this may change, or be optional.)
Re:Even in Go, concurrency is still not easy by unscaled on Friday September 04, 2020 @11:29PM (#24381209)
Interesting. The only other instance I remember of (sort-of) awaits being converted into callbacks are LiveScript "backcalls":

https://livescript.net/#functions-backcalls

Re:Even in Go, concurrency is still not easy by ben0x539 on Thursday September 03, 2020 @02:51AM (#24361432)
I think that's just "easy"/"not easy" from a different baseline, not a comparison of C# vs Go. C# async/await is easy compared to something like callback-based asynchronous IO. Go concurrency is "not easy" compared to serial code.