Skip to content

Possible Memory Leak with tokio::TcpStream #2

@sampaioletti

Description

@sampaioletti

Thanks for the work you've done on bringing tokio to the esp I think its a great scenario, its the closest we have been to successfully sharing our codebase from other platforms.

I'm trying to figure out how to narrow this down but tokio tcp streams seem to cause a memory leak of around 250b that I have been unable to duplicate on other platforms. It appears to be when a connection never happens.

// this section leaks on the heap
loop {
      log::info!(
          "Stack High Water Mark {} Heap {}",
          unsafe { esp_idf_sys::uxTaskGetStackHighWaterMark2(std::ptr::null_mut()) },//no leak
          unsafe {
              esp_idf_sys::heap_caps_get_free_size(esp_idf_sys::MALLOC_CAP_8BIT) //leaks about 250b
          }
      );
      let conn = tokio::net::TcpStream::connect("192.168.1.10:12345").await; //this endpoint doesn't exist so connection will fail
  
      match conn{
          Ok(_) => {}
          Err(_) => {}
      }
      tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
  }

// this non async version does not
loop {
    log::info!(
        "Stack High Water Mark {} Heap {}",
        unsafe {
            esp_idf_sys::uxTaskGetStackHighWaterMark2(std::ptr::null_mut())
        },
        unsafe {
            esp_idf_sys::heap_caps_get_free_size(esp_idf_sys::MALLOC_CAP_8BIT)
        } //stays consistent
    );

    let conn = std::net::TcpStream::connect("192.168.1.10:12345"); //this endpoint doesn't exist so connection will fail

    match conn {
        Ok(_) => {}
        Err(_) => {}
    }
    std::thread::sleep(std::time::Duration::from_secs(5));
}

I've tried playing with time to see if the connections weren't being released, but it never stabilized it will decrease until it runs out of memory, and the sync version stabilizes after the first allocation.

Any thoughts on what might cause this, or how i could figure out what might be causing it. I'm not a esp expert (yet) so i'm still trying to learn the tooling.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions