-
Notifications
You must be signed in to change notification settings - Fork 804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apparent memory leak when calling python from rust #1547
Comments
I cannot reproduce this bug with Python 3.7.10 / pyo3 0.13.2 / rustc 1.53.0-nightly (07e0e2ec2 2021-03-24). I also tried with your specific rustc nightly and that did not reproduce either. I used only code from the issue (except for a Are you sure you've minimized your test case properly? |
One possible difference is that the entry point to my program is python.
Tomorrow I'll put together and end to end repro and test a rust entry point.
…On Sun, Apr 11, 2021, 2:09 AM Georg Brandl ***@***.***> wrote:
I cannot reproduce this bug with Python 3.7.10 / pyo3 0.13.2 / rustc
1.53.0-nightly (07e0e2ec2 2021-03-24). I also tried with your specific
rustc nightly and that did not reproduce either.
I used only code from the issue (except for a fn main() that just calls
leaks() in a a loop).
Are you sure you've minimized your test case properly?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1547 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAPCTEABIXHFHGMJ7LKNPBLTIFRMRANCNFSM42NV2MGQ>
.
|
If your entry point is Python then #1556 sounds very similar. The repro would be very helpful, thank you! |
Ok, I think #1556 does explain the issue. Ultimately it comes down to an oversight on my part (that a What was happening was, I was acquiring the GIL on crossing from python into rust, then inside the rust calling back into python in a loop. In each pass of the loop I was apparently acquiring and dropping the GIL, but those were no-ops because it was already acquired and therefor the reference counts were not decremented (because the GIL and its pool were still held) causing the apparent leak. Adding I will say, it's a bit of a gotcha that reference counts aren't decremented until the GIL is properly dropped even if it appears dropped locally, and also that a |
You're right, it is definitely unexpected. The guide kind-of mentions it in "Advanced topics", but it is far from obvious. |
Nature of the bug: Values returned from python functions called from rust are not freed (refcount goes up but does not come down).
🌍 Environment
rustc --version
): rustc 1.50.0-nightly (349b3b324 2020-11-29)version = "0.x.y"
withgit = "https://github.com/PyO3/pyo3")?
: no. I have rust-numpy in my dependencies and it turns on default-features includingauto-initialize
which prevents me from compiling.💥 Reproducing
python code:
rust code:
Calling
leaks
in a loop will grow memory usage unboundedly (the values I'm working with are much larger than empty objects so this becomes a problem very quickly). This example does nothing with the returned values because I found it wasn't necessary to reproduce the leak, but it also leaks if you use the value for something (e.g. return some property of it).this formulation also leaks.
this too, along with a few other variations I tried just in case.
a python variation that prints the refcount before and after the rust call (requires that you call leaks multiple times)
The text was updated successfully, but these errors were encountered: