pyo3: segmentation fault second run of rust function in python pyo3 0.21.2
Hi. I’m trying to apply pyo3 0.21.2 to 0.20 codes.
With 0.20 codes my python app works, but with the new code does not.
The current situation:
- python version = 3.9.15
- the first call of
rust_functionin python succeeds, but the second call fails. - The error was segmentation fault.
- The error messages are slightly different per run:
vk[d].setdefault(a, dict())[p_key] = sound_data
KeyError: 'chr11:871499/2' // <- this string can't be one of p_key, a or d. The string like that comes from another variable.
Exception ignored deletion of interned string failed:
Traceback (most recent call last):
File "python_project.py", line 333, in rust_function
self.rust_have_been_failed = True
KeyError: 'À\xad\x8dU' // <- A value that shouldn't exist if it's working properly.
Thread 1 "python" received signal SIGSEGV, Segmentation fault.
--Type <RET> for more, q to quit, c to continue without paging--
pymalloc_free (ctx=0x0, p=0x1)
at /usr/local/src/conda/python-3.9.15/Objects/obmalloc.c:1866
1866 /usr/local/src/conda/python-3.9.15/Objects/obmalloc.c: No such file or directory.
/usr/local/src/conda/python-3.9.15/Objects/object.c: No such file or directory.
- returning
(String, i32)instead of(Py<PyString>, i32)does not raise the error.
The codes here: https://gist.github.com/Crispy13/03d25f8d556e644b5425ddf1decff6dc
The rust function get input string from python, and return it back with additional i32.
Is it me? What’s the cause of the error?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 25 (19 by maintainers)
I see two problems: We do not control all reference count updates as some will have through Python/C code instead of our code paths. And this would undo the performance gains on which you worked so hard as part of the bound API.
I agree that we should not try to defer reference count updates. I think that the convenience is warranted though as user code cannot do something else but call
with_gilto then callclone_ref. This has known deadlock issues, so if we remove theClone, we should probably at least provide atry_with_gilso that user code can emulate its current mode of operation.Considering the loopholes, its performance impacts and the possibility of solving this problem via a cooperating GILless Python interpreter, I fear we need to drop the reference pool unconditionally.
I think we might still want to keep the
Cloneimpl though because we cannot remove theDropimpl, so we have to decide on what to do about the reference count updates without the GIL in any case, i.e. finish our discussion in #4095 albeit under different circumstances.(Funny coincidence to get this bug report after discussing what to do about the reference pool. But then again, with the GIL pool on the way out, the reference pool is the last remnant of the “global state” memory management style we seem to be moving away from.)
I don’t think this will work as we cannot rely on getting these updates in pairs, e.g. the following reduced test case
will fail with
because we apply the decrement before we had any chance to actually look at the increment.
I think we never ran into this because the common pattern is to wrap such sections in
allow_threadswhich works around this. But my conclusion here would be that to provide the service that the global reference pool provides, we could actually “update the counts” before each and every single reference count operation. (And I am not even sure if this means, operation by us viaPyor any reference count update.)