News for hackers. Stuff that matters.
Re:Why asynchronous Rust doesn't work by xiphias2 on Tuesday March 09, 2021 @09:49PM (#26407186)
As I read through the database example, I saw that the compiler just caught a multi-threading bug for the author, and instead of being thankful, he’s complaining that Rust is bad.

I think he should use a higher level framework, or wait a few years for them to mature, and use a garbage collected language until then.

Re:Why asynchronous Rust doesn't work by Deukhoofd on Wednesday March 10, 2021 @03:58AM (#26409286)
He's just pointing out that it isn't very ergonomic, not that Rust is bad (in fact he states the opposite multiple times).

Pointing out weaknesses and things that might be done better is what helps something mature, not just praising it.

Re:Why asynchronous Rust doesn't work by filleduchaos on Wednesday March 10, 2021 @05:14AM (#26409729)
"Ergonomic" is such a nebulous word as to be nearly useless honestly.

I don't see how it is [unduly] inefficient or uncomfortable for a language at Rust's level to ensure that the programmer actually thinks through the execution of the code they are writing. If one doesn't want to think about such things - and there's absolutely nothing wrong with that! - there are plenty of higher level languages that can do that legwork for them with more than good enough performance.

To me it feels a bit like pointing out that the process of baking a cake from scratch isn't very ergonomic, what with all the careful choosing and measuring of ingredients and combining them in the right order using different techniques (whisking, folding, creaming, etc). That is simply what goes into creating baked goods. If that process doesn't work for you, you can buy cake mix instead and save yourself quite a bit of time and energy - but that doesn't necessarily mean there's anything to be done better about the process of making a cake from scratch.

Re:Why asynchronous Rust doesn't work by skohan on Wednesday March 10, 2021 @07:52AM (#26410709)
I actually find ergonomic to be a very clear and intuitive term as it refers to programming language design decisions.

And to use your cake analogy, the fact that a process is complex or laborious is orthogonal to the degree to which it is ergonomic. You could imagine baking a cake in a well organized kitchen with well designed, comfortable tools, or you could imagine having to stand on your toes and reach to the back of a slightly-too-high cupboard every time you need to fetch an ingredient, and having to use a mixer with way too many settings you can never remember, and none that does exactly what you want. I think this is a better analogy for ergonomics in programming languages.

Re:Why asynchronous Rust doesn't work by filleduchaos on Wednesday March 10, 2021 @10:04AM (#26411912)
> I think this is a better analogy for ergonomics in programming languages.

While it may be a better analogy, it doesn't really reflect the way the term is used. In my (admittedly anecdotal) experience, most people's complaints about ergonomics are more of expecting difficult things to be easier than they inherently are than of the actual quality of the tools they are presented with. People think they are complaining about a mixer with too many settings, when in reality what they are complaining about is the fact that there are several variables that go into mixing batter and dough. They buy a mixer that's targeted at a baker or chef who wants complete control over how their batter comes out or who even needs a mixer versatile enough to also mill grain or roll pasta, then are predictably lost because it's not as immediately intuitive to use as a simple hand mixer. I don't think that makes the mixer not ergonomic, I think it just makes it not the right tool for that particular person.

Re:Why asynchronous Rust doesn't work by skohan on Wednesday March 10, 2021 @04:27PM (#26416491)
Or maybe it's a poorly designed mixer, or it's trying to be a mixer and a blender at the same time and not doing a good job at either task.

Sometimes complexity is a necessary consequence of the domain, and sometimes it's simply the result of poor design.

Re:Why asynchronous Rust doesn't work by jnwatson on Wednesday March 10, 2021 @08:36AM (#26411033)
The whole point of async is entirely ergonomics. Anything you can do in async you can do in continuation-passing style.

I believe the point the author is making is that they added a feature for ergonomics' sake that ended up not being ergonomic; async-style programming usually coincides with first-class functions and closures, and those are painful in Rust.

Re:Why asynchronous Rust doesn't work by filleduchaos on Wednesday March 10, 2021 @09:12AM (#26411391)
> async-style programming usually coincides with first-class functions and closures, and those are painful in Rust.

Are they, though?

The example the author gives in the article tries to write a multithreaded program as if it is a singlethreaded one. Of course that is going to be painful, whether you're trying to use futures/promises or you're using callbacks. If you want to use multiple threads safely (i.e. at all) you need to use the right structures for the job - smart pointers, mutexes, etc. This is independent of language really, even dynamically typed languages like Ruby still have mutexes for when you want to use multiple threads (though they do take control away from the programmer in the form of things like global interpreter locks).

If you don't actually want to deal with the complexities of multithreading, then don't use it. For example there are a good number of languages/runtimes that only expose a single-threaded event loop to the programmer (even though they may use thread pools in the background). But I don't think "ergonomic" should necessarily mean "abstract away every single detail" or "things should Just Work even though one is straight-up not approaching the problem correctly".

Re:Why asynchronous Rust doesn't work by throwaway17_17 on Wednesday March 10, 2021 @12:55PM (#26414014)
It is my understanding that the entire point of the async/await sugar in any language is to explicitly take away the appearance of multithreading/concurrency/parallelism in code. I often see the argument for async/await syntax as being made in the following way, it is not natural or feasible to reason about concurrent and multithreaded code, programmers need to be able to reason about all code as though it were a linear list of instructions no matter what is going on with the implementation, therefore, we need the async/await syntax to enable linear, straight line code appearance for programmer benefit. While I do not agree that doing so is at all a good thing, it is the most prevelant argument I have seen in justifying async/await style sugar. If I am right and this is the primary motivation for adding this to a language, your comment is in direct opposition to the entire point of such constructs. Using this reasoning, I think the author was doing as suggested and writing multithreaded code as though it was just a non threaded imperative program.

Note: I don’t really disagree with your view, but it seems that view is in the minority.

Re:Why asynchronous Rust doesn't work by marshray on Wednesday March 10, 2021 @05:48PM (#26417487)
Nothing about Rust async depends on closures or functions being first-class. It's literally just a state machine transformation.

But now that state data that previously lived on a nice last-in-first-out stack has a different lifetime. Since Rust fastidiously encodes lifetimes in the type system, using async can result in having more complicated types to talk about.

To me it's just the price that must be paid to use a language that doesn't constantly spill garbage everywhere, necessitating collection.

Re:Why asynchronous Rust doesn't work by EugeneOZ on Wednesday March 10, 2021 @03:43AM (#26409191)
You are to find fancy runtime-only errors in async functions. Their existence outweighs any other compiler’s benefits.
Re:Why asynchronous Rust doesn't work by joubert on Tuesday March 09, 2021 @10:12PM (#26407326)
> caught a multi-threading bug

The compiler complained: “error[E0308]: mismatched types”

Re:Why asynchronous Rust doesn't work by xiphias2 on Tuesday March 09, 2021 @10:20PM (#26407387)
That’s why it’s amazing, type safe efficient multi-threading without dynamic memory allocation with a nice syntax...it’s the holy grail of server programming.
Re:Why asynchronous Rust doesn't work by andrewflnr on Tuesday March 09, 2021 @10:39PM (#26407486)
It would be nice if it was explicitly a borrowing-related message. That's the kind of thing I can see happening in a future rustc actually.
Re:Why asynchronous Rust doesn't work by xiphias2 on Tuesday March 09, 2021 @11:03PM (#26407615)
Yeah, the good thing is that the devs are always happy to take feedback on improving the error messages (like explaining differences between Fn/FnMut/fn here)
Re:Why asynchronous Rust doesn't work by thu2111 on Wednesday March 10, 2021 @05:20AM (#26409761)
Why would I want to write a server in a language that requires such awkward approaches to basics like coroutines and dynamic dispatch when I could use kotlin on the JVM and get pauseless GC, ultra fast edit/compile/run cycles, efficient and powerful coroutines, and eventually Loom which will eliminate the whole problem of coloured functions completely and let me just forget about coroutines? Multi threading bugs are not so common in real Java/Kotlin programs to justify this epic set of costs, in my view.
Re:Why asynchronous Rust doesn't work by karlmdavis on Wednesday March 10, 2021 @09:11AM (#26411374)
Have you ever tried writing the same application on both a JVM language and in Rust, and then measuring the latency & throughput differentials? Blew my mind the first time.

Of course, if speed isn’t a concern for you, then please carry on.

Re:Why asynchronous Rust doesn't work by roca on Wednesday March 10, 2021 @07:52AM (#26410706)
GC always forces you into a tradeoff between pause time, throughput, and memory overhead.
Re:Why asynchronous Rust doesn't work by xiphias2 on Wednesday March 10, 2021 @11:00AM (#26412606)
Good luck getting Google's ad backend with 20ms latency budget and billions of dollars of revenue approved with the JVM.

Therr are many tasks where what you suggests is just too slow and unpredictable.

Don't get me wrong, I think JVM is great, it's just not systems level programming.

Re:Why asynchronous Rust doesn't work by thu2111 on Wednesday March 10, 2021 @11:24AM (#26412895)
Modern JVM GCs have pause times below 1msec. That's a new capability though so most people aren't yet aware of it.

And Google's ads backend are hardly the definition of server. Their ads front-end for example always used to be in Java. Not sure what it is these days

Re:Why asynchronous Rust doesn't work by ha_jo_331 on Wednesday March 10, 2021 @06:40AM (#26410223)
Or even better: Go
Re:Why asynchronous Rust doesn't work by literallyWTF on Tuesday March 09, 2021 @10:25PM (#26407411)
Yeah I don’t get it either. We’re supposed to be upset that it did it’s job? Lol?
Re:Why asynchronous Rust doesn't work by agumonkey on Wednesday March 10, 2021 @01:26PM (#26414322)
I believe haskellers would love (and maybe did) to encode commutativity and thread-safety in the type system :)
Re:Why asynchronous Rust doesn't work by redis_mlc on Tuesday March 09, 2021 @10:21PM (#26407397)
Everything is "mismatched types" in Rust, literally it doesn't do any automatic type conversion (casting), so it's not the right language for most people.
Re:Why asynchronous Rust doesn't work by gohbgl on Wednesday March 10, 2021 @06:48AM (#26410275)
What is the bug? I don't see it.
Re:Why asynchronous Rust doesn't work by Cyph0n on Wednesday March 10, 2021 @12:53PM (#26413999)
The bug is that the closure is mutating the same data structure (a Vec in this case) in a different thread - i.e., a data race.

In this specific case, only the spawned thread is mutating the Vec, but the Rust compiler is usually conservative, so it marked this as a bug.

The actual bug is one or both of the following:

1. One of the threads could cause the underlying Vec to reallocate/resize while other threads are accessing it.

2. One of the threads could drop (free) the Vec while other threads are using it.

In Rust, only one thread can “own” a data structure. This is enforced through the Send trait (edit: this is probably wrong, will defer to a Rust expert here).

In addition, you cannot share a mutable reference (pointer) to the same data across threads without synchronization. This is enforced through the Sync trait.

There are two common solutions here:

1. Clone the Vec and pass that to the thread. In other words, each thread gets its own copy of the data.

2. Wrap the Vec in a Mutex and a Arc - your type becomes a Arc<Mutex<Vec<String>>>. You can then clone() the data and pass it to the new thread. Under the hood, this maps to an atomic increment instead of a deep clone of the underlying data like in (1).

The Mutex implements the Sync trait, which allows multiple threads to mutate the data. The Arc (atomic ref count) allows the compiler to guarantee that the Vec is dropped (freed) exactly once.

Re:Why asynchronous Rust doesn't work by fulafel on Tuesday March 09, 2021 @10:34PM (#26407455)
There wasn't any multi-threading in that example I think.
Re:Why asynchronous Rust doesn't work by Cyph0n on Tuesday March 09, 2021 @10:52PM (#26407556)
Yes, there was: the function was calling the passed in closure from a different thread.

It is just one thread that’s actually appending to the Vec, but it’s still a multi-threaded example.

Re:Why asynchronous Rust doesn't work by fulafel on Wednesday March 10, 2021 @02:43AM (#26408851)
Are we talking about the same example?

  struct Database {
    data: Vec<i32>
  }
  impl Database {
     fn store(&mut self, data: i32) {
         self.data.push(data);
     }
  }

  fn main() {
    let mut db = Database { data: vec![] };
    do_work_and_then(|meaning_of_life| {
        println!("oh man, I found it: {}", meaning_of_life);
        db.store(meaning_of_life);
    });
    // I'd read from `db` here if I really were making a web server.
    // But that's beside the point, so I'm not going to.
    // (also `db` would have to be wrapped in an `Arc<Mutex<T>>`)
    thread::sleep_ms(2000);
  }
No threads are spawned.
Re:Why asynchronous Rust doesn't work by valand on Wednesday March 10, 2021 @03:02AM (#26408947)
`

    fn do_work_and_then(func: fn(i32)) {
        thread::spawn(move || {
            // Figuring out the meaning of life...
            thread::sleep_ms(1000); // gee, this takes time to do...
            // ah, that's it!
            let result: i32 = 42;
            // let's call the `func` and tell it the good news...
            func(result)
        });
    }
`

you missed a snippet, a thread is spawned.

edit: formatting

Re:Why asynchronous Rust doesn't work by imtringued on Wednesday March 10, 2021 @04:38AM (#26409517)
If for some magic reason thread::sleep_ms(1000) takes longer than 2000ms the main function would reach its end and deallocate the closure that is about to get called. Basically use after free.
Re:Why asynchronous Rust doesn't work by richardwhiuk on Wednesday March 10, 2021 @06:41AM (#26410228)
And that's possible, because the sleep call is a suggestion, not a guarantee. If the CPU is blocked for 2s, then the order of waking the threads is undetermined, and the race will occur.
Re:Why asynchronous Rust doesn't work by mav3rick on Wednesday March 10, 2021 @11:49AM (#26413248)
That is technically a race (sure) but at that point the program is getting killed anyway. Agree on the overall point though. I am a big fan of moving complete ownership / lifetime. It just makes things easier to reason about. In Chromium weak pointers are used to solve this use after free problem.
Re:Why asynchronous Rust doesn't work by filleduchaos on Wednesday March 10, 2021 @03:05AM (#26408963)
The `do_work_and_then` function does spawn a thread, it's one of the first things established in the article.
Re:Why asynchronous Rust doesn't work by fulafel on Wednesday March 10, 2021 @10:12AM (#26412014)
Ah, I thought it just named a block, my mistake.
Re:Why asynchronous Rust doesn't work by mav3rick on Tuesday March 09, 2021 @11:21PM (#26407730)
You created a DB on one thread and accessed / modified it on another.