Uh oh!
There was an error while loading. Please reload this page.
Free tstate on python 3.7+ on finalize_interpreter - #2020
Conversation
269327b to
5eb948eComparebstaletic
commented
Dec 11, 2019
Rebased after travis fixes. Tests are now green. |
Hi @bstaletic, indeed I think that it would be more appropriate to put this into the destructor. Consequently, the PYBIND11 ABI version should be updated. (there is a constant indicating this in one of the header files). |
YannickJadoul
commented
Dec 11, 2019
We'd need to make sure |
bstaletic
commented
Dec 11, 2019
If |
YannickJadoul
commented
Dec 11, 2019
We might get away with it, but it wouldn't be very clean. Also, another reminder: there's an explicit comment saying But actually, are we sure none of the objects That being said, it would be nice to have the |
bstaletic
commented
Dec 11, 2019
Is that allowed? If |
bstaletic
commented
Dec 11, 2019
@wjakob I've updated the pull request. It turns out that using the destructor to clean up |
wjakob
commented
Dec 12, 2019
Ok, looks good to me -- thank you. |
Right now pybind leaks a single pointer worth of memory when embedding the interpreter. This is because, with the new python 3.7 thread C API,
tstatein theinternalsstruct needs to be freed with a call toPyThread_tss_free().This PR frees the block inside the
finalize_interpreter, where all the cleanup seems to be located.An alternative solution is adding
~internals() { PyThread_tss_free(tstate); }, which would make it impossible for pybind to forget to freetstateever again. However this solution has ABI implications. A destructor that isn't implicitly generated or explicitly defaulted makes the type non-trivial, which changes the calling convention rules. I'm fine with moving the code to the destructor if @wjakob thinks that is more appropriate.Finally, whatever soluton we settle on, this only affects python 3.7+.
Ping @YannickJadoul
EDIT: The valgrind reported stack trace of the leak: