Skip to content

Potential memory leak when calling Python from Rust #1801

@pschafhalter

Description

@pschafhalter

🐛 Bug Reports

Repeatedly calling Python from Rust appears to cause a memory leak of ~700 bytes per invocation. This poses a problem for long-lived Rust programs that repeatedly call Python. In my case, I ran into this issue while adding support for Python UDFs to a Rust-based streaming system.

As a side note, thanks for all the hard work on this fantastic library! Hoping this can be resolved, and that I haven't missed something on my end.

🌍 Environment

  • Your operating system and version: Ubuntu 20.04 LTS
  • Your python version: 3.8.10
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: Installed via apt, using a virtualenv.
  • Your Rust version (rustc --version): 1.54.0
  • Your PyO3 version: 0.14.2
  • Have you tried using latest PyO3 main (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: yes

💥 Reproducing

use pyo3::prelude::*;

fn main() {
    loop {
        Python::with_gil(move |py| {
            py.run("1 + 1", None, None).unwrap();
        }); 
    }
}

The following also causes the issue:

use pyo3::prelude::*;

fn main() {
    Python::with_gil(move |py| {
        loop {
            py.run("1 + 1", None, None).unwrap();
        }
    }); 
}

Run with cargo run --release and observe memory use grow.

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