Skip to content

task::spawn cause memory leak #702

Closed
@gmg137

Description

@gmg137

I have a download function:

pub(crate) async fn download_music(url: &str, path: &str) -> Result<(), surf::Exception> {
    if url.starts_with("http://") || url.starts_with("https://") {
        let music_url = url.replace("https:", "http:");
        let client = surf::Client::new();
        let buffer = client.get(&music_url).recv_bytes().await?;
        if !buffer.is_empty() {
            fs::write(path, &buffer).await?;
        }
    }
    Ok(())
}

Executing with task :: spawn will cause a significant increase in memory usage and will not be released after the download is complete.

    for _ in 0..100 {
        task::spawn(async {
            let url = "https://***.mp3";
            download_music(url, "***.mp3").await.ok();
        });
    }

With task :: block_on there is no memory leak.

    for _ in 0..100 {
        task::block_on(async {
            let url = "https://***.mp3";
            download_music(url, "***.mp3").await.ok();
        });
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions