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

Crash when bounds of prange() are Python objects #2504

Open
scoder opened this issue Jul 18, 2018 · 2 comments
Open

Crash when bounds of prange() are Python objects #2504

scoder opened this issue Jul 18, 2018 · 2 comments

Comments

@scoder
Copy link
Contributor

scoder commented Jul 18, 2018

The following construct crashes:

cdef unsigned long i
cdef double[:] data = ...
for i in prange(len(data), nogil=True):
    ...

The generated C code is as follows:

      #ifdef WITH_THREAD
      PyThreadState *_save;
      Py_UNBLOCK_THREADS
      __Pyx_FastGIL_Remember();
      #endif
      /*try:*/ {
        if (unlikely(!__pyx_v_data.memview)) { __Pyx_RaiseUnboundMemoryviewSliceNogil("data"); __PYX_ERR(0, 24, __pyx_L4_error) }
        __pyx_t_1 = __pyx_memoryview_fromslice(__pyx_v_data, 1, (PyObject *(*)(char *)) __pyx_memview_get_double, (int (*)(char *, PyObject *)) __pyx_memview_set_double, 0);; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 24, __pyx_L4_error)
        __Pyx_GOTREF(__pyx_t_1);
        __pyx_t_2 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_2 == ((Py_ssize_t)-1))) __PYX_ERR(0, 24, __pyx_L4_error)
        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

len() is evaluated after releasing the GIL. The loop bounds should be evaluated before, and the GIL check applied to all parts of the loop.

Also, actually evaluating Python's len() after first converting the memory view to a Python object is stupid. Might also be part of the problem here.

@pshashank8
Copy link

Hi @scoder

I would like to work on this issue. I am trying to reproduce this, could you please help me with this

Thanks

@scoder
Copy link
Contributor Author

scoder commented Jan 2, 2019

@pshashank8, sorry, I think I missed your comment somehow. If you're still interested, see the HackerGuide on how to write a test, then look at the existing prange tests in tests/memoryview/memslice.pyx.

Regarding the implementation, I think it might be enough to call .coerce_to_simple() on the non-trivial prange() arguments during type analysis. That should trigger their ahead-of-time evaluation into a temporary variable, if necessary.

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

No branches or pull requests

2 participants