How does mpsc's "channel" have an "infinite" buffer? How is OS allowing this?

I'm in the process of getting used to Rust's asynchronous programming model and seeking some explanation to better my understanding.

Looks like mpsc provides channel and in its documentation, it says it would have an "unlimited" buffer.

...All data sent on the [Sender] will become available on the [Receiver] inthe same order as it was sent, and no [send] will block the calling thread(this channel has an "infinite buffer", unlike [sync_channel], which will block after its buffer limit is reached)...

How is this possible in terms of OS's resource allocation? Let's say when the program keeps getting dominantly outnumbering sender.send() calls than receiver.recv(), would this "unlimited buffer" somehow magically remember all those send requests to be later (the exact time never known) processed by the consumers?

Or would Rust just dismiss send()s once it reaches the device' capacity even though it didn't ask for initializing a "fixed" size of queue (of calls) ?

I think it's "unlimited" (not infinite, only "infinite") just the same way a Vec is unlimited - I didn't look into the mpsc code. You can push elements to the vector without expecting any errors to be returned - but at some point you might actually run out of memory - handled by abort or panic etc.

If there is no evidence of it in the API, the unlimited channel is probably not handling out of memory events (but it could do so in the future?)

Edit: I think the relevant code ends up in Queue::push here - source and it allocates nodes in a linked list, but reusing nodes if possible. Allocated using box <expr> (i.e like Box::new) in the current implementation.


Rust will generally abort the process if you run out of memory.


The buffer is unlimited as much as your computer is an ideal Turing machine with an infinite tape.


I checked the portion of the code you mentioned. That "queue" looks like it might theoretically continue to attach a new node to its tail but in the end, when the machine cannot create a new "node" because of resource depletion, the process would die as I guess..

Thanks for the lead!

Yeap, in other words, when that tape in reality only has 16GB, 1.6 * 10^9 * 8 slots to be punched, the remaining operation will be discarded.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.