Skip to content
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

[BUG] Crash due to duplicate __PYX_XCLEAR_MEMVIEW #5571

Closed
fanosta opened this issue Jul 27, 2023 · 2 comments · Fixed by #5574
Closed

[BUG] Crash due to duplicate __PYX_XCLEAR_MEMVIEW #5571

fanosta opened this issue Jul 27, 2023 · 2 comments · Fixed by #5574

Comments

@fanosta
Copy link

fanosta commented Jul 27, 2023

Describe the bug

When using a property setter that accepts a memory view, Python crashes with the following error:

Fatal Python error: __pyx_fatalerror: Acquisition count is -1 (line 16959)
Python runtime state: initialized

Current thread 0x00000001e83a1e00 (most recent call first):
  File "/private/tmp/cdtmp.l8Y/test.py", line 7 in <module>

Extension modules: cython.cimports.libc.math, foo (total: 2)

Code to reproduce the behaviour:

# foo.pyx
cdef class Foo:
    @property
    def bar(self):
        return b'bar'

    @bar.setter
    def bar(self, const unsigned char[::1] bar):
        pass
# test.py
#!/usr/bin/env python3

from pyximport import install; install()
from foo import Foo

c = Foo()
c.bar = b'some string'  # <-- this crashes

In the generated foo.c, I can see the following code where __PYX_XCLEAR_MEMVIEW is called twice on the same variable which is likely the cause of the bug:

/* Python wrapper */
static int __pyx_pw_3foo_3Foo_3bar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_arg_bar); /*proto*/
static int __pyx_pw_3foo_3Foo_3bar_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_arg_bar) {
  /* ... */

  /* function exit code */
  __PYX_XCLEAR_MEMVIEW(&__pyx_v_bar, 1);
  __PYX_XCLEAR_MEMVIEW(&__pyx_v_bar, 1);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

Expected behaviour

The code is executed without crashing the Python interpreter.

OS

macOS

Python version

Python 3.11.4

Cython version

Cython version 3.0.0

Additional context

I can also reproduce the error on Debian 11 with Cython 3.0.0 and Python 3.9.2

@da-woods
Copy link
Contributor

So the double __PYX_XCLEAR_MEMVIEW isn't right, but it should be safe. The crash is actually on the first one

da-woods added a commit to da-woods/cython that referenced this issue Jul 28, 2023
Fixes cython#5571.

I believe this is to any function with a special signature
(essentially most dunder methods of cdef classes). The specific
report was about properties though.
da-woods added a commit to da-woods/cython that referenced this issue Jul 28, 2023
Fixes cython#5571.

I believe this is to any function with a special signature
(essentially most dunder methods of cdef classes). The specific
report was about properties though.
@da-woods da-woods added this to the 3.0.1 milestone Jul 30, 2023
@fanosta
Copy link
Author

fanosta commented Jul 31, 2023

Thank you for debugging the code and creating a patch.

scoder pushed a commit that referenced this issue Aug 21, 2023
…H-5574)

It largely just aligns the reference counting of memoryviews and PyObjects for these functions.

I believe this is to any function with a special signature (essentially most dunder methods of cdef classes).
The specific report was about properties though.

Closes #5571
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants