zesterer / flume

A safe and fast multi-producer, multi-consumer channel.
https://crates.io/crates/flume
Apache License 2.0
2.37k stars 84 forks source link

Memory leak in unbounded channels #146

Open rakbladsvalsen opened 4 months ago

rakbladsvalsen commented 4 months ago

Just as the title suggests, I found out there's a memory leak when consumers process items slowly.

While the channel's queue will eventually be drained by any given number of consumers, the internal VecDeque 's excess capacity will never be freed (because that's how most rust's collections work, they over-allocate in advance to prevent frequent allocations).

This is notorious when you use flume in long-lived tasks/threads: fluctuations (usually network/db) sometimes cause consumers to process items slowly, causing the VecDeque to allocate extra space.

PoC:

use std::{hint::black_box, thread::park, time::Duration};

use flume;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    let (tx, rx) = flume::unbounded();
    let mut cont: u64 = 0;
    tokio::spawn(async move {
        while let Ok(_val) = rx.try_recv() {
            if cont % 1000 == 0 {
                println!("Capacity: {}", rx.len());
                sleep(Duration::from_millis(1)).await;
                // rx.shrink_to_fit();
            }
            cont += 1;
        }
        println!("cont: {cont:?}");
    });
    for _ in 0..1000000u64 {
        let waste = black_box([10u8; 100]);
        tx.try_send(waste).ok();
    }
    park();
}

Note the commented line: there's a method that basically calls the inner queue's shrink_to_fit method to drop excess capacity. I've submitted a PR with shrink_to_fit() and queue_capacity(), respectively :D

rakbladsvalsen commented 3 months ago

@zesterer I submitted a new PR a few weeks ago. This one should be way easier to review: https://github.com/zesterer/flume/pull/148