News for hackers. Stuff that matters.
Re:Async Rust in Three Parts by keyle on Wednesday October 23, 2024 @08:39PM (#41930738)
My only experience with Rust has been synchronous mostly with little to show for in terms of async. And I liked Rust. When it ran, I was damn sure it was going to run. There is comfort in that: "Tell me what to fix". The clippy stuff etc. was great too.

I read the 3 parts of this website and 'Wowsa'... I'm definitely not going in that direction with Rust. I'll stick to dumb Go code, Swift or Python if I do async heavy stuff.

It's hard enough to write good code, I don't see the point of finding walls to smash my head into.

Think about it, if you write a lot of async code, chances are you have a ton of latency, waiting on I/O, disk, network etc. Using Rust for this in the first place isn't the wisest since performance isn't as important, most of your performance is wasted 'waiting' on things. Besides Rust wants purity and I/O is gritty little dirt.

Sorry my comment turned into a bit of a hot take, feel free to disagree. This async stuff, doesn't look fun at all.

Re:Async Rust in Three Parts by mplanchard on Wednesday October 23, 2024 @11:02PM (#41931512)
Having written a lot of asynchronous code in python and in rust, I’d take Rust any day. If it compiles, it works.

I also don’t think it’s hard to reason about in practice. Tutorials tend to get much deeper into the weeds than you typically need to go.

Re:Async Rust in Three Parts by gpderetta on Thursday October 24, 2024 @04:36AM (#41933415)
asyncio is its own special kind of hell. I understand there are significantly better async libraries for python, but being built-in it is what you end up reaching to.
Re:Async Rust in Three Parts by palata on Thursday October 24, 2024 @09:59AM (#41935585)
What is bad with asyncio? Genuinely interested.
Re:Async Rust in Three Parts by gpderetta on Thursday October 24, 2024 @12:36PM (#41937144)
From the top of my head: lack of structured consistency, error prone , atrocious stack traces, and (and this might be a skill issue on my part) confusing task cancel semantics.
Re:Async Rust in Three Parts by pdimitar on Thursday October 24, 2024 @06:51AM (#41934227)
Maybe Rust has improved in the last 13-14 months but last time I needed to do a lot of async code I ended up with a browser session with at least 12 crates on docs.rs where only a small part of the picture was explained. It was absolutely terrible to track down what's the problem.

In the end, very helpful (and hardcore -- like the main author of Tokio) people unblocked me. I am not sure I was left very enlightened though; but I likely didn't stay for long enough for the whole thing to stick firmly into my memory. It's likely that.

Re:Async Rust in Three Parts by mplanchard on Thursday October 24, 2024 @11:31AM (#41936538)
It has improved a lot, yes. impl in return position in traits makes writing async traits much, much easier, as you can avoid a large number of Boxing and lifetime issues when you don't need a trait object (which is what the async_trait crate does under the hood).

I also think you've really got to be willing to be pragmatic when writing async code. If you like to do functional chains, you've got to be willing to let go of that and go for simple imperative code with match statements.

Where I find it gets complicated is dealing with streams and stuff, but for most application authoring use-cases, you can just await stuff that other people have written, or throw it into a `join_all` or whatever.

Re:Async Rust in Three Parts by samatman on Thursday October 24, 2024 @11:30AM (#41936528)
> If it compiles, it works.

This slogan sucks. If it compiles, it type checks. Yes, Rust has a more sophisticated type system than Python with annotations, so it catches more type errors.

No, the type system cannot prevent logic bugs or design bugs. Stop pretending it does.

Re:Async Rust in Three Parts by mplanchard on Thursday October 24, 2024 @11:35AM (#41936577)
Well, for stuff like "does my async code actually run," or "will the program crash," the slogan is generally true. Or, versus C++, "are all of my references valid, or will my program segfault."

Obviously a type system cannot catch all your logic errors, but you can write code such that more of the logic is encapsulated in types, which _does_ help to catch a lot of logic errors.

There's a strong qualitative difference working with the Rust compiler versus working with Python or C++. Do you have a better suggestion for how to express that?

Re:Async Rust in Three Parts by samatman on Thursday October 24, 2024 @02:07PM (#41937960)
I made the suggestion! "if it compiles, it type checks". Rust's type system is much more sophisticated than that of C++ and Python, and that is the difference you're gesturing at.

Also, no, the Rust compiler will happily pass code which will crash your program, all it takes is an out of bounds array access. That's the kind of puffery many of us are tired of. The "if it compiles, it works" slogan is, bluntly, wrong.

Re:Async Rust in Three Parts by mplanchard on Thursday October 24, 2024 @09:49PM (#41941587)
I mean, if it compiles it typechecks is kind of tautological, and not particularly effective as a saying or slogan. I feel like you miss the point of a phrase like that, which like any aphorism, slogan, saying, or rule of thumb is not about representing reality with 100% accuracy but instead about conveying an idea.

The only two languages I’ve worked with that gave me the feeling that I could generally trust a compiling program to be approximately correct are Rust and Haskell. That difference relative to other languages is meaningful enough in practice that it seems to me to be worth a slogan. I believe it’s meant to be more of a “works, relative to what you might expect from other languages” kind of thing versus, “is a completely perfect program.”

And, if you care about maximizing the “if it compiles it works” feeling, it’s possible to use .get() for array access, to put as much logic in the type system as is feasible, etc. This is probably more idiomatic and is generally how I write code, so it does often feel that way to me, regardless of whether it is completely, objectively, literally true.

Re:Async Rust in Three Parts by samatman on Friday October 25, 2024 @10:24AM (#41945589)
> if it compiles it typechecks is kind of tautological

It's not tautological at all, because the type system in Rust and Haskell is not a trivial condition of the language.

> not particularly effective as a saying or slogan

Neither is "if it compiles it runs", rather less so in fact, everyone is sick of hearing it, and rolls their eyes so hard it's actually audible.

Every one of these 764 bugs compiled and passed type checks:

https://github.com/tokio-rs/tokio/labels/C-bug

Not picking on tokio in particular, mind you, finding and fixing bugs is a sign of quality in a library or program.

> I believe it’s meant to be more of a “works, relative to what you might expect from other languages” kind of thing versus, “is a completely perfect program.”

Which is why I describe it as meaningless puffery. What you're saying here is that you know full well it isn't true, but want to keep saying it anyway. My reply is find a way to express yourself which is true, rather than false. I bet you can figure one out.

Re:Async Rust in Three Parts by kod on Friday October 25, 2024 @11:57AM (#41946496)
> the type system cannot prevent logic bugs or design bugs

^ your words, that statement is false. the type system _can_ prevent logic bugs or design bugs, exhaustive pattern matching is an obvious example.

I bet you can find a way to express yourself which is true, e.g. "the type system cannot prevent _all_ logic bugs or design bugs"

Re:Async Rust in Three Parts by worik on Wednesday October 23, 2024 @10:07PM (#41931208)
> My only experience with Rust has been synchronous

It is a shame that the dominance of the "async/await" paradigm has made us think in terms of "synchronous" or "async/await"

> Think about it, if you write a lot of async code, chances are you have a ton of latency, waiting on I/O, disk, network etc

Yes. For almost all code anyone writes blocking code and threads are perfectly OK

Asynchronous programming is more trouble, but if trying to deal with a lot of access to those high latency resources asynchronous code really shines.

The trouble is that "async/await" is a really bad fit for rust. Every time you say `await` you start invisible magic happening. (A state machine starts spinning I believe in Rust - I may be mistaken)

"No invisible magic" was a promise that Rust made to us. What you say is what you mean, and what you mean is what you get.

No more, if you use async/await Rust

I really do not understand why people who are comfortable with "invisible magic" are not using a language with a garbage collector - that *really* useful invisible magic.

Asynchronous programming is the bees knees. It lets you get so much more from your hardware. I learnt to do it implementing telephone switching systems on MS-DOS. We could run seven telephone lines on a 486, with DOS, in (?) about 1991.

Async/await has so poisoned the well in Rust that many Rust people do not understand there is more to asynchronous programming than that

Re:Async Rust in Three Parts by the__alchemist on Wednesday October 23, 2024 @11:47PM (#41931714)
I notice this as well; there is a false dichotomy of "Async/Await" or "blocking". I see this in embedded too. I think a lot of rust embedded programmers learned on Embassy, and to them, not using Async means blocking.
Re:Async Rust in Three Parts by zavec on Wednesday October 23, 2024 @11:57PM (#41931766)
Is the alternative the more traditional spawning threads and using channels, or is there another paradigm? That's definitely something I'd be interested in learning more about.
Re:Async Rust in Three Parts by onjectic on Thursday October 24, 2024 @02:10AM (#41932426)
I think they mean that there is more than one asynchronous paradigm. Actors is one alternative I can think of.
Re:Async Rust in Three Parts by palata on Thursday October 24, 2024 @03:58AM (#41933172)
Where an actor has its own thread and communicates with a channel, right?
Re:Async Rust in Three Parts by pdimitar on Thursday October 24, 2024 @07:00AM (#41934286)
Not necessarily, a lot of actors can be sharing the same OS thread and be preemptively switched away from when they hit a certain limit of instructions or start an I/O call. Erlang demonstrates this very well because it works there without a glitch 99.99% of the time (and the remaining 0.01% are because of native code that does not yield control).
Re:Async Rust in Three Parts by GolDDranks on Thursday October 24, 2024 @04:37AM (#41933422)
There is also readyness-based polling in a loop, without the async/await sugar.
Re:Async Rust in Three Parts by omani on Wednesday October 23, 2024 @11:53PM (#41931745)
can you elaborate on this?
Re:Async Rust in Three Parts by the__alchemist on Thursday October 24, 2024 @08:55AM (#41935028)
Writing programs that perform multiple tasks asynchronously can be done in many ways. It is a broad and important tool in software, and the Async/Await language is only one way to do it. Examples:

  - Multiple cores
  - DMA or other dedicated hardware
  - GPU programming
  - Distributed systems (e.g. the CAN network in your car)
  - Threads
  - Interrupts
  - Event loops
  - Coroutines
Re:Async Rust in Three Parts by _flux on Thursday October 24, 2024 @04:47AM (#41933500)
However, if you need a state machine, async/await is a super-elegant way to express it. No other language provides as nice way to do it.

I believe best you can do in other languages is using continuations as the state.

Re:Async Rust in Three Parts by worik on Friday October 25, 2024 @05:03PM (#41949741)
State machines are the easy part.
Re:Async Rust in Three Parts by metaltyphoon on Saturday October 26, 2024 @10:07AM (#41954800)
C# async transform into a state machine too.
Re:Async Rust in Three Parts by rcxdude on Thursday October 24, 2024 @04:54AM (#41933551)
>very time you say `await` you start invisible magic happening. (A state machine starts spinning I believe in Rust - I may be mistaken)

It's more that an async function in Rust is compiled completely differently: it's turned into a state machine at that point, with the code between 'awaits' being the transitions. In and of itself, it's not actually particularly difficult to grok (I'd say you have about as much an idea of what the resulting machine code looks like as with an optimized non-async function), the headaches are all in the edges of what the language can currently support when compiling under this model.

Re:Async Rust in Three Parts by kod on Thursday October 24, 2024 @11:21AM (#41936400)
> "No invisible magic" was a promise that Rust made to us.

Honest question, where did you get that promise from?

The 1.0 release didn't really emphasize that: https://blog.rust-lang.org/2015/05/15/Rust-1.0.html

The current rhetoric is more about empowering more people to have confidence in systems programming: https://doc.rust-lang.org/book/foreword.html

Some of graydon's ideas starting almost a decade before 1.0 might have included that https://github.com/graydon/rust-prehistory/blob/df8cc964772b...

but his recent posts on what he would have done differently if he was BDFL include a bunch of stuff that's arguably more magical, not less: https://graydon2.dreamwidth.org/307291.html

Re:Async Rust in Three Parts by zavec on Thursday October 24, 2024 @12:21PM (#41936988)
No invisible magic sounds more like something I've read about zig, maybe they were confusing it with that?
Re:Async Rust in Three Parts by pkolaczk on Thursday October 24, 2024 @02:41AM (#41932620)
Performance is not only wall clock time. With high latency, I/O bound tasks, the cost will be often determined by how much memory you need. And in the cloud, you can’t pay for memory alone. The more memory you need, the more vcores you have to reserve. You might end up in a situation your cpus are mostly idling, but you can’t use fewer cores because of RAM needed to keep the state of thousands of concurrent sessions. In this case Rust async can bring a tremendous advantage over other languages, particularly Java and Go.
Re:Async Rust in Three Parts by palata on Thursday October 24, 2024 @03:55AM (#41933142)
> you can’t use fewer cores because of RAM needed to keep the state of thousands of concurrent sessions. In this case Rust async can bring a tremendous advantage over other languages, particularly Java and Go.

Can you elaborate on that? What about green threads?

Re:Async Rust in Three Parts by vlovich123 on Thursday October 24, 2024 @05:15AM (#41933667)
Green threads can use more memory than async if you have a lot of I/O operations in flight since the stack is not precisely sized at pause points. Similarly, switching in new stacks is a bit more expensive than continuing a paused async task. Implementation details matter of course.
Re:Async Rust in Three Parts by palata on Thursday October 24, 2024 @09:58AM (#41935574)
Right, that's interesting. Still curious to know if that is "slightly better" or if that qualifies for the "tremendous advantage" mentioned above!
Re:Async Rust in Three Parts by vlovich123 on Thursday October 24, 2024 @11:45AM (#41936689)
It’s hard to say. If you want to use green threads, you may want to look at the may crate [1]. The reason Rust prioritized stackless async just like C++ did is that it fits better the systems programming needs (i.e. it’s syntactic sugar for a state machine you could hand implement) while not preventing things like stackful coroutines.

If Rust manages to solve the coloring problem of async (e.g. by adopting effect systems [2] or alternatives), then stackful and stackless coroutines syntactic sugar could conceivably exist within the std language (perhaps leaving out stackless on nostd).

The reason you don’t see both stackless and stackful coroutines in a single language like Rust is the coloring problem is made 50% worse.

[1] https://crates.io/crates/may

[2] https://blog.yoshuawuyts.com/extending-rusts-effect-system/

Re:Async Rust in Three Parts by steveklabnik on Thursday October 24, 2024 @11:48AM (#41936717)
Note that May has soundness issues that the authors handwave away. You can get UB in safe code while using it.
Re:Async Rust in Three Parts by vlovich123 on Thursday October 24, 2024 @01:35PM (#41937663)
You mean the TLS issue called out or something else?

I wasn't trying to recommend may specifically of course. Or are you saying that stackful coroutines must have soundness issues due to missing language features to make it safe?

Re:Async Rust in Three Parts by steveklabnik on Thursday October 24, 2024 @04:08PM (#41939220)
The TLS thing, yeah.

I am unsure if it's inherent to stackful coroutines or not, it's been a minute since I've dug into that.

Re:Async Rust in Three Parts by vlovich123 on Friday October 25, 2024 @02:43PM (#41948306)
Yeah, for TLS to work safely I suspect the only way would be language support which knows to media TLS through a "context pointer" so that the green threads could restore the context when resuming. In C++ the story is even worse because someone could take the address of a TLS variable & use it elsewhere. I think in general it's very very tricky to mix TLS and stackful coroutines that can be scheduled on arbitrary threads and languages like Go pick the stackful coroutines and drop the ability to do TLS.

To be fair though, I think people generally just avoid TLS when running with green thread systems.

Re:Async Rust in Three Parts by tcfhgj on Thursday October 24, 2024 @03:37AM (#41933015)
Performance isn't wasted. More waiting and less CPU time also means longer battery life / energy efficiency.
Re:Async Rust in Three Parts by DeathArrow on Thursday October 24, 2024 @03:44AM (#41933063)
>Think about it, if you write a lot of async code, chances are you have a ton of latency, waiting on I/O, disk, network etc. Using Rust for this in the first place isn't the wisest since performance isn't as important, most of your performance is wasted 'waiting' on things. Besides Rust wants purity and I/O is gritty little dirt.

But isn't most code going to perform some I/O at some time? Whether is calling an API, writing on disk or writing to a DB?

Re:Async Rust in Three Parts by pdimitar on Thursday October 24, 2024 @06:56AM (#41934260)
I am mostly in agreement -- I moved to Golang for not-super-complex async programs because I value my time and day-to-day productivity more than the super strictness of Rust (which I love and I wish more languages had it, though in a more approachable manner).

Rust was a legendary pain to untangle when learning to do async, though as I admitted in a comment down-thread this was also because I didn't stay for long enough for everything to cement itself in my head. It was still an absolute hell to get into. I needed help from Tokio's author to have some pieces of code even compile because I couldn't for the life of me understand why they didn't.

...BUT, with that being said, Rust has a much smaller memory footprint and that is an actual and measurable advantage on cloud deployments. It could be painful to make it compile and run but then it'll give you your money's worth and then some. So it's worth even only for that (and "that" is a lot!), if you are optimizing for those values. I plan to do that in the future. In the meantime Golang is an amazing compromise between productivity and machine performance.