镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Nested Iterator Seg Fault #435

Closed

Description

@wiwichips

Issue type

Bug

How did you install PythonMonkey?

Source

OS platform and distribution

Ubuntu 22.04.4 LTS x86_64

Python version (python --version)

Python 3.10.12

PythonMonkey version (pip show pythonmonkey)

0.8.2.dev4+935e9e9

Bug Description

Noticed this bug while playing around with iterators.

This is not required for Bifrost2 and can be worked around.

Standalone code to reproduce the issue

import pythonmonkey as pm

make_js_generator = pm.eval("""
function* sliceGenerator(pyIter)
{
    yield python.eval('lambda x: next(x)')(pyIter);
}
sliceGenerator;
""")

next(make_js_generator(range(0,5)))

Relevant log output or backtrace

Segmentation fault (core dumped)

Additional info if applicable

This is not required for Bifrost2 and can be worked around.

What branch of PythonMonkey were you developing on? (If applicable)

No response

Activity

  1. wiwichips commented on Sep 10, 2024

    @wiwichips
    CollaboratorAuthor

    normal generators made in js seem to work, but this one caused a seg fault probably because of the Python code I'm executing inside it?

  2. moved this from Backlog to In progress in PythonMonkey Kanban Boardon Sep 10, 2024
  3. wiwichips commented on Sep 10, 2024

    @wiwichips
    CollaboratorAuthor

    EDIT: this is an invalid REPRO, it still does seg fault, but I'm not sure if it's related to the above repro

    This seg faults because the object instance argument isn't in the next function's parameters. Changing it to next(self):... results in a separate error:

    pythonmonkey.SpiderMonkeyError: Error in file evaluate, on line 3, column 23:
    TypeError: pyiterable is not iterable
    Stack Trace:
      iterateAndPrint@evaluate:3:23
    

    I made a ticket for it here: #436

    Anyways, I'll keep the original comment pasted below the horizontal line below in this comment for context.


    repro # 2

    import pythonmonkey as pm
    
    class JSIter:
        def __init__(self, py_iter):
            self.py_iter = py_iter
    
        def next():
            return next(self.py_iter)
    
    js_iter = JSIter( range(1,5) )
    js_iter = pm.eval('x => { x[Symbol.iterator] = ()=>x; return x }')(js_iter)
    
    pm.eval('x => x.next()')(js_iter)
    Segmentation fault (core dumped)
    
  4. wesgarland commented on Sep 11, 2024

    @wesgarland
    Collaborator

    @wiwichips please follow up with a C++ stack trace from a debug build

  5. wiwichips commented on Sep 11, 2024

    @wiwichips
    CollaboratorAuthor

    @wesgarland - I tried building it with a debug build and didn't see anything, here's the output with BUILD_TYPE=Sanitize

    import pythonmonkey as pm
    
    make_js_generator = pm.eval("""
    function* sliceGenerator(pyIter)
    {
        yield python.eval('lambda x: next(x)')(pyIter);
    }
    sliceGenerator;
    """)
    
    next(make_js_generator(range(0,5)))
    
    AddressSanitizer:DEADLYSIGNAL
    =================================================================
    ==1522488==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000030 (pc 0x7dd33033feaf bp 0x7ffe9e4b68d0 sp 0x7ffe9e4b6500 T0)
    ==1522488==The signal is caused by a READ memory access.
    ==1522488==Hint: address points to the zero page.
        #0 0x7dd33033feaf in getKey /home/will/git/pythonmonkey/src/JSObjectProxy.cc:87
        #1 0x7dd330340ef1 in JSObjectProxyMethodDefinitions::JSObjectProxy_get(JSObjectProxy*, _object*) /home/will/git/pythonmonkey/src/JSObjectProxy.cc:151
        #2 0x7dd330343ae0 in JSObjectProxyMethodDefinitions::JSObjectProxy_iter_next(JSObjectProxy*) /home/will/git/pythonmonkey/src/JSObjectProxy.cc:326
        #3 0x5b64d0546a71  (/usr/bin/python3.10+0x201a71)
        #4 0x5b64d04a0908  (/usr/bin/python3.10+0x15b908)
        #5 0x5b64d0488934 in _PyEval_EvalFrameDefault (/usr/bin/python3.10+0x143934)
        #6 0x5b64d0485095  (/usr/bin/python3.10+0x140095)
        #7 0x5b64d057af65 in PyEval_EvalCode (/usr/bin/python3.10+0x235f65)
        #8 0x5b64d05a5e97  (/usr/bin/python3.10+0x260e97)
        #9 0x5b64d059f79a  (/usr/bin/python3.10+0x25a79a)
        #10 0x5b64d05a5be4  (/usr/bin/python3.10+0x260be4)
        #11 0x5b64d05a50c7 in _PyRun_SimpleFileObject (/usr/bin/python3.10+0x2600c7)
        #12 0x5b64d05a4d12 in _PyRun_AnyFileObject (/usr/bin/python3.10+0x25fd12)
        #13 0x5b64d059770d in Py_RunMain (/usr/bin/python3.10+0x25270d)
        #14 0x5b64d056ddfc in Py_BytesMain (/usr/bin/python3.10+0x228dfc)
        #15 0x7dd334029d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
        #16 0x7dd334029e3f in __libc_start_main_impl ../csu/libc-start.c:392
        #17 0x5b64d056dcf4 in _start (/usr/bin/python3.10+0x228cf4)
    
    AddressSanitizer can not provide additional info.
    SUMMARY: AddressSanitizer: SEGV /home/will/git/pythonmonkey/src/JSObjectProxy.cc:87 in getKey
    ==1522488==ABORTING
    
  6. added this to the v1.0.1 milestone on Sep 11, 2024
  7. philippedistributive commented on Sep 11, 2024

    @philippedistributive
    Contributor

    @wiwichips
    Note that the range object is not an iterator, so the yield expression produces an exception when trying to call next on it.

    indeed:
    python 3.12.3 (main, Jul 31 2024, 17:43:48) [GCC 13.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    a = range(0,5)
    next(a)
    Traceback (most recent call last):
    File "", line 1, in
    TypeError: 'range' object is not an iterator

    So the proper behavior is to exit once this exception is noticed

    Now get:

    pythonmonkey.SpiderMonkeyError: Error in file , on line 1, column 1:
    Error: Python TypeError: 'range' object is not an iterator
    Traceback (most recent call last):
    File "", line 1, in

    JS Stack Trace:
    sliceGenerator@evaluate:4:43

  8. moved this from In progress to In review in PythonMonkey Kanban Boardon Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions