DEV Community

Vee Satayamas
Vee Satayamas

Posted on

The worst thing I did in Rust

The worst thing I did in Rust was using unbounded asynchronous channels. It took days to figure out that my program had backpressure, and channels ate up all RAM.

So, since then, I have always used sync_channel or bounded channels from Crossbeam.

Top comments (3)

Collapse
 
nfrankel profile image
Nicolas Fränkel

Your "post" fits in a Tweet: I don't think it's very useful as it is.

But it has a lot of potential. Writing a post explaining the problems and showing a solution with the relevant code could be much more useful to readers.

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Could you please elaborate on unbounded asynchronous channels? They're not described in The Rust Programming Language, I could only find them mentioned in Rust by Example. And by "backpressure" you mean that the channels were steadily growing...? P.S.: I'm a Rust newbie.

Collapse
 
veer66 profile image
Vee Satayamas • Edited

You are right. I created a channel using the std::sync::mpsc::channel function. So the channel can grow until my computer is out of memory. I knew this before I wrote my code. However, after my program became more complex, there were many assumptions why the program took more than 32GB of memory after running for one week. It took me days to figure out that a producer among many of them sent much more data than a consumer can finish processing in time.

These days, I use a bounded channel from the Crossbeam library instead.

use crossbeam::channel;
//...
//...
//...
const LINE_BUF_SIZE: usize = 0x10;
//...
//...
//...
let (line_tx, line_rx) = channel::bounded(LINE_BUF_SIZE);
Enter fullscreen mode Exit fullscreen mode

A bounded channel will block a producer if the channel reaches the limit, and it will unblock if the channel has some space. So the program won't use up the memory.