Distributive-Network / PythonMonkey

A Mozilla SpiderMonkey JavaScript engine embedded into the Python VM, using the Python engine to provide the JS host environment.
https://pythonmonkey.io
Other
854 stars 40 forks source link

Segmentation fault when calling `quit()` on a job event listener #387

Closed wiwichips closed 3 months ago

wiwichips commented 4 months ago

Issue type

Bug

How did you install PythonMonkey?

None

OS platform and distribution

Ubuntu 22.04.4 LTS x86_64

Python version (python --version)

3.10

PythonMonkey version (pip show pythonmonkey)

0.7.0

Bug Description

Not sure why in more detail, but when you call quit() on a job.on('accepted') listener, it seg faults every time!

Standalone code to reproduce the issue

#! /usr/bin/env python3

import pythonmonkey as pm
import asyncio

print(pm.__version__)

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

dcpClient = pm.require('dcp-client')
dcpClient['init']()

async def compute_for_it():
    pm.eval('''
    globalThis.myJob = dcp.compute.for(new Array(100), (x) => { progress(); return x + x })
    ''')
loop.run_until_complete(compute_for_it())

my_j = pm.eval("globalThis.myJob")

my_j.on('readystatechange', print)
my_j.on('accepted', lambda: quit())
my_j.on('result', print)

my_j.public.name = 'simple example'

async def exec_it():
    return await my_j.exec()

loop.run_until_complete(exec_it())

Relevant log output or backtrace

0.7.0
exec
init
preauth
deploying
listeners
compute-groups
uploading
deployed
Segmentation fault (core dumped)

Additional info if applicable

No response

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

No response

philippedistributive commented 4 months ago

stack looks like

Thread 1 "python" received signal SIGSEGV, Segmentation fault. 0x00007ffff768597e in FuncType::getPyObject (cx=0x55555561b3f0, fval=...) at /home/philippe/Sources/PythonMonkey/src/FuncType.cc:19 19 proxy->jsFunc->set(&fval.toObject()); (gdb) bt

0 0x00007ffff768597e in FuncType::getPyObject (cx=0x55555561b3f0, fval=...)

at /home/philippe/Sources/PythonMonkey/src/FuncType.cc:19

1 0x00007ffff76c605e in pyTypeFactory (cx=0x55555561b3f0, rval=...)

at /home/philippe/Sources/PythonMonkey/src/pyTypeFactory.cc:108

2 0x00007ffff76b8ef4 in enqueueWithDelay (cx=0x55555561b3f0, argc=4, vp=0x7fffffffb998)

at /home/philippe/Sources/PythonMonkey/src/internalBinding/timers.cc:34

3 0x000026506dd3de1b in ?? ()

4 0x00001327f7a5d780 in ?? ()

5 0x00007fffffffb970 in ?? ()

6 0x0000000000000000 in ?? ()

philippedistributive commented 4 months ago

@wiwichips This is due to quit not being async, (all other functions in the example are async) and is thus not supported without an async wrapper such as

async def async_quit(): quit()

my_j.on('accepted', lambda: async_quit())

zollqir commented 4 months ago

Even if its unsupported, it should result in an exception, not a segmentation fault

philippedistributive commented 4 months ago

also works fine if not using lambda: def non_async_quit(): quit()

my_j.on('accepted', non_async_quit())

philippedistributive commented 4 months ago

just made an incorrect pr closes comment

philippedistributive commented 4 months ago

note same result if call exit or sys.exit